2016-05-20 28 views
9

Tôi có 3 loại sau đây:Xuân @Scheduler song song chạy

ComponantA

package mytest.spring.test.spring; 

import org.apache.log4j.Logger; 
import org.springframework.scheduling.annotation.Scheduled; 
import org.springframework.stereotype.Component; 

@Component 
public class ComponentA { 

    Logger log = Logger.getLogger(ComponentB.class); 

    @Scheduled(fixedRate=2000) 
    public void sayHello() { 
     for(int i=1 ; i<=5 ; i++) { 
      try { 
       Thread.sleep(1000); 
      } catch (InterruptedException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      log.info("Hello from ComponentA " + i); 
     } 
    } 
} 

ComponentB

package mytest.spring.test.spring; 

import org.apache.log4j.Logger; 
import org.springframework.scheduling.annotation.Scheduled; 
import org.springframework.stereotype.Component; 

@Component 
public class ComponentB { 

    Logger log = Logger.getLogger(ComponentB.class); 

    @Scheduled(fixedRate=2000) 
    public void sayHello() { 
     for(int i=1 ; i<=3 ; i++) { 
      try { 
       Thread.sleep(2000); 
      } catch (InterruptedException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      log.info("Hello from ComponentB " + i); 
     } 
    } 
} 

MyApplication

package mytest.spring.test.spring; 

import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
import org.springframework.scheduling.annotation.EnableScheduling; 

@SpringBootApplication 
@EnableScheduling 
public class MyApplication { 

    public static void main(String[] args) { 
     SpringApplication.run(MyApplication.class, args); 
    } 
} 

Khi tôi thực hiện nó, tôi nhận được kết quả như sau:

Hello from ComponentA 1 
Hello from ComponentA 2 
Hello from ComponentA 3 
Hello from ComponentA 4 
Hello from ComponentA 5 
Hello from ComponentB 1 
Hello from ComponentB 2 
Hello from ComponentB 3 
Hello from ComponentA 1 
Hello from ComponentA 2 
Hello from ComponentA 3 
Hello from ComponentA 4 
Hello from ComponentA 5 
Hello from ComponentB 1 
Hello from ComponentB 2 
Hello from ComponentB 3 
... 

Tôi cần 2 phương pháp theo lịch trình để chạy song song, mà rõ ràng là không phải là CAE theo đầu ra tôi nhận được . Tôi đọc rằng có thể cung cấp chú thích @Schedule với TaskExecutor tùy chỉnh, mà chúng ta có thể xác định số lượng sợi mà chúng ta muốn ...

Tôi có đúng không? Tôi không thể tìm cách cung cấp thông tin này.

Trả lời

15

The documentation nêu rõ rằng:

Theo mặc định, sẽ được tìm kiếm một định nghĩa lên lịch liên quan: hoặc là một TaskScheduler đậu độc đáo trong bối cảnh, hoặc một TaskScheduler đậu có tên là "TaskScheduler" hình thức khác; cùng một tra cứu cũng sẽ được thực hiện cho một hạt đậu ScheduledExecutorService. Nếu cả hai số đều không thể giải quyết được, thì trình lên lịch mặc định một luồng cục bộ sẽ là được tạo và sử dụng trong công ty đăng ký tên miền.

Khi có nhiều điều khiển hơn, lớp @Configuration có thể triển khai SchedulingConfigurer. Điều này cho phép truy cập vào cá thể cơ bản ScheduledTaskRegistrar cơ bản. Ví dụ, ví dụ sau chứng minh làm thế nào để tùy chỉnh Executor sử dụng để thực hiện dự kiến ​​ nhiệm vụ:

@Configuration 
@EnableScheduling 
public class AppConfig implements SchedulingConfigurer { 

    @Override 
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { 
     taskRegistrar.setScheduler(taskExecutor()); 
    } 

    @Bean(destroyMethod="shutdown") 
    public Executor taskExecutor() { 
     return Executors.newScheduledThreadPool(100); 
    } 
} 
Các vấn đề liên quan