如何从 XML Spring 调度配置到注解/代码配置? [英] How to go from XML Spring scheduling configuration to annotation/code configuration?

查看:24
本文介绍了如何从 XML Spring 调度配置到注解/代码配置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

<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:executor id="myExecutor" pool-size="5"/><task:scheduler id="myScheduler" pool-size="10"/>

代码配置就像启用@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) 有没有办法创建一个我可以控制名称和池大小的调度程序实例?

解决方案

查看类型 AsyncConfigurer, AsyncConfigurerSupportSchedulingConfigurer.它们是可用于通过异步/调度配置增强 @Configuration 类的辅助类型.

跨越所有这些,以及@EnabledAsync,您将找到设置异步/调度@Configuration 类所需的所有设置方法.

给出的例子等于

 @Configuration@EnableAsync公共类 AppConfig 实现 AsyncConfigurer {@豆角,扁豆公共 MyAsyncBean asyncBean() {返回新的 MyAsyncBean();}@覆盖公共执行器 getAsyncExecutor() {ThreadPoolTask​​Executor executor = new ThreadPoolTask​​Executor();executor.setCorePoolSize(7);executor.setMaxPoolSize(42);executor.setQueueCapacity(11);executor.setThreadNamePrefix("MyExecutor-");executor.initialize();返回执行器;}@覆盖公共 AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {返回新的 MyAsyncUncaughtExceptionHandler();}}

 <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"/></豆类>

SchedulingConfigurertask:scheduler 有类似的设置.

I am trying to convert the following Spring task xml configuration to a purely code/annotation based 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>

Per the Spring spec, 28.4.1 (http://docs.spring.io/spring/docs/current/spring-framework-reference/html/scheduling.html), they say that to go from XML like this:

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

to code configuration is as simply as enabling either @EnableScheduling and/or @EnableAsync.

However, I don't see anywhere I can actually instantiate the scheduler. The javadoc for @EnableScheduling (http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/scheduling/annotation/EnableScheduling.html) shows how I can get plug in my own created Executor, though I'm not exactly 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 I can schedule my createPartitions method using the configureTasks override. However, I would like to be able to name my scheduler (so I can identify its threads) and control its pool size.

So, I wish to know these things:

1) What class can I use to set the executor fields that the XML has?

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

解决方案

Check out the types AsyncConfigurer, AsyncConfigurerSupport, and SchedulingConfigurer. They are helper types you can use to enhance your @Configuration class with async/scheduling configurations.

Across all of them, and the javadoc of @EnabledAsync, you'll find all the setup methods you need to setup your async/scheduling @Configuration class.

The example given equates

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

SchedulingConfigurer has a similar setup for task:scheduler.

这篇关于如何从 XML Spring 调度配置到注解/代码配置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆