Java 升级到springframework.scheduling.concurrent?
从Spring3.0开始,ScheduledTimerTask已被弃用,我无法理解如何升级到org.springframework.scheduling.concurrentJava 升级到springframework.scheduling.concurrent?,java,spring,spring-3,Java,Spring,Spring 3,从Spring3.0开始,ScheduledTimerTask已被弃用,我无法理解如何升级到org.springframework.scheduling.concurrent <bean id="timerFactoryBean" class="org.springframework.scheduling.timer.TimerFactoryBean"> <property name="scheduledTimerTasks">
<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>
上面的代码是否正确地替代了xml?在我的情况下,setScheduledExecutorTasks是否正常工作?我的意思是,如果不止一次调用onlineTimeSchedule(),那么对同一bean实例的引用会在这里起作用吗
scheduledFactoryBean.setScheduledExecutorTasks(new ScheduledExecutorTask[] {onlineTimeSchedule()});
使用
org.springframework.scheduling.concurrent.ScheduledExecutorFactoryBean
代替org.springframework.scheduling.timer.TimerFactoryBean
并使用org.springframework.ScheduledExecutorTask
代替org.springframework.scheduled.timer.ScheduledTimerTask
。您需要根据需要调整属性名称和值,但这应该是不言而喻的
或者,您可以重构com.example.OnlineTimerTask
以不扩展java.util.TimeTask
,因为ScheduledTimerTask只需要一个可运行的。答案是-添加一个“可运行”字段
10
1000
Spring 4配置-以下配置在Spring从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>
@Richards正确,执行者不要求任务是TimerTask实例。他们只需要实现Runnable。你有一个打字错误,这就是为什么我这么说。但是,是的,删除了TimerTask。fixedRate是什么意思?我确实设置了周期、延迟和任务,但无法确定fixedRate的意义。尝试按照您所说的操作,我得到了以下结果:创建名为“timerFactoryBean”的bean时出错,该bean在中定义为。。。使用键[0]设置bean属性“scheduledExecutorTasks”时,无法解析对bean“onlineTimeSchedule”的引用;嵌套异常是。。。BeanCreationException:创建名为“onlineTimeSchedule”的bean时出错,该名称在…设置属性值时出错;嵌套异常是。。。NotWritablePropertyException:bean类的属性“executorTask”无效。。。ScheduledExecutorTask:Bean属性“executorTask”不可写或具有无效的setter方法。setter的参数类型与getter的返回类型匹配吗?@Anjanaa可以帮助回答您的问题吗?
<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>
<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>