java - 如何从 XML Spring 调度配置到注解/代码配置?

标签 java spring configuration annotations scheduled-tasks

我正在尝试将以下 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>

根据 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 和/或@EnableAsync 一样简单。

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

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

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

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

最佳答案

查看类型 AsyncConfigurer , AsyncConfigurerSupport , 和 SchedulingConfigurer .它们是帮助类型,可用于通过异步/调度配置增强 @Configuration 类。

所有这些,以及 @EnabledAsync 的 javadoc ,您将找到设置异步/调度 @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();
     }
 }

 <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 有类似的设置。

关于java - 如何从 XML Spring 调度配置到注解/代码配置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27970650/

相关文章:

php - 为什么我的 php 没有被渲染?为什么我的 php 页面是空白的?

java - SSL 通信 Java

java - 在 Web 应用程序中注册 shutdownHook

java - 使用 IBM WAS ConnectionFactory 和 Spring 时出错

java - 使用 log4j2 时日志文件为空

magento - 打开系统配置时首先显示默认的自定义模块部分

java - -Djava.library.path=... 是否等同于 System.setProperty ("java.library.path", ...)

java - 使用分词器来计算文件中的单词数?

java - 使用java读取HDFS中的文件并通过regex匹配多行 block

javascript - 如何将 javascript (js) Map 传递给 spring boot Controller?