diff --git a/redisson/src/main/java/org/redisson/RedissonExecutorService.java b/redisson/src/main/java/org/redisson/RedissonExecutorService.java index 933240a08..1bf665452 100644 --- a/redisson/src/main/java/org/redisson/RedissonExecutorService.java +++ b/redisson/src/main/java/org/redisson/RedissonExecutorService.java @@ -41,7 +41,6 @@ import java.lang.ref.ReferenceQueue; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.time.LocalDateTime; -import java.time.ZoneId; import java.time.ZonedDateTime; import java.util.*; import java.util.concurrent.*; @@ -975,7 +974,7 @@ public class RedissonExecutorService implements RScheduledExecutorService { check(task); ClassBody classBody = getClassBody(task); byte[] state = encode(task); - ZonedDateTime currentDate = ZonedDateTime.of(LocalDateTime.now(), ZoneId.systemDefault()); + ZonedDateTime currentDate = ZonedDateTime.of(LocalDateTime.now(), cronSchedule.getZoneId()); ZonedDateTime startDate = cronSchedule.getExpression().nextTimeAfter(currentDate); if (startDate == null) { throw new IllegalArgumentException("Wrong cron expression! Unable to calculate start date"); @@ -989,7 +988,7 @@ public class RedissonExecutorService implements RScheduledExecutorService { params.setState(state); params.setStartTime(startTime); params.setCronExpression(cronSchedule.getExpression().getExpr()); - params.setTimezone(ZoneId.systemDefault().toString()); + params.setTimezone(cronSchedule.getZoneId().toString()); params.setExecutorId(executorId); RemotePromise result = (RemotePromise) asyncScheduledServiceAtFixed.schedule(params).toCompletableFuture(); addListener(result); diff --git a/redisson/src/main/java/org/redisson/api/CronSchedule.java b/redisson/src/main/java/org/redisson/api/CronSchedule.java index 2d837ab55..cd89025cc 100644 --- a/redisson/src/main/java/org/redisson/api/CronSchedule.java +++ b/redisson/src/main/java/org/redisson/api/CronSchedule.java @@ -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; + } } +