- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我遇到了这个问题:
我有一个文本框,
应该写一个 CRON 表达式,然后保存。
现在我需要一种方法将 CRON 字符串(这里有一些随机示例:http://www.quartz-scheduler.org/documentation/quartz-2.x/tutorials/crontrigger.html)转换为 java ScheduleExpression(http://docs.oracle.com/javaee/6/api/javax/ejb/ScheduleExpression.html)
但是,我不知道该怎么做......
我有一个基于计时器的执行系统,它只在几天、几周和几个月内运行,但现在我需要实现 CRON 模型,以便可以在特定时间段内运行...
这里有一些代码,只是为了支持我:
@Resource
private TimerService timerService;
@Timeout
public void execute(Timer timer) {
Script s = (Script) timer.getInfo();
execute(s, true);
System.out.println("Timer Service : " + s.getScriptId());
System.out.println("Current Time : " + new Date());
System.out.println("Next Timeout : " + timer.getNextTimeout());
System.out.println("Time Remaining : " + timer.getTimeRemaining());
System.out.println("____________________________________________");
Date today = new Date();
if (s.getTimerSetup().getEndDate() <= today.getTime()) {
stopTimer(s);
}
}
@Override
public void startTimer(Script s) {
if (s.getTimerSetup().getTimerRepeat().equals("0")) {
return;
}
s.setStatus(true);
em.merge(s);
em.flush();
if (s.getTimerSetup().getEndDate() > System.currentTimeMillis()) {
long timeOut = 1L;
String timerRepeat = s.getTimerSetup().getTimerRepeat();
if (timerRepeat.equals("1")) {// day
timeOut = 1000L * 60L * 60L * 24L;
} else if (timerRepeat.equals("2")) {// week
timeOut = 1000L * 60L * 60L * 24L * 7L;
} else if (timerRepeat.equals("3")) {// month
timeOut = 1000L * 60L * 60L * 24L * 30L;
} else {
return; //Here is the part where the cron string is detected
}
long initialTimeOut = s.getTimerSetup().getStartDate() - System.currentTimeMillis();
if (initialTimeOut < 0) {
long initCheck = initialTimeOut * -1;
while (initCheck > timeOut) {
initCheck -= timeOut;
}
initialTimeOut = timeOut - initCheck;
}
Boolean found = false;
if (timerService.getAllTimers().size() == 0) {
System.out.println("Started the timer for the script: " + s.getFileName());
timerService.createTimer(initialTimeOut, timeOut, s);
} else {
for (Timer timer : timerService.getAllTimers()) {
if (((Script) timer.getInfo()).getScriptId() == s.getScriptId()) {
System.out.println("This script's timer was already started!");
found = true;
}
}
if (!found) {
System.out.println("Started the timer for the script: " + s.getFileName());
timerService.createTimer(initialTimeOut, timeOut, s);
found = true;
}
}
} else {
System.out.println("The script's end date has expired");
}
}
我在检测到cron字符串的地方做了标记(在if语句中)我现在需要将字符串转换为 ScheduleExpression。
然后用正常的计时器运行它。 (但那是后来的:))
请帮忙。提前致谢。
最佳答案
我找到了答案,但忘了回答,这是对我有用的代码:
private ScheduleExpression parseCronExpressionToScheduleExpression(String cronExpression) {
if ("never".equals(cronExpression)) {
return null;
}
// parsing it more or less like cron does, at least supporting same fields (+ seconds)
final String[] parts = cronExpression.split(" ");
final ScheduleExpression scheduleExpression;
if (parts.length != 6 && parts.length != 5) {
scheduleExpression = scheduleAliases.get(cronExpression);
if (scheduleExpression == null) {
throw new IllegalArgumentException(cronExpression + " doesn't have 5 or 6 segments as excepted");
}
return scheduleExpression;
} else if (parts.length == 6) { // enriched cron with seconds
return new ScheduleExpression()
.second(parts[0])
.minute(parts[1])
.hour(parts[2])
.dayOfMonth(parts[3])
.month(parts[4])
.dayOfWeek(parts[5]);
}
// cron
return new ScheduleExpression()
.minute(parts[0])
.hour(parts[1])
.dayOfMonth(parts[2])
.month(parts[3])
.dayOfWeek(parts[4]);
}
因此,如果您将 cron 表达式发送给该函数,它会从中生成一个 shedule 表达式,但它不会 100% 对所有 cron 表达式起作用,但对大多数
这是对 cron 表达式中的各个位置有效的方法
* works
- works
, works
/ works
last works (only in the part Day Of Month)
不起作用的是字母,例如 L 和其他字母,至少在我上次检查时是这样。
希望这对下一个人有帮助:)
关于java - 如何在 Java 中将 CRON 字符串转换为 ScheduleExpression?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46344453/
我正在试验 ScheduleExpression 并注意到一个奇怪的行为: 如果我每 30 秒创建一个计时器 scheduleExpression.second("*/30"); 我明白了 [seco
我正在尝试设置 Cloudwatch 计划事件,但我的 cron 表达式似乎无效,但我不知道为什么。 我的 cron 表达式是: cron(5,15,25,35,45,55 * * * *) 我希望它
我希望我的时间表每晚 00:00 运行。 所以在云的形成中我设置了: 计划表达式:“cron(0 0 * * * *)” 但是部署失败,提示 ScheduleExpression 不正确。有什么提示吗
我有一个 EJB 计时器,其计时在 ScheduleExpression 中指定。有没有办法排除日程中的日期列表,即: 不要在周末和其他日期运行? 最佳答案 在所有日子而不是周末执行它很容易:您可以设
我有一个文本字段,用户可以在其中输入 cron 表达式(例如,0 */5 * * * *)。然后,我将其拆分并构建一个 javax.ejb.ScheduleExpression。 现在,javax.e
我有许多任务使用相同的 ScheduleExpression。但我的问题是,执行每一项都需要一个命令。我可以告诉定时服务执行顺序吗? 注意:任务没有持久性 提前致谢, 阿德拉 最佳答案 在这种情况下,
我正在尝试在执行 15 分钟后安排事件: client = boto3.client('events') d = datetime.now() + timedelta(minutes=40) cron
如何根据 CloudFormation 模板中的 Mappings block 下定义的 Region 自动选择 AWS 备份 SheduleExpression。 这意味着如果我在 eu-west-
我遇到了这个问题: 我有一个文本框, 应该写一个 CRON 表达式,然后保存。 现在我需要一种方法将 CRON 字符串(这里有一些随机示例:http://www.quartz-scheduler.or
我必须使用 ScheduleExpression(10 天) 在 cloudwatch 中设置“AWS::Events::Rule”,并编写一些代码来测试它,但我无法将“10 天”更改为 1 分钟或调
我是一名优秀的程序员,十分优秀!