Interface ZonedTrigger

All Superinterfaces:
Trigger
All Known Implementing Classes:
CronTrigger

public interface ZonedTrigger extends Trigger
Triggers allow application developers to plug in rules for when and how often a task should run. The trigger can be as simple as a single, absolute date-time or can include Jakarta™ EE business calendar logic. A trigger implementation is created by the application developer (or may be supplied to the application externally) and is registered with a task when it is submitted to a ManagedScheduledExecutorService using any of the schedule methods. Each method will run with unspecified context. The methods can be made contextual through creating contextual proxy objects using ContextService.

Each trigger instance will be invoked within the same process in which it was registered.

Example:

 /**
  * A trigger that runs on the hour, Mon-Fri from 8am-8pm Central US time.
  */
  public class HourlyDuringBusinessHoursTrigger implements ZonedTrigger {
      static final ZoneId ZONE = ZoneId.of("America/Chicago");

      public ZoneId getZoneId() {
          return ZONE;
      }

      public ZonedDateTime getNextRunTime(LastExecution lastExec, ZonedDateTime taskScheduledTime) {
          ZonedDateTime prevTime = lastExec == null ? taskScheduledTime : lastExec.getRunEnd(ZONE);
          ZonedDateTime nextTime = prevTime.truncatedTo(ChronoUnit.HOURS).plusHours(1);
          DayOfWeek day = nextTime.getDayOfWeek();
          if (day.equals(DayOfWeek.SATURDAY) || day.equals(DayOfWeek.SUNDAY)) {
              nextTime = nextTime.truncatedTo(ChronoUnit.DAYS).plusDays(1).withHour(8);
          } else { // Mon-Fri 8am-8pm
              int hour = nextTime.getHour();
              if (hour< 8)
                  nextTime = nextTime.plusHours(8 - hour);
              else if (hour> 20)
                  nextTime = nextTime.truncatedTo(ChronoUnit.DAYS)
                                     .plusDays(day.equals(DayOfWeek.FRIDAY) ? 3 : 1)
                                     .withHour(8);
          }
          return nextTime;
      }
  }
 

Since:
3.0