如何在Java中安排定期任务?

225

我需要安排一个任务以固定的时间间隔运行。如何在支持长时间间隔的情况下执行此操作(例如每8小时一次)?

我目前正在使用java.util.Timer.scheduleAtFixedRate。这个方法是否支持长时间间隔?

13个回答

325

使用ScheduledExecutorService

 private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
 scheduler.scheduleAtFixedRate(yourRunnable, 8, 8, TimeUnit.HOURS);

1
如果您想在特定时间每天运行此程序,没有很好的方法可以做到这一点,因为TimeUnit适用于initialDelayperiod。当夏令时开始时,每24小时运行一次将被打乱,但是DAYSTimeUnit不允许您指定细粒度的initialDelay。(我认为内部的ScheduledExecutorService实现会将DAYS转换为纳秒)。 - Sam Barnum

49
你应该看一下Quartz,它是一个Java框架,可与EE和SE版本一起使用,并允许定义在特定时间执行的作业。

30

试试这种方法 ->

首先创建一个运行您任务的 TimeTask 类,它应该如下所示:

public class CustomTask extends TimerTask  {

   public CustomTask(){
 
     //Constructor

   }

   public void run() {
       try {
     
         // Your task process

       } catch (Exception ex) {
           System.out.println("error running thread " + ex.getMessage());
       }
    }
}

然后在主类中,您实例化任务并定期运行它,从指定的日期开始:

 public void runTask() {

        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        calendar.set(Calendar.HOUR_OF_DAY, 15);
        calendar.set(Calendar.MINUTE, 40);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);


    
        Timer time = new Timer(); // Instantiate Timer Object

        // Start running the task on Monday at 15:40:00, period is set to 8 hours
        // if you want to run the task immediately, set the 2nd parameter to 0
        time.schedule(new CustomTask(), calendar.getTime(), TimeUnit.HOURS.toMillis(8));
}

6
为了使代码更易读,您可以将调度调用中的最后一个参数更改为TimeUnit.HOURS.toMillis(8)。 - darrenmc
计时器的文档建议使用Executor框架。 - Karan Khanna

14

使用Google Guava AbstractScheduledService,如下所示:

public class ScheduledExecutor extends AbstractScheduledService {

   @Override
   protected void runOneIteration() throws Exception {
      System.out.println("Executing....");
   }

   @Override
   protected Scheduler scheduler() {
        return Scheduler.newFixedRateSchedule(0, 3, TimeUnit.SECONDS);
   }

   @Override
   protected void startUp() {
       System.out.println("StartUp Activity....");
   }


   @Override
   protected void shutDown() {
       System.out.println("Shutdown Activity...");
   }

   public static void main(String[] args) throws InterruptedException {
       ScheduledExecutor se = new ScheduledExecutor();
       se.startAsync();
       Thread.sleep(15000);
       se.stopAsync();
   }
}

如果您有更多类似此类的服务,则在ServiceManager中注册所有服务将是一个好选择,因为所有服务都可以一起启动和停止。阅读这里了解更多关于ServiceManager的信息。


10
如果您想继续使用 java.util.Timer,您可以使用它来安排较大的时间间隔。您只需传入您所需要的周期即可。在此处查看文档(链接)

9
每秒做一些事情
Timer timer = new Timer();
timer.schedule(new TimerTask() {
    @Override
    public void run() {
        //code
    }
}, 0, 1000);

2
计时器的文档建议使用Executor框架代替。 - Karan Khanna

7

这两个类可以共同工作来安排定期任务:

计划任务

import java.util.TimerTask;
import java.util.Date;

// Create a class extending TimerTask
public class ScheduledTask extends TimerTask {
    Date now; 
    public void run() {
        // Write code here that you want to execute periodically.
        now = new Date();                      // initialize date
        System.out.println("Time is :" + now); // Display current time
    }
}

运行计划任务

import java.util.Timer;

public class SchedulerMain {
    public static void main(String args[]) throws InterruptedException {
        Timer time = new Timer();               // Instantiate Timer Object
        ScheduledTask st = new ScheduledTask(); // Instantiate SheduledTask class
        time.schedule(st, 0, 1000);             // Create task repeating every 1 sec
        //for demo only.
        for (int i = 0; i <= 5; i++) {
            System.out.println("Execution in Main Thread...." + i);
            Thread.sleep(2000);
            if (i == 5) {
                System.out.println("Application Terminates");
                System.exit(0);
            }
        }
    }
}

参考 https://www.mkyong.com/java/how-to-run-a-task-periodically-in-java/

这篇文章介绍了如何在Java中定期运行任务。在Java中,可以使用ScheduledExecutorService来创建一个计划任务,并使用不同的方法来安排任务的执行频率,例如每个固定的延迟时间,或者在特定的日期和时间运行。此外,文章还提供了代码示例来帮助您更好地理解如何实现定期任务。

到目前为止最好的解决方案,更加简洁易实现。 - Salvador Vigo

6
如果您的应用程序已经使用了Spring框架,那么您已经内置了调度

5

我使用Spring Framework的功能。(spring-context jar或maven依赖)。

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;


@Component
public class ScheduledTaskRunner {

    @Autowired
    @Qualifier("TempFilesCleanerExecution")
    private ScheduledTask tempDataCleanerExecution;

    @Scheduled(fixedDelay = TempFilesCleanerExecution.INTERVAL_TO_RUN_TMP_CLEAN_MS /* 1000 */)
    public void performCleanTempData() {
        tempDataCleanerExecution.execute();
    }

}

ScheduledTask是我自己的接口,其中包括我自定义的execute方法,我将其称为我的计划任务。


4

您还可以使用JobRunr,这是一个易于使用和开源Java计划程序

要使用JobRunr每8小时安排一个任务,则需要使用以下代码:

BackgroundJob.scheduleRecurrently(Duration.ofHours(8), () -> yourService.methodToRunEvery8Hours());

如果你正在使用Spring Boot、Micronaut或Quarkus,你也可以使用@Recurring注解:

public class YourService {

    @Recurring(interval="PT8H")
    public void methodToRunEvery8Hours() {
        // your business logic
    }

}

JobRunr还内置了一个仪表板,可让您跟踪作业的运行状况。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接