Java中是否有类似Cron的ScheduledExecutorService实现?

时间:2021-04-27 02:12:22

The ScheduledExecutorService in Java is pretty handy for repeating tasks with either fixed intervals or fixed delay. I was wondering if there is an something like the existing ScheduledExecutorService that lets you specify a time of day to schedule the task at, rather than an interval i.e. "I want this task to fire at 10am each day".

Java中的ScheduledExecutorService对于以固定间隔或固定延迟重复任务非常方便。我想知道是否有像现有的ScheduledExecutorService这样的东西可以指定一天中安排任务的时间,而不是一个间隔,即“我希望这个任务每天上午10点开火”。

I know you can achieve this with Quartz, but I'd rather not use that library if possible (it's a great library but I'd rather not have the dependency for a few reasons).

我知道你可以用Quartz实现这个目标,但是如果可能的话我宁愿不使用那个库(它是一个很棒的库,但我宁愿没有依赖性,原因有几个)。

5 个解决方案

#1


You can use the Timer class. Specifically, scheduleAtFixedRate(TimerTask task, Date firstTime, long period). Where you can set a task to start at 10am on a particular day and repeat every 24 hours.

您可以使用Timer类。具体来说,scheduleAtFixedRate(TimerTask任务,Date firstTime,long period)。您可以将任务设置为在特定日期的上午10点开始,并且每24小时重复一次。

#2


A bit more searching has turned up CronExecutorService in HA-JDBC. Interestingly, it has a dependency on Quartz for its CronExpression class, but that's it. That's not too bad.

在HA-JDBC中,更多的搜索已经发现了CronExecutorService。有趣的是,它对CronExpression类依赖于Quartz,但就是这样。那不算太糟糕。

Update: I've fixed the broken links to point at new versions, but I don't know if that is the only dependency any more

更新:我已修复损坏的链接以指向新版本,但我不知道这是否是唯一的依赖项

#3


When you use scheduleAtFixedRate you provide a delay. So the delay can be the difference to 10 am and period is 24 hours. This could drift a bit, even with a timer so what you can do is schedule a task which adds itself to the ScheduledExecutorService with an appropriate delay each time.

当您使用scheduleAtFixedRate时,您提供延迟。所以延迟可以是上午10点的差异,周期是24小时。即使使用计时器,这可能会有点漂移,因此您可以做的是安排一个任务,每次都会以适当的延迟将自己添加到ScheduledExecutorService。

#5


ThreadPoolTaskScheduler, can be used whenever external thread management is not a requirement. Internally, it delegates to a ScheduledExecutorService instance. ThreadPoolTaskScheduler actually implements Spring’s TaskExecutor interface as well, so that a single instance can be used for asynchronous execution as soon as possible as well as scheduled, and potentially recurring, executions.

无需外部线程管理时,可以使用ThreadPoolTask​​Scheduler。在内部,它委托给ScheduledExecutorService实例。 ThreadPoolTask​​Scheduler实际上也实现了Spring的TaskExecutor接口,因此单个实​​例可以尽快用于异步执行以及计划和可能重复执行。

Where as CronTrigger() takes in cronExpression http://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/scheduling/support/CronSequenceGenerator.html

CronTrigger()接受cronExpression的地方http://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/scheduling/support/CronSequenceGenerator.html

For more information on this solution refer Spring docs: https://docs.spring.io/spring/docs/current/spring-framework-reference/html/scheduling.html

有关此解决方案的更多信息,请参阅Spring文档:https://docs.spring.io/spring/docs/current/spring-framework-reference/html/scheduling.html

import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.support.CronTrigger;
import java.util.Date;

public class CronTriggerSpringTest{
public static void main(String args[]){
    String cronExpression = "0/5 * * * * *";
    ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
    scheduler.initialize();
    scheduler.schedule(new Runnable() {
        @Override
        public void run() {
            System.out.println("Hello Date:"+new Date());
        }
    }, new CronTrigger(cronExpression));
}
}

#1


You can use the Timer class. Specifically, scheduleAtFixedRate(TimerTask task, Date firstTime, long period). Where you can set a task to start at 10am on a particular day and repeat every 24 hours.

您可以使用Timer类。具体来说,scheduleAtFixedRate(TimerTask任务,Date firstTime,long period)。您可以将任务设置为在特定日期的上午10点开始,并且每24小时重复一次。

#2


A bit more searching has turned up CronExecutorService in HA-JDBC. Interestingly, it has a dependency on Quartz for its CronExpression class, but that's it. That's not too bad.

在HA-JDBC中,更多的搜索已经发现了CronExecutorService。有趣的是,它对CronExpression类依赖于Quartz,但就是这样。那不算太糟糕。

Update: I've fixed the broken links to point at new versions, but I don't know if that is the only dependency any more

更新:我已修复损坏的链接以指向新版本,但我不知道这是否是唯一的依赖项

#3


When you use scheduleAtFixedRate you provide a delay. So the delay can be the difference to 10 am and period is 24 hours. This could drift a bit, even with a timer so what you can do is schedule a task which adds itself to the ScheduledExecutorService with an appropriate delay each time.

当您使用scheduleAtFixedRate时,您提供延迟。所以延迟可以是上午10点的差异,周期是24小时。即使使用计时器,这可能会有点漂移,因此您可以做的是安排一个任务,每次都会以适当的延迟将自己添加到ScheduledExecutorService。

#4


#5


ThreadPoolTaskScheduler, can be used whenever external thread management is not a requirement. Internally, it delegates to a ScheduledExecutorService instance. ThreadPoolTaskScheduler actually implements Spring’s TaskExecutor interface as well, so that a single instance can be used for asynchronous execution as soon as possible as well as scheduled, and potentially recurring, executions.

无需外部线程管理时,可以使用ThreadPoolTask​​Scheduler。在内部,它委托给ScheduledExecutorService实例。 ThreadPoolTask​​Scheduler实际上也实现了Spring的TaskExecutor接口,因此单个实​​例可以尽快用于异步执行以及计划和可能重复执行。

Where as CronTrigger() takes in cronExpression http://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/scheduling/support/CronSequenceGenerator.html

CronTrigger()接受cronExpression的地方http://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/scheduling/support/CronSequenceGenerator.html

For more information on this solution refer Spring docs: https://docs.spring.io/spring/docs/current/spring-framework-reference/html/scheduling.html

有关此解决方案的更多信息,请参阅Spring文档:https://docs.spring.io/spring/docs/current/spring-framework-reference/html/scheduling.html

import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.support.CronTrigger;
import java.util.Date;

public class CronTriggerSpringTest{
public static void main(String args[]){
    String cronExpression = "0/5 * * * * *";
    ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
    scheduler.initialize();
    scheduler.schedule(new Runnable() {
        @Override
        public void run() {
            System.out.println("Hello Date:"+new Date());
        }
    }, new CronTrigger(cronExpression));
}
}