升级到springframework.scheduling.concurrent吗? [英] Upgrading to springframework.scheduling.concurrent?

查看:425
本文介绍了升级到springframework.scheduling.concurrent吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从Spring 3.0开始,不建议使用ScheduledTimerTask,我不明白如何升级到org.springframework.scheduling.concurrent.

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>

OnlineTimerTask在其中扩展了java.util.TimerTask.这是一个简单的任务,每分钟都会向发布者发布一条消息.我检查了文档,但一无所获..我不明白从并发包中使用哪种方法,哪种方法最合适.

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.

我也想将此xml转换为Java中的@Bean.

Also I want to turn this xml into @Bean in Java.

因此,我尝试改为使用@Bean和@Configuration来实现xml,这就是我所得到的.

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;
    }
}

上面的代码是否可以正确替换xml?就我而言,setScheduledExecutorTasks是否可以正常工作?我的意思是,如果多次调用onlineTimeSchedule(),则引用同一bean实例是否可以在这里工作?

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()});

推荐答案

使用org.springframework.scheduling.concurrent.ScheduledExecutorFactoryBean代替org.springframework.scheduling.timer.TimerFactoryBean,并使用org.springframework.scheduling.concurrent.ScheduledExecutorTask代替org.springframework.scheduling.timer.ScheduledTimerTask.您将需要根据需要调整属性名称和值,但这应该是不言而喻的.

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.

(可选)您可以重构com.example.OnlineTimerTask以不扩展java.util.TimeTask,因为ScheduledTimerTask只需要一个可运行的对象.

Optionally, you could refactor your com.example.OnlineTimerTask to not extend java.util.TimeTask as the ScheduledTimerTask only requires a runnable.

这篇关于升级到springframework.scheduling.concurrent吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆