Spring/Java 中的调度任务

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15511965/
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-31 19:52:28  来源:igfitidea点击:

Scheduling Task in Spring/Java

javaspringscheduled-tasks

提问by Ashu

I am spawning a thread which will keep pulling the chunk of records from database and putting them into the queue. This thread will be started on the server load. I want this thread to be active all the time. If there are no records in the database I want it to wait and check again after some time. I was thinking of using spring task scheduler to schedule this but not sure if that is right because I only want my task to be started once. What will be the good way of implementing this in Spring ?

我正在生成一个线程,它将不断从数据库中提取记录块并将它们放入队列中。该线程将在服务器负载时启动。我希望这个线程一直处于活动状态。如果数据库中没有记录,我希望它等待一段时间后再次检查。我正在考虑使用 spring 任务调度程序来安排这个,但不确定这是否正确,因为我只希望我的任务启动一次。在 Spring 中实现这一点的好方法是什么?

Also, i need to have a boundary check that if my thread goes down (because of any error or exception condition) it should be re-instantiated after some time.

此外,我需要进行边界检查,如果我的线程关闭(由于任何错误或异常情况),它应该在一段时间后重新实例化。

I can do all this in java by using thread communication methods but just trying if there is something available in Spring or Java for such scenarios.

我可以通过使用线程通信方法在 java 中完成所有这些,但只是尝试在 Spring 或 Java 中是否有适用于此类场景的东西。

Any suggestions or pointer will help.

任何建议或指针都会有所帮助。

回答by Kevin Bowersox

You can use the @Scheduledannotation to run jobs. First create a class with a method that is annotated with @Scheduled.

您可以使用@Scheduled注释来运行作业。首先创建一个带有注释的方法的类@Scheduled

Class

班级

public class GitHubJob {

   @Scheduled(fixedDelay = 604800000)
   public void perform() {
      //do Something
    }
}

Then register this class in your configuration files.

然后在您的配置文件中注册这个类。

spring-context.xml

spring-context.xml

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

<tx:annotation-driven/>
<task:annotation-driven scheduler="myScheduler"/>

<task:scheduler id="myScheduler" pool-size="10"/>
<bean id="gitHubJob" class="org.tothought.spring.jobs.GitHubJob"/>

</beans>

For more about scheduling visit the Spring Docs.

有关调度的更多信息,请访问Spring Docs

回答by CorayThan

@Scheduled(fixedDelay=3600000)
private void refreshValues() {
   [...]
}

That runs a task once every hour. It needs to be void and accept no arguments. If you use Spring's Java configuration you'll also need to add the annotation @EnableSchedulingto one of your @Configurationclasses.

每小时运行一次任务。它必须是无效的并且不接受任何参数。如果您使用 Spring 的 Java 配置,您还需要将注释添加@EnableScheduling到您的@Configuration类之一。

回答by Henry Leu

I think your requirement is just regular senario which quartz or spring scheduling framework supports very well. but you want to create a spececial approach to impl it. my suggestion is to keep it simple and stupid. spring scheudling will take advantage your worker thread by pooling instead of runing it all the time.

我认为您的要求只是普通的 senario,quartz 或 spring 调度框架支持得很好。但是您想创建一种特殊的方法来实现它。我的建议是保持简单和愚蠢。spring scheudling 将通过池化而不是一直运行来利用您的工作线程。

As of statistics, it 's easy to check worker class's log. if you want see the statistics in web console, you need to record worker's log in database. I am sure you could make it easily in normal way.

从统计上看,查看worker 类的日志很容易。如果要在 Web 控制台中查看统计信息,则需要在数据库中记录工作人员的日志。我相信您可以通过正常方式轻松完成。

回答by jason gotlieb

You can try using Quartz scheduler. http://quartz-scheduler.org/That will allow you to specify the duration of time between task executions. You can set a flag (boolean) that says if the class has run before to avoid duplicating code that you only want to run the 'first' time.

您可以尝试使用 Quartz 调度程序。http://quartz-scheduler.org/这将允许您指定任务执行之间的持续时间。您可以设置一个标志(布尔值),表明该类之前是否运行过,以避免重复您只想在“第一次”运行的代码。

回答by pmckeown

Spring has out-ot-the-box support for scheduling tasks. Here are for the docsfor the latest 3.2.x version of spring but check the docs for the version you are using. Looks like it uses Quartz under the hood for scheduling tasks.

Spring 对调度任务具有开箱即用的支持。 以下是最新 3.2.x 版本 spring 的文档,但请查看您正在使用的版本的文档。看起来它在底层使用 Quartz 来调度任务。