2016-05-20 18 views
9

I mają następujące 3 klasy:Wiosna @Scheduler równolegle działa

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); 
    } 
} 

Kiedy go wykonać, ja otrzymuję następujący wynik:

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 
... 

muszę 2 zaplanowanego Metody biec równolegle, co oczywiście nie jest cae według wyjściu dostaję . Czytałem, że powinno być możliwe podanie adnotacji @Schedule z niestandardowym TaskExecutor, z którym powinno być możliwe określenie, ile wątku chcemy ...

Mam rację? Nie mogę znaleźć informacji na ten temat.

Odpowiedz

15

The documentation wyraźnie stwierdza, że:

domyślnie będzie poszukiwanie powiązanej definicji harmonogramu: albo unikalny TaskScheduler fasoli w kontekście, czy TaskScheduler fasoli nazwie „taskScheduler” inaczej; to samo wyszukiwanie będzie również wykonane jako dla fasoli ScheduledExecutorService. Jeśli żadna z dwóch opcji nie zostanie rozstrzygnięta, lokalny, domyślny, jednowątkowy program planujący będzie utworzony i używany w rejestratorze.

Gdy wymagana jest większa kontrola, klasa @Configuration może implementować SchedulingConfigurer. Umożliwia to dostęp do podstawowej instancji ScheduledTaskRegistrar. Na przykład, następujący przykład demonstruje jak dostosować wykonawca używany do wykonywania zaplanowanych zadania:

@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); 
    } 
}