Spring Boot role in @Async

        In Spring, @ Async this annotation asynchronous method for labeling. Once the method of marking this way, when the other thread calls this method, it will open a new thread to handle asynchronous business logic.

 Use this annotation Description:

       1, annotations can be used in this method, it can also be used in class (if used in the class, all the methods in this class is asynchronous)

       2, this class object annotation method, the object needs to be under spring management bean

       3, the program's main class or the main class of this comment, you need to enable asynchronous open configuration, the configuration on @EnableAsync comment

 

    To Spring boot, for example, start class increase @EnableAsync ,

@EnableAsync
@SpringBootApplication
public class ManageApplication {

}

Asynchronous categories:

@Component
public class MyAsyncTask {
@Async
public void asyncCpsItemImportTask(Long platformId, String jsonList){}
} 

The above configuration will enable the default actuator, asynchronous execution of the specified method.

In the business scenario, sometimes you need to use their own definition of actuators to run asynchronous business logic, then how to do it?

After the transformation of the code above is as follows:

@EnableAsync
@SpringBootApplication
public class ManageApplication {

 

@Bean("MyExecutor")
public TaskExecutor workExecutor1(){
ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
threadPoolTaskExecutor.setThreadNamePrefix("parseMyTask");
threadPoolTaskExecutor.setCorePoolSize(10);
threadPoolTaskExecutor.setMaxPoolSize(30);
threadPoolTaskExecutor.setQueueCapacity(100);
threadPoolTaskExecutor.afterPropertiesSet();
return threadPoolTaskExecutor;
}

 

}

Asynchronous categories:

@Component
public class MyAsyncTask {
@Async("MyExecutor")
public void asyncCpsItemImportTask(Long platformId, String jsonList){}
}

 

Guess you like

Origin www.cnblogs.com/xuzhujack/p/11322439.html