Rih*_*rds 15 java spring spring-3
从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>
Run Code Online (Sandbox Code Playgroud)
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;
}
}
Run Code Online (Sandbox Code Playgroud)
上面的代码是否正确替换xml?在我的情况下,setScheduledExecutorTasks会正常工作吗?我的意思是,如果不止一次调用onlineTimeSchedule(),那么引用同一个bean实例会在这里工作吗?
scheduledFactoryBean.setScheduledExecutorTasks(new ScheduledExecutorTask[] {onlineTimeSchedule()});
Run Code Online (Sandbox Code Playgroud)
Bre*_*den 31
使用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.
小智 6
Spring 4配置 - 以下配置在从3.2.x弹出迁移到4.6.x后工作
<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>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9934 次 |
| 最近记录: |