Time Events and Scheduler Functions : Scheduling Events Using Scheduler Functions (Requires Cache OM)

Scheduling Events Using Scheduler Functions (Requires Cache OM)
You schedule time events as explained in Overview of Time Events, and you schedule simple events using scheduler functions. Events scheduled using these functions are sent to their default destination.
You might use the event scheduler functions instead of a time event in the following cases:
If you need to send any event on a schedule, you must use the scheduler. If you schedule a memory-only simple event the schedule is persisted. If you schedule a memory only time event (using the time event's function, not the scheduler functions) then the schedule is not persisted.
If you need to create many, perhaps thousands, of schedules at one time, performance is better using the event scheduler functions.
Because schedule management and event sending can be handled by a cache server, fewer resources in the inference agents are required compared to time events. Additionally, with backing store enabled the schedules are only loaded into memory a batch at a time, reducing the total memory requirement compared to time events.
 
Task A Create a Scheduler
First you create a scheduler using the function Cluster.createScheduler(). You can do this, for example, in a startup rule function, or other rule function or rule:

 
void createScheduler(String schedulerName, long pollInterval, long refreshAhead)

 
schedulerName   A unique ID for the scheduler you are creating.
pollInterval   BusinessEvents checks the scheduler cache every pollInterval milliseconds, for scheduler work items whose scheduledTime falls in the current interval.
refreshAhead  Time in milliseconds (into the future) used to pre-load the scheduled events from the backing store. Ignored if no backing store is used.
For example:

 
Cluster.createScheduler("myScheduler", 1000, 5000);

 
Task B Schedule the Event to be Sent
You use the function Cluster.scheduleEvent() to schedule an event to be sent at a certain time, using a scheduler you created earlier. You can do this in a rule or rule function as needed:

 
void scheduleEvent(String schedulerName, String workKey, SimpleEvent evt, long scheduledTime)

 
Where:
schedulerName is the unique ID for the scheduler you created at an earlier time.
workKey is a unique key that identifies the work item (that is, the scheduled task). This key can be used to identify the work item later, for example to cancel it.
evt is the simple event to be scheduled.
scheduledTime is used to specify the time the event is sent to its default destination. The value is interpreted as an absolute time. To schedule a time relative to the present, use the System.currentTimeMillis() function, as shown below.
For example:

 
Cluster.scheduleEvent("MyScheduler", myworkKey, Event.createEvent("xslt:// details omitted "), System.currentTimeMillis() + 5000)