如何从 XML Spring 调度配置转到 annotation/code 配置?

How to go from XML Spring scheduling configuration to annotation/code configuration?

我正在尝试将以下 Spring 任务 xml 配置转换为纯粹基于 code/annotation 的版本:

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

根据 Spring 规范,28.4.1 (http://docs.spring.io/spring/docs/current/spring-framework-reference/html/scheduling.html),他们说从 XML 开始是这样的:

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

代码配置就像启用 @EnableScheduling and/or @EnableAsync.

一样简单

但是,我没有看到任何可以实际实例化调度程序的地方。 @EnableScheduling (http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/scheduling/annotation/EnableScheduling.html) 的 javadoc 展示了我如何插入我自己创建的执行器,尽管我不确定它应该是什么 class(我仍然希望能够控制池大小、队列容量和拒绝策略)。它还展示了我如何使用 configureTasks 覆盖来安排我的 createPartitions 方法。但是,我希望能够命名我的调度程序(以便我可以识别它的线程)并控制它的池大小。

所以,我想知道这些:

1) 我可以使用什么 class 来设置 XML 具有的执行程序字段?

2) 有没有办法创建一个我可以控制其名称和池大小的调度程序实例?

检查类型 AsyncConfigurer, AsyncConfigurerSupport, and SchedulingConfigurer。它们是辅助类型,您可以使用它们来增强 @Configuration class 和 async/scheduling 配置。

在所有这些以及 @EnabledAsync 的 javadoc 中,您将找到设置 async/scheduling @Configuration class 所需的所有设置方法。 =22=]

给出的示例等同于

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

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

SchedulingConfigurertask:scheduler 的设置类似。

如果你想要更细粒度的控制,你可以另外实现 SchedulingConfigurer and/or AsyncConfigurer 接口。

如下,

请注意泳池,

@Configuration
@EnableScheduling
public class CronConfig implements SchedulingConfigurer{

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


     @Bean(destroyMethod="shutdown")
     public Executor taskExecutor() {
         return Executors.newScheduledThreadPool(10);
     }

}

对于异步,

@Configuration
@EnableAsync
public class AsyncConfig implements AsyncConfigurer {

    @Override
    public Executor getAsyncExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(5);
        executor.setMaxPoolSize(10);
        executor.setQueueCapacity(100);
        executor.initialize();
        return executor;
    }

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

请注意,@EnableAsync@EnableScheduling 必须存在才能正常工作。