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

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

问题描述

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

 < 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.schedulerpool size =$ {xyz.job.scheduler.pool.size:4}/>

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

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

< task:scheduled-tasks scheduler =xyz.scheduler>
< task:scheduled ref =partitionermethod =createPartitionscron =$ {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 =myExecutorpool-size =5/>
< task:scheduler id =mySchedulerpool-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 AsyncConfigurerSupport SchedulingConfigurer 。它们是帮助类型,可用于使用异步/调度配置来增强 @Configuration 类。



以及 <$ c $的javadoc c> @EnabledAsync ,您将找到设置异步/调度 @Configuration 类所需的所有设置方法。 / p>

示例给出等于

  @Configuration 
@EnableAsync
public class AppConfig实现AsyncConfigurer {

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

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

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

 < beans> 
< task:annotation-driven executor =myExecutorexception-handler =exceptionHandler/>
< task:executor id =myExecutorpool-size =7-42queue-capacity =11/>
< bean id =asyncBeanclass =com.foo.MyAsyncBean/>
< bean id =exceptionHandlerclass =com.foo.MyAsyncUncaughtExceptionHandler/>
< / beans>

SchedulingConfigurer c $ c> task: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 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天全站免登陆