如何模拟'号在Java中使用CronTriggers发生'或'重复计数'? [英] How to simulate 'No. of occurrences' or 'Repeat Count' with CronTriggers in Java?

查看:112
本文介绍了如何模拟'号在Java中使用CronTriggers发生'或'重复计数'?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Quartz Scheduler(由于项目限制而导致版本为1.8.3),并且我分配了为特定于我的项目的作业创建类似MS Outlook的调度程序的任务。一切似乎工作正常,但我对CronTriggers有一个非常大的问题(这个问题也存在于Quartz的2.1版本中):

I am using the Quartz Scheduler (version 1.8.3 due to project constraints) and I as assigned the task of creating an "MS Outlook-like" scheduler for Jobs specific to my project. Everything seems fine to work fine but I have a really huge problem with CronTriggers (this problem also exists in version 2.1 of Quartz):

我使用CronTriggers进行重复模式每周,每周和每月。除了重复发生模式,我还提供了否的选项。发生的'。这已成为我生命中的祸根! CronTrigger不像SimpleTriggers那样为'repeatCount'提供选项(bug: https://jira.terracotta.org/jira/browse/QTZ-242?page=com.atlassian.jira.plugin.system.issuetabpanels%3Achangehistory-tabpanel )。显然这可以在版本2.2中修复,但我不能等待那么久,我也不相信我的问题是独特的!

I am using CronTriggers for recurrence patterns of DAILY, WEEKLY and MONTHLY. In addition to the recurrence pattern, I also provide an option for 'No. of occurrences'. This has become the bane of my life! CronTrigger does not provide an option for 'repeatCount' like SimpleTriggers do (bug: https://jira.terracotta.org/jira/browse/QTZ-242?page=com.atlassian.jira.plugin.system.issuetabpanels%3Achangehistory-tabpanel). Apparently this may be fixed in version 2.2 but I cannot wait that long nor do I believe my problem is unique!

我认为值得调查的一些选项:

A few options that I deemed worthy of investigation:


  1. 计算CronTrigger的'EndTime',但是使用我自己的逻辑 - 这不能涵盖所有可能的情况,并且只是近似的近似值即使是简单的情况。

  1. Calculate the 'EndTime' for the CronTrigger but using my own logic - this fails to cover all possible cases and is only approximate at best even for simple cases.

使用TriggerListener或JobListener来跟踪no。由于我只需要在'N'次迭代后停止工作,并且我从Job实例到Trigger的1:1映射,因此工作的迭代。这似乎在任何想象中都不可行和/或有效。

Use a TriggerListener or JobListener to keep track of no. of iterations of the job since I just need the job to stop after 'N' iterations and I have a 1:1 mapping from Job instance to Trigger. This does not seem very feasible and/or efficient by any stretch of the imagination.

你们中的任何人都可以使用CronTriggers选项'否对于如何解决这个难题,请提供一些见解?

Could any of you who have used CronTriggers with the option of 'No. of occurrences' please give some insights on how to solve this conundrum?

推荐答案

似乎Quartz已经实现了一些可以帮助的东西: TriggerUtils.computeEndTimeToAllowParticularNumberOfFirings

It seems that Quartz have implemented something that can help: TriggerUtils.computeEndTimeToAllowParticularNumberOfFirings.

我还没有测试过,但这是我现在写的代码:

I haven't tested it yet, but this is the code I have wrote for now:

CronTrigger trigger = newTrigger()
    .withSchedule(cronSchedule(cronExpression))
    .build();
Date endDate = TriggerUtils.computeEndTimeToAllowParticularNumberOfFirings((OperableTrigger) trigger,
              new BaseCalendar(Calendar.getInstance().getTimeZone()), 10);
trigger = trigger.getTriggerBuilder().endAt(endDate).build();

如果这不起作用,那么就像所说的这里这里,你不能设置重复计数,你应该使用TriggerListener。

If this won't work, then as said here and here, you can't set a repeat count, and you should use TriggerListener.

无论如何,版本2.2没有此功能。

In any case, version 2.2 doesn't have this feature.

更新

我'测试了它,它的工作原理。

I've tested it, and it works.

这篇关于如何模拟'号在Java中使用CronTriggers发生'或'重复计数'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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