安排在24小时间隔 [英] Schedule at 24hrs interval

查看:65
本文介绍了安排在24小时间隔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道安排代码的最佳方法。我有一个代码可以生成报告并以24小时的间隔向一组人发送邮件。它是基于控制台的java应用程序。我想知道安排它的最佳方法。有时我可能需要将其更改为12小时间隔。但是,应用程序不会在间隔之间执行任何其他任务。

I want to know the best method to schedule a code. I have a code that generates reports and sends mail to a set of people at interval of 24hrs. Its a console based java application. I want to know the best method to schedule that. Sometimes I may need to change that to 12hrs interval. However the application doesn't perform any other task in between the interval.

推荐答案

这里有一些方法,从最简单到最全面:

Here are few approach, from simplest to most comprehensive:


  1. sleep()

TimeUnit.HOURS.sleep(24)

这种方法很简单,做工作并睡24小时。实际上它有点复杂,因为报告生成需要一些时间,所以你必须稍微睡一会儿。以下所有解决方案都可以透明地处理。

This approach is very simple, do the work and sleep for 24 hours. Actually it is a bit more complex because the report generation takes some time, so you have to sleep slightly shorter. All solutions below handle this transparently.

java.util.Timer#scheduleAtFixedRate() - 简单的内置Java解决方案。

java.util.Timer#scheduleAtFixedRate() - simple, built-in Java solution.

@Scheduled 注释 spring @Schedule 的问题class =post-tagtitle =显示问题标记'ejb'rel =tag> ejb - 更复杂但也更强大,例如接受 cron 表达式:

@Scheduled annotation in spring or @Schedule in ejb - more complex but also more powerful, e.g. accepts cron expressions:

@Scheduled(fixedRate=DateUtils.MILLIS_PER_DAY)
public void generateReport() {
  //...
}


  • quartz-scheduler - 完整的Java调度程序,具有群集和故障转移,失火处理,完整 cron support et etc.非常全面:

  • quartz-scheduler - full blown Java scheduler with clustering and fail-over, misfire handling, full cron support, etc. Very comprehensive:

    newTrigger().
      withSchedule(
        simpleSchedule().
          withIntervalInHours(24).
          repeatForever()
        ).build();
    

    newTrigger().
      withSchedule(
        cronSchedule().
          dailyAtHourAndMinute(17, 30).  //17:30
        ).build();
    


  • 这篇关于安排在24小时间隔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆