java 升级到 springframework.scheduling.concurrent?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5369246/
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
Upgrading to springframework.scheduling.concurrent?
提问by Rihards
As of Spring 3.0 the ScheduledTimerTask is deprecated and I can't understand how to upgrade to org.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>
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.
其中 OnlineTimerTask 扩展了 java.util.TimerTask。这是每分钟向发布者发布一条消息的简单任务。我检查了文档,但什么也没有。我无法理解从并发包中使用哪种方式以及哪种方式最适合。
Also I want to turn this xml into @Bean in Java.
另外我想把这个xml变成Java中的@Bean。
EDIT:So I tried to implement the xml with @Bean and @Configuration instead and here is what I got.
编辑:所以我尝试用@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;
}
}
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?
上面的代码是否可以正确替换 xml?在我的情况下 setScheduledExecutorTasks 会正常工作吗?我的意思是,如果 onlineTimeSchedule() 被多次调用,对同一个 bean 实例的引用会在这里工作吗?
scheduledFactoryBean.setScheduledExecutorTasks(new ScheduledExecutorTask[] {onlineTimeSchedule()});
回答by Brent Worden
Use org.springframework.scheduling.concurrent.ScheduledExecutorFactoryBean
in place of org.springframework.scheduling.timer.TimerFactoryBean
and use org.springframework.scheduling.concurrent.ScheduledExecutorTask
in place of org.springframework.scheduling.timer.ScheduledTimerTask
. You will need to adjust the property names and values as needed but, that should be pretty self evident.
使用org.springframework.scheduling.concurrent.ScheduledExecutorFactoryBean
替代org.springframework.scheduling.timer.TimerFactoryBean
和使用org.springframework.scheduling.concurrent.ScheduledExecutorTask
到位org.springframework.scheduling.timer.ScheduledTimerTask
。您需要根据需要调整属性名称和值,但这应该是不言而喻的。
Optionally, you could refactor your com.example.OnlineTimerTask
to not extend java.util.TimeTask
as the ScheduledTimerTask only requires a runnable.
或者,您可以重构您com.example.OnlineTimerTask
的不扩展,java.util.TimeTask
因为 ScheduledTimerTask 只需要一个可运行的。
回答by Balu Gulla
Spring 4 configuration - Below configuration working after spring migration from 3.2.x to 4.6.x
Spring 4 配置 - 在从 3.2.x 到 4.6.x 的 spring 迁移后,下面的配置工作
<bean id="schedulerTask"
class="org.springframework.scheduling.support.MethodInvokingRunnable">
<property name="targetObject" ref="springJmsListnerContainer" />
<property name="targetMethod" value="execute" />
</bean>
<bean id="timerTask" class="org.springframework.scheduling.concurrent.ScheduledExecutorTask">
<property name="runnable" ref="schedulerTask" />
<property name="delay" value="100" />
<property name="period" value="60000" />
</bean>
<bean class="org.springframework.scheduling.concurrent.ScheduledExecutorFactoryBean">
<property name="scheduledExecutorTasks">
<list>
<ref bean="timerTask" />
</list>
</property>
</bean>
回答by Aman
The answer is - add one "runnable" field
答案是——添加一个“可运行”字段
<bean id="scheduledExecutorTask"
class="org.springframework.scheduling.concurrent.ScheduledExecutorTask">
<!-- wait 10 milli seconds before starting repeated execution -->
<property name="delay">
<value>10</value>
</property>
<!-- run every 1 second -->
<property name="period">
<value>1000</value>
</property>
<property name="runnable">
<ref bean="checkInvokingTask"/>
</property>
</bean>