java 如何从 XML Spring 调度配置到注解/代码配置?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27970650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to go from XML Spring scheduling configuration to annotation/code configuration?
提问by AHungerArtist
I am trying to convert the following Spring task xml configuration to a purely code/annotation based version:
我正在尝试将以下 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.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:
根据 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"/>
to code configuration is as simply as enabling either @EnableScheduling and/or @EnableAsync.
代码配置就像启用@EnableScheduling 和/或@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.
但是,我看不到任何可以实际实例化调度程序的地方。@EnableScheduling ( http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/scheduling/annotation/EnableScheduling.html)的 javadoc展示了如何插入我自己创建的 Executor,虽然我不确定它应该是什么类(我仍然希望能够控制池大小、队列容量和拒绝策略)。它还展示了如何使用 configureTasks 覆盖来安排我的 createPartitions 方法。但是,我希望能够命名我的调度程序(以便我可以识别其线程)并控制其池大小。
So, I wish to know these things:
所以,我想知道这些事情:
1) What class can I use to set the executor fields that the XML has?
1) 我可以使用什么类来设置 XML 具有的执行程序字段?
2) Is there a way to create a scheduler instance that I can control the name and pool size of?
2)有没有办法创建一个我可以控制名称和池大小的调度程序实例?
回答by Sotirios Delimanolis
Check out the types AsyncConfigurer
, AsyncConfigurerSupport
, and SchedulingConfigurer
. They are helper types you can use to enhance your @Configuration
class with async/scheduling configurations.
退房的类型AsyncConfigurer
,AsyncConfigurerSupport
和SchedulingConfigurer
。它们是可用于@Configuration
通过异步/调度配置增强类的辅助类型。
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.
在所有这些以及 的 javadoc 中@EnabledAsync
,您将找到设置异步/调度@Configuration
类所需的所有设置方法。
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
.
SchedulingConfigurer
有类似的设置task:scheduler
。
回答by diyoda_
If you want more fine-grained control you can additionally implement the SchedulingConfigurer
and/or AsyncConfigurer
interfaces.
如果您想要更细粒度的控制,您可以另外实现SchedulingConfigurer
和/或AsyncConfigurer
接口。
As belows,
如下,
Please notice the pools also,
还请注意游泳池,
@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);
}
}
And for the 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();
}
}
Note that @EnableAsync
and @EnableScheduling
has to be there for this to work.
请注意,@EnableAsync
并且@EnableScheduling
必须在那里才能工作。