首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >升级到springframework.scheduling.concurrent?

升级到springframework.scheduling.concurrent?
EN

Stack Overflow用户
提问于 2011-03-20 15:01:22
回答 3查看 11.4K关注 0票数 16

从Spring3.0开始,ScheduledTimerTask就不再受欢迎了,我不知道如何升级到org.springframework.scheduling.concurrent。

代码语言:javascript
复制
    <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。这是一个简单的任务,它每分钟向publisher发布一条消息。我查过文件但什么也没查..。我不知道从并发包中使用哪种方式最适合。

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

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

代码语言:javascript
复制
@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实例的引用在这里会起作用吗?

代码语言:javascript
复制
scheduledFactoryBean.setScheduledExecutorTasks(new ScheduledExecutorTask[] {onlineTimeSchedule()});
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-03-20 15:18:58

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只需要一个可运行的。

票数 31
EN

Stack Overflow用户

发布于 2016-05-27 16:02:37

Spring 4配置-在从3.2.x迁移到4.6.x之后,下面的配置工作

代码语言:javascript
复制
<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>
票数 7
EN

Stack Overflow用户

发布于 2015-09-16 17:50:11

答案是-添加一个“可运行”字段。

代码语言:javascript
复制
 <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>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5369246

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档