加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-14 17:47:58 所属栏目:Java 来源:网络整理
导读:我正在尝试将以下 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
我正在尝试将以下 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,AsyncConfigurerSupportSchedulingConfigurer.它们是帮助类型,可用于通过异步/调度配置来增强@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>

SchedulingConfigurer具有与task:scheduler相似的设置.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读