问题描述
我遇到了这个问题:
我有一个文本字段,
应该有一个CRON表达式
There should be a CRON expression written, and later on saved.
现在,我需要一种方法来转换CRON字符串(以下是一些随机示例:)转换为Java ScheduleExpression()
Now I need a method to convert the CRON string (here are some random examples: http://www.quartz-scheduler.org/documentation/quartz-2.x/tutorials/crontrigger.html) to java ScheduleExpression (http://docs.oracle.com/javaee/6/api/javax/ejb/ScheduleExpression.html)
但是,我不知道该怎么做...
But, I have no idea how to do it...
我有一个基于计时器的执行系统,该系统仅在几天,几周和几个月内运行,但是现在我需要实现CRON模型,以便执行可以在特定时间段内运行...
I have a timer based execution system, that run only on days, weeks and months, but now I need to implement the CRON models, so that the executions can be run on a specific period of time...
这里有一些代码,只是为了备份我:
here is a little code, just to back me up:
@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。
I marked the place where the cron string is detected (in the if statement)And I need now to transform the string to a ScheduleExpression.
然后用普通的计时器运行它。 (但这会在以后出现:))
And after that to run it with the normal timers. (but that comes later :))
请帮助。
推荐答案
我找到了答案,但忘了回答,这是对我有用的代码:
I found the answer, but forgot to answer it, here is the code that worked for me:
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表达式发送到函数,它将产生一个摆脱掉表达式,但不是在所有cron表达式上都能100%起作用,但是在大多数cron表达式上都起作用
So, if you send the cron expression to the function, it will make a shedule expression out of it, but it does not work 100% on all cron expressions but on most
这是对cron表达式中各个位置起作用的
Here is what worked for the individual positions in the cron expression
* works
- works
, works
/ works
last works (only in the part Day Of Month)
不起作用的是字母,例如L,而其他字母,至少在我上次检查时没有。
What does not work are the letters, such as L, and the others, at least not when I last checked.
希望这对下一个家伙有帮助:)
Hope this will help the next guy :)
这篇关于如何在Java中将CRON字符串转换为ScheduleExpression?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!