Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring boot. How to Create TaskExecutor with Annotation?

Tags:

I did a @Service class in Spring Boot application with one of the methods that should run asynchronously. As I read method should be @Async annotated and also I have to run a TaskExecutor bean. But in Spring manual http://docs.spring.io/spring/docs/current/spring-framework-reference/html/scheduling.html I not find any info or example how to run TaskExecutor with annotation, without XML config. Is it possible to create TaskExecutor bean in Spring Boot without XML, with annotations only? Here my Service class:

@Service public class CatalogPageServiceImpl implements CatalogPageService {      @Override     public void processPagesList(List<CatalogPage> catalogPageList) {         for (CatalogPage catalogPage:catalogPageList){             processPage(catalogPage);         }     }      @Override     @Async("locationPageExecutor")     public void processPage(CatalogPage catalogPage) {         System.out.println("print from Async method "+catalogPage.getUrl());     } } 
like image 261
Pavlo Morozov Avatar asked Jul 14 '16 09:07

Pavlo Morozov


People also ask

What does @async annotation do?

Simply put, annotating a method of a bean with @Async will make it execute in a separate thread. In other words, the caller will not wait for the completion of the called method. One interesting aspect in Spring is that the event support in the framework also has support for async processing if necessary.

What is the use of TaskExecutor in Spring?

The TaskExecutor was originally created to give other Spring components an abstraction for thread pooling where needed. Components such as the ApplicationEventMulticaster , JMS's AbstractMessageListenerContainer , and Quartz integration all use the TaskExecutor abstraction to pool threads.


2 Answers

Add a @Bean method to your Spring Boot application class:

@SpringBootApplication @EnableAsync public class MySpringBootApp {      @Bean     public TaskExecutor taskExecutor() {         ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();         executor.setCorePoolSize(5);         executor.setMaxPoolSize(10);         executor.setQueueCapacity(25);         return executor;     }      public static void main(String[] args) {         // ...     } } 

See Java-based container configuration in the Spring Framework reference documentation on how to configure Spring using Java config instead of XML.

(Note: You don't need to add @Configuration to the class because @SpringBootApplication already includes @Configuration).

like image 60
Jesper Avatar answered Sep 19 '22 11:09

Jesper


First – let’s go over the rules – @Async has two limitations:

  • it must be applied to public methods only
  • self-invocation – calling the async method from within the same class – won’t work

So your processPage() method should be in separate class

like image 27
Igor Shevchenko Avatar answered Sep 21 '22 11:09

Igor Shevchenko