如何从XML Spring调度配置转向注释/代码配置?

我试图将以下Spring任务xml配置转换为纯粹基于代码/注释的版本:

       

根据Spring规范28.4.1( http://docs.spring.io/spring/docs/current/spring-framework-reference/html/scheduling.html ),他们说要像这样从XML转发:

    

代码配置就像启用@EnableScheduling和/或@EnableAsync一样简单。

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

所以,我想知道这些事情:

1)我可以使用哪个类来设置XML具有的执行器字段?

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

查看AsyncConfigurerAsyncConfigurerSupportSchedulingConfigurer类型。 它们是辅助类型,您可以使用它们来增强具有异步/调度配置的@Configuration类。

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

给出的例子等同于

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

        

SchedulingConfigurer具有类似的task:scheduler设置task:scheduler

如果您想要更细粒度的控制,您还可以实现SchedulingConfigurer和/或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); } } 

对于Asyncs,

 @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必须在此处才能工作。