Java – how do I go from XML spring scheduling configuration to annotation / code configuration?

I am trying to convert the following spring task XML configuration to a pure code / annotation version:

<task:executor id="xyz.executor"
    pool-size="${xyz.job.executor.pool.size:1-40}"
    queue-capacity="${xyz.job.executor.queue.capacity:0}"
    rejection-policy="CALLER_RUNS"/>

<task:scheduler id="xyz.scheduler" pool size="${xyz.job.scheduler.pool.size:4}"  />

<task:annotation-driven executor="xyz.executor" scheduler="xyz.scheduler" />

<bean id='xyzProcessor' class="xyz.queueing.QueueProcessor" /> 

<task:scheduled-tasks scheduler="xyz.scheduler" >
    <task:scheduled ref="partitioner" method="createPartitions" cron="${xyz.job.partitioner.interval:0 0 3 * * *}" />
</task:scheduled-tasks>

According to the spring specification, 28.4 1( http://docs.spring.io/spring/docs/current/spring-framework-reference/html/scheduling.html ), they say from XML:

<task:annotation-driven executor="myExecutor" scheduler="myScheduler"/>
<task:executor id="myExecutor" pool-size="5"/>
<task:scheduler id="myScheduler" pool-size="10"/>

The code configuration is as simple as enabling @ enablesscheduling and / or @ enableasync

However, I don't see any schedulers that can actually be instantiated@ EnableScheduling( http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/scheduling/annotation/EnableScheduling.html )The Javadoc shows how I can insert the executor I created myself, although I'm not completely sure what class it should be (I still want to be able to control the pool size, queue capacity and rejection Policy) It also shows how to use configuretasks to override my createpartitions method However, I want to name my scheduler (so I can identify its thread) and control its pool size

So I want to know these things:

1) What classes can I use to set the executor field of XML?

2) Is there any way to create a scheduler instance that can control the name and pool size?

Solution

View the types asyncconfigurator, asyncconfigurersupport, and schedulingconfigurator They are help types that can be used to enhance the @ configuration class through asynchronous / scheduling configuration

In all of these, as well as the Javadoc of @ enabledasync, you can find all the installation methods required to set up the asynchronous / scheduling @ configuration class

The examples given are quite

@Configuration
 @EnableAsync
 public class AppConfig implements AsyncConfigurer {

     @Bean
     public MyAsyncBean asyncBean() {
         return new MyAsyncBean();
     }

     @Override
     public Executor getAsyncExecutor() {
         ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
         executor.setCorePoolSize(7);
         executor.setMaxPoolSize(42);
         executor.setQueueCapacity(11);
         executor.setThreadNamePrefix("MyExecutor-");
         executor.initialize();
         return executor;
     }

     @Override
     public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
         return new MyAsyncUncaughtExceptionHandler();
     }
 }

with

<beans>
     <task:annotation-driven executor="myExecutor" exception-handler="exceptionHandler"/>
     <task:executor id="myExecutor" pool-size="7-42" queue-capacity="11"/>
     <bean id="asyncBean" class="com.foo.MyAsyncBean"/>
     <bean id="exceptionHandler" class="com.foo.MyAsyncUncaughtExceptionHandler"/>
 </beans>

Schedulingconfigurator has settings similar to task: scheduler

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>