|
|
|
@ -17,6 +17,8 @@ package org.redisson.api;
|
|
|
|
|
|
|
|
|
|
import org.redisson.executor.CronExpression;
|
|
|
|
|
|
|
|
|
|
import java.time.ZoneId;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Cron expression object used in {@link RScheduledExecutorService}.
|
|
|
|
|
* Fully compatible with quartz cron expression.
|
|
|
|
@ -28,11 +30,13 @@ import org.redisson.executor.CronExpression;
|
|
|
|
|
*/
|
|
|
|
|
public final class CronSchedule {
|
|
|
|
|
|
|
|
|
|
private CronExpression expression;
|
|
|
|
|
|
|
|
|
|
CronSchedule(CronExpression expression) {
|
|
|
|
|
private final CronExpression expression;
|
|
|
|
|
private final ZoneId zoneId;
|
|
|
|
|
|
|
|
|
|
CronSchedule(CronExpression expression, ZoneId zoneId) {
|
|
|
|
|
super();
|
|
|
|
|
this.expression = expression;
|
|
|
|
|
this.zoneId = zoneId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -44,7 +48,20 @@ public final class CronSchedule {
|
|
|
|
|
* wrapping a ParseException if the expression is invalid
|
|
|
|
|
*/
|
|
|
|
|
public static CronSchedule of(String expression) {
|
|
|
|
|
return new CronSchedule(new CronExpression(expression));
|
|
|
|
|
return new CronSchedule(new CronExpression(expression), ZoneId.systemDefault());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates cron expression object with defined expression string and time-zone ID
|
|
|
|
|
*
|
|
|
|
|
* @param expression of cron
|
|
|
|
|
* @param zoneId id of zone
|
|
|
|
|
* @return object
|
|
|
|
|
* @throws IllegalArgumentException
|
|
|
|
|
* wrapping a ParseException if the expression is invalid
|
|
|
|
|
*/
|
|
|
|
|
public static CronSchedule of(String expression, ZoneId zoneId) {
|
|
|
|
|
return new CronSchedule(new CronExpression(expression), zoneId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -62,6 +79,22 @@ public final class CronSchedule {
|
|
|
|
|
return of(expression);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates cron expression which schedule task execution
|
|
|
|
|
* every day at the given time in specified time-zone ID
|
|
|
|
|
*
|
|
|
|
|
* @param hour of schedule
|
|
|
|
|
* @param minute of schedule
|
|
|
|
|
* @param zoneId id of zone
|
|
|
|
|
* @return object
|
|
|
|
|
* @throws IllegalArgumentException
|
|
|
|
|
* wrapping a ParseException if the expression is invalid
|
|
|
|
|
*/
|
|
|
|
|
public static CronSchedule dailyAtHourAndMinute(int hour, int minute, ZoneId zoneId) {
|
|
|
|
|
String expression = String.format("0 %d %d ? * *", minute, hour);
|
|
|
|
|
return of(expression, zoneId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates cron expression which schedule task execution
|
|
|
|
|
* every given days of the week at the given time.
|
|
|
|
@ -85,6 +118,30 @@ public final class CronSchedule {
|
|
|
|
|
return of(expression);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates cron expression which schedule task execution
|
|
|
|
|
* every given days of the week at the given time in specified time-zone ID.
|
|
|
|
|
* Use Calendar object constants to define day.
|
|
|
|
|
*
|
|
|
|
|
* @param hour of schedule
|
|
|
|
|
* @param minute of schedule
|
|
|
|
|
* @param zoneId id of zone
|
|
|
|
|
* @param daysOfWeek - Calendar object constants
|
|
|
|
|
* @return object
|
|
|
|
|
*/
|
|
|
|
|
public static CronSchedule weeklyOnDayAndHourAndMinute(int hour, int minute, ZoneId zoneId, Integer... daysOfWeek) {
|
|
|
|
|
if (daysOfWeek == null || daysOfWeek.length == 0) {
|
|
|
|
|
throw new IllegalArgumentException("You must specify at least one day of week.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String expression = String.format("0 %d %d ? * %d", minute, hour, daysOfWeek[0]);
|
|
|
|
|
for (int i = 1; i < daysOfWeek.length; i++) {
|
|
|
|
|
expression = expression + "," + daysOfWeek[i];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return of(expression, zoneId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates cron expression which schedule task execution
|
|
|
|
|
* every given day of the month at the given time
|
|
|
|
@ -98,9 +155,28 @@ public final class CronSchedule {
|
|
|
|
|
String expression = String.format("0 %d %d %d * ?", minute, hour, dayOfMonth);
|
|
|
|
|
return of(expression);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates cron expression which schedule task execution
|
|
|
|
|
* every given day of the month at the given time in specified time-zone ID.
|
|
|
|
|
*
|
|
|
|
|
* @param hour of schedule
|
|
|
|
|
* @param minute of schedule
|
|
|
|
|
* @param dayOfMonth of schedule
|
|
|
|
|
* @param zoneId id of zone
|
|
|
|
|
* @return object
|
|
|
|
|
*/
|
|
|
|
|
public static CronSchedule monthlyOnDayAndHourAndMinute(int dayOfMonth, int hour, int minute, ZoneId zoneId) {
|
|
|
|
|
String expression = String.format("0 %d %d %d * ?", minute, hour, dayOfMonth);
|
|
|
|
|
return of(expression, zoneId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public CronExpression getExpression() {
|
|
|
|
|
return expression;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public ZoneId getZoneId() {
|
|
|
|
|
return zoneId;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|