升级到 springframework.scheduling.concurrent?

发布于 2024-10-23 15:40:44 字数 1970 浏览 2 评论 0原文

从 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>

其中 OnlineTimerTask 扩展了 java.util.TimerTask。这是一个简单的任务,每分钟向发布者发布一条消息。我检查了文档,但什么也没有..我无法理解从并发包中使用哪种方法以及哪种方法最适合。

我还想把这个xml变成Java中的@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;
    }
}

上面的代码可以正确替换 xml 吗?在我的情况下,setScheduledExecutorTasks 能正常工作吗?我的意思是,如果 onlineTimeSchedule() 被多次调用,对同一个 bean 实例的引用会在这里起作用吗?

scheduledFactoryBean.setScheduledExecutorTasks(new ScheduledExecutorTask[] {onlineTimeSchedule()});

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>

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.

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

东北女汉子 2024-10-30 15:40:44

使用 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 仅需要一个可运行对象。

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.

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

月隐月明月朦胧 2024-10-30 15:40:44

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>

Spring 4 configuration - Below configuration working after spring migration from 3.2.x to 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>
童话里做英雄 2024-10-30 15:40:44

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

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

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文