java 延迟任务:Spring 3 中的调度器第一次执行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2080717/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 19:19:23  来源:igfitidea点击:

Delay task:scheduler first execution in Spring 3

javaspring

提问by Gerco Dries

I have a simple application that uses Spring 3 for dependency injection. I have a JFrame for the user to look at and some background tasks for synchronizing with a back-end server and local database maintenance.

我有一个简单的应用程序,它使用 Spring 3 进行依赖注入。我有一个 JFrame 供用户查看,还有一些后台任务用于与后端服务器和本地数据库维护同步。

This is the relevant part of my application context:

这是我的应用程序上下文的相关部分:

<task:scheduler id="scheduler" pool-size="1"/>
<task:scheduled-tasks scheduler="scheduler">
    <task:scheduled ref="synchronizer" method="incrementalSync" fixed-delay="600000"/>
    ... more tasks ...
</task:scheduled-tasks>

<bean id="mainFrame" class="nl.gdries.myapp.client.ui.MainFrame">
    ... properties and such ...
</bean>

When I start this applicationContext the scheduler immediately starts executing the background tasks even while my UI is loading. Because the first task is a rather heavy one at the start I want it to wait for the UI to fully load and display before it starts execution.

当我启动这个 applicationContext 时,即使我的 UI 正在加载,调度程序也会立即开始执行后台任务。因为第一个任务在开始时相当繁重,我希望它在开始执行之前等待 UI 完全加载和显示。

Does anyone know how to tell Spring to delay executing the scheduled tasks until a moment of my choosing?

有谁知道如何告诉 Spring 将计划任务的执行延迟到我选择的某个时刻?

采纳答案by skaffman

This seems to have been left out of the <task:scheduled>bean definition, something I only just noticed last week.

这似乎已被排除在<task:scheduled>bean 定义之外,这是我上周才注意到的。

Remember, though, that the <task:...>definitions are just shortcuts, you can always use the explicit approach, by defining a ScheduledExecutorFactoryBean, with nested ScheduledExecutorTaskbeans. This gives you much finer control, including initialDelay.

但是请记住,<task:...>定义只是快捷方式,您始终可以使用显式方法,通过ScheduledExecutorFactoryBean使用嵌套ScheduledExecutorTaskbean定义 a 。这为您提供了更精细的控制,包括initialDelay.

回答by inv

I've had the same problem and came back to TimerTask as it is in 25.7.1 point in http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/scheduling.html

我遇到了同样的问题,回到了 TimerTask,因为它在http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/scheduling.html中的 25.7.1 点

<bean id="scheduledTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">
    <!--  wait 25 seconds before starting repeated execution --> 
    <property name="delay" value="25000" />
    <!--  run every 50 seconds -->
    <property name="period" value="50000" />
    <property name="timerTask" ref="task" />
</bean>

<bean id="timerFactory" class="org.springframework.scheduling.timer.TimerFactoryBean">
    <property name="scheduledTimerTasks">
        <list>
            <ref bean="scheduledTask" />
        </list>
    </property>
</bean>

I hope in Spring 3.1 will be initialDelay attribute in <task:scheduled>, since in Spring 3.0 TimerFactoryBean is Deprecated. You can vote for this issue: jira.springframework.org/browse/SPR-7022

我希望在 Spring 3.1 中将是 initialDelay 属性<task:scheduled>,因为在 Spring 3.0 TimerFactoryBean 中已弃用。您可以为此问题投票:jira.springframework.org/browse/SPR-7022

回答by Liv

This has been introduced by the way in spring 3.2so if you use the 3.2 schema it's available again -- e.g.:

这已在spring 3.2 中引入,因此如果您使用 3.2 模式,它再次可用 - 例如:

<?xml version="1.0"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
       http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd">

....

....

the above allows you to do this:

以上允许你这样做:

<task:scheduler id="scheduler" pool-size="1"/>
<task:scheduled-tasks scheduler="scheduler">
    <task:scheduled ref="synchronizer" method="incrementalSync" fixed-delay="600000" initial-delay="initial delay needed for the app to start"/>
    ... more tasks ...
</task:scheduled-tasks>