如何每天下午 2 点运行 TimerTask?
我想每天下午 2 点执行一项工作。我可以使用 java.util.Timer 的哪种方法来安排我的工作?
2 小时后,运行它将停止作业并重新安排到第二天下午 2 点。
I want to execute a job everyday 2PM. Which method of java.util.Timer
I can use to schedule my job?
After 2Hrs, run it will stop the job and reschedule for next day 2PM.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
您可以使用 Timer.schedule(TimerTask task, Date firstTime, long period) 方法,将
firstTime
设置为今天下午 2 点,并将period
设置为24小时:You could use
Timer.schedule(TimerTask task, Date firstTime, long period)
method, settingfirstTime
to 2PM today and the setting theperiod
to 24-hours:我发现执行此操作的最简单方法始终是通过 Windows 中的任务计划程序和 Linux 中的 cron。
然而,对于 Java,请查看 Quartz Scheduler
从他们的网站:
The easiest way I've found of doing this has always been through Task Scheduler in Windows and cron in Linux.
However for Java, take a look at Quartz Scheduler
From their website:
此问题和以下两个问题的大多数答案假设每天的毫秒数始终相同。
如何在 Java 中安排周期性任务?
如何运行特定任务使用 ScheduledExecutorService 确定一天中的特定时间?
但是,由于夏令时和闰秒,这不是真的。
这意味着如果您的服务已存在多年并且计时器的准确性很重要,
固定周期计时器不是您的选择。
因此,我想介绍一个比
java.util.Timer
更灵活的计时器的代码片段。然后,您可以通过以下方式调用上述方法来执行每天下午 2 点的作业。
代码背后的基本思想是重新计算到下一个tick的剩余时间并每次启动一个新的计时器。
Most answers for this question and the following two questions assume that the milliseconds per day is always the same.
How to schedule a periodic task in Java?
How to run certain task every day at a particular time using ScheduledExecutorService?
However, it's not ture due to day lights saving and leap seconds.
This means that if your service is alive for years and the accuracy of the timer matters,
a fixed period timer is not your choice.
So, I'd like to introduce my code snippet for a more flexible timer than the
java.util.Timer
.Then, you can execute your job everyday 2PM by invoking the above method in the following way.
The basic idea behind the code is to recalculate the left time to the next tick and start a new timer every time.
您应该尝试使用scheduleAtFixedRate(这将重复您的任务)。您将需要创建一个 TimerTask 对象,该对象将指定要运行的内容(在 run() 中)以及何时运行(scheduledExecutionTime)。 ScheduleAtFixedRate 还允许您指定第一个执行日期。
You should try using scheduleAtFixedRate (this will repeat your task). You will need to create an TimerTask object which will specify what to run (in run()) and when to run (scheduledExecutionTime). scheduleAtFixedRate also allows you to specify the first date of execution.
你为什么不使用 Spring 的 @Scheduled(cron="0 0 14 * * *") .. 秒、分、小时、日、月、dayOfWeek。酷。您甚至可以在最后一个参数中指定 9-11 表示 9:00 到 11:00,或 MON-FRI。
如果您想在运行时设置时间,您也可以以编程方式调用它,就像大多数 Spring 的情况一样。
看看这个:-
http://docs.spring.io/ spring/docs/current/spring-framework-reference/html/scheduling.html
添加一个示例
,也请使用 @Component 注释来注释包含此示例的类,并请在 Application.java 中提供 @EnableScheduling (类包含主要方法)类到让 spring 知道您正在应用程序中使用taskscheduler。
Why don't you use Spring's @Scheduled(cron="0 0 14 * * *") .. for sec, min, hours, day, month, dayOfWeek. V Cool. You can even specify 9-11 for 9:00 to 11:00, or MON-FRI in last parameter.
You can invoke this programatically too, like is case of most of Spring, in case you want to set the time at runtime.
See this:-
http://docs.spring.io/spring/docs/current/spring-framework-reference/html/scheduling.html
Adding an example
also please annotate the class containing this with @Component annotation and please provide @EnableScheduling in the Application.java (class contains the main method) class to make spring aware that you are using taskscheduler in your applicaiton.
使用 public void Schedule(TimerTask task,Date firstTime,long period)
使任务在第二天再次重复,只需将 period 设置为 86400000 毫秒(即 1 天)
use public void schedule(TimerTask task,Date firstTime,long period)
to make the task repeats again the next day, just set period to 86400000 milliseconds ( which means 1 day )
由于 java.util.Timer:
所以(将此代码添加到您的类中):
Due java.util.Timer:
so (add this code to your class):