java cronexpression,如何在Java中将CRON字符串转换为ScheduleExpression?

I got this problem:

I have a text field,

There should be a CRON expression written, and later on saved.

But, I have no idea how to do it...

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");

}

}

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 :))

Please help. Thanks in advance.

解决方案

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]);

}

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

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)

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 :)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值