升级到 springframework.scheduling.concurrent?
从 Spring 3.0 开始,ScheduledTimerTask 已被弃用,我不明白如何升级到 org.springframework.scheduling.concurrent。
<bean id="timerFactoryBean" class="org.springframework.scheduling.timer.TimerFactoryBean">
<property name="scheduledTimerTasks">
<list>
<ref bean="onlineTimeSchedule" />
</list>
</property>
</bean>
<bean id="onlineTimeSchedule" class="org.springframework.scheduling.timer.ScheduledTimerTask">
<property name="timerTask" class="com.example.OnlineTimerTask" />
</property>
<property name="period" value="60000" />
<property name="delay" value="1000" />
</bean>
其中 OnlineTimerTask 扩展了 java.util.TimerTask。这是一个简单的任务,每分钟向发布者发布一条消息。我检查了文档,但什么也没有..我无法理解从并发包中使用哪种方法以及哪种方法最适合。
我还想把这个xml变成Java中的@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()});
As of Spring 3.0 the ScheduledTimerTask is deprecated and I can't understand how to upgrade to org.springframework.scheduling.concurrent.
<bean id="timerFactoryBean" class="org.springframework.scheduling.timer.TimerFactoryBean">
<property name="scheduledTimerTasks">
<list>
<ref bean="onlineTimeSchedule" />
</list>
</property>
</bean>
<bean id="onlineTimeSchedule" class="org.springframework.scheduling.timer.ScheduledTimerTask">
<property name="timerTask" class="com.example.OnlineTimerTask" />
</property>
<property name="period" value="60000" />
<property name="delay" value="1000" />
</bean>
Where the OnlineTimerTask extends java.util.TimerTask. It's simple task which publishes a message to publisher every minute. I checked the documentation, but nothing.. I can't understand which way to use from the concurrent package and which suits the best.
Also I want to turn this xml into @Bean in Java.
EDIT: So I tried to implement the xml with @Bean and @Configuration instead and here is what I got.
@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;
}
}
Will the code above be correct replacement for xml? Will in my case the setScheduledExecutorTasks work properly? I mean will the referencing to the same bean instance, if onlineTimeSchedule() is called more than once, will work here?
scheduledFactoryBean.setScheduledExecutorTasks(new ScheduledExecutorTask[] {onlineTimeSchedule()});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 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 仅需要一个可运行对象。Use
org.springframework.scheduling.concurrent.ScheduledExecutorFactoryBean
in place oforg.springframework.scheduling.timer.TimerFactoryBean
and useorg.springframework.scheduling.concurrent.ScheduledExecutorTask
in place oforg.springframework.scheduling.timer.ScheduledTimerTask
. You will need to adjust the property names and values as needed but, that should be pretty self evident.Optionally, you could refactor your
com.example.OnlineTimerTask
to not extendjava.util.TimeTask
as the ScheduledTimerTask only requires a runnable.Spring 4 配置 - Spring 从 3.2.x 迁移到 4.6.x 后,以下配置工作
Spring 4 configuration - Below configuration working after spring migration from 3.2.x to 4.6.x
答案是——添加一个“可运行”字段
The answer is - add one "runnable" field