升级到springframework.scheduling.concurrent?

从Spring 3.0开始,不推荐使用ScheduledTimerTask,我无法理解如何升级到org.springframework.scheduling.concurrent。

             

OnlineTimerTask扩展java.util.TimerTask的位置。 这是一项简单的任务,每分钟都会向发布者发布一条消息。 我检查了文档,但没有..我无法理解从并发包使用哪种方式,哪种方式最适合。

此外,我想在Java中将此xml转换为@Bean。

编辑:所以我尝试用@Bean和@Configuration实现xml,这就是我得到的。

 @Configuration public class ContextConfiguration { @Bean public ScheduledExecutorFactoryBean scheduledExecutorFactoryBean() { ScheduledExecutorFactoryBean scheduledFactoryBean = new ScheduledExecutorFactoryBean(); scheduledFactoryBean.setScheduledExecutorTasks(new ScheduledExecutorTask[] {onlineTimeSchedule()}); return scheduledFactoryBean; } @Bean public ScheduledExecutorTask onlineTimeSchedule() { ScheduledExecutorTask scheduledTask = new ScheduledExecutorTask(); scheduledTask.setDelay(1000); scheduledTask.setPeriod(60000); scheduledTask.setRunnable(new OnlineTimerTask()); return scheduledTask; } } 

上面的代码是否正确替换xml? 在我的情况下,setScheduledExecutorTasks会正常工作吗? 我的意思是,如果不止一次调用onlineTimeSchedule(),那么引用同一个bean实例会在这里工作吗?

 scheduledFactoryBean.setScheduledExecutorTasks(new ScheduledExecutorTask[] {onlineTimeSchedule()}); 

使用org.springframework.scheduling.concurrent.ScheduledExecutorFactoryBean代替org.springframework.scheduling.timer.TimerFactoryBean并使用org.springframework.scheduling.concurrent.ScheduledExecutorTask代替org.springframework.scheduling.timer.ScheduledTimerTask 。 您需要根据需要调整属性名称和值,但这应该是非常明显的。

(可选)您可以重构com.example.OnlineTimerTask以不扩展java.util.TimeTask因为ScheduledTimerTask只需要一个runnable。

Spring 4配置 – 以下配置在从3.2.x弹出迁移到4.6.x后工作

                 

答案是 – 添加一个“runnable”字段

     10    1000