Scheduling reactions
You can configure when a Reaction should invoke its action for a matching event by configuring its execution time.To schedule a reaction in the future, you need to specify an offset in the reaction definition that you create. When the reaction processor will schedule the reaction to be triggered depends on the following:
- The current time when the event is processed
- The offset configured in the reaction definition
- The event data
When the reaction processor schedules a reaction, it will store the reaction immediately, so you can view and list
scheduled reactions immediately after the event has been processed. A scheduled reaction will have the
status SCHEDULED
. When the calculated trigger time has passed, the reaction will be triggered.
Serialized supports scheduling of reactions for all supported actions (HTTP, Slack, IFTTT, etc.)
Scheduling reactions
To schedule a reaction to trigger a point in time relative to the time the event occurs, you must specify a pattern in
the field offset
. The pattern is defined in the ISO-8601 duration format (PnDTnHnMn.nS) -
see separate section for details. Note that it is possible to use a negative offset
value as well.
The example below shows how you can create a reaction definition that schedules a reaction to trigger 15 minutes after
an event of type OrderShipped
is processed.
ReactionDefinition request = newReactionDefinition("notify-order-shipped") .feed("order") .reactOnEventType("OrderShipped") .action(httpAction(URI.create("https://some-server.com")).build()) .offset("PT15M") .build();reactionClient.createDefinition(request);
Scheduling based on event data
The scheduling of a reaction can also be based on the event data. This is even more powerful than scheduling based on processing time since it gives you as a developer the ability to schedule a reaction based on the timestamps your application provides.
To trigger reactions at a point in time specified in the event data, use the field triggerTimeField
and point out
where the time information is located in the event data. In is also possible to use dot-notation if the field in the
event is part of a nested data structure.
The date or timestamp in the field specified as the triggerTimeField
must be in any of the following formats:
Epoch millis timestamp (number)
'yyyy-MM-dd'T'HH:mm:ssZ'
'yyyy-MM-dd'
'yyyyMMdd'
'yyMMdd'
The example below shows how you can create a reaction definition that schedules the reaction to trigger when the time
for the timestamp in the field estimatedArrival
occur.
ReactionDefinition request = newReactionDefinition("notify-order-arrival") .feed("order") .reactOnEventType("OrderShipped") .action(httpAction(URI.create("https://some-server.com")).build()) .triggerTimeField("estimatedArrival") .build();reactionClient.createDefinition(request);
Relative event scheduling
You can combine the two scheduling methods to create a reaction that is scheduled relative to the time of the event.
This means triggering a reaction at a point in time relative to the timestamp in the triggerTimeField
the event
occurs. To accomplish this, you need to set both the offset
and triggerTimeField
.
Below is an example of a reaction definition that triggers a reaction 15 minutes after the timestamp in the shippedAt
field:
ReactionDefinition request = newReactionDefinition("notify-order-shipped") .feed("order") .reactOnEventType("OrderShipped") .action(httpAction(URI.create("https://some-server.com")).build()) .offset("PT15M") .triggerTimeField("shippedAt") .build();reactionClient.createDefinition(request);
Automatically canceling scheduled reactions
In some situations, you might want to cancel a scheduled reaction when another event occurs. For example, if you have a payment notification reaction that is scheduled to trigger 7 days after an order is placed, you might want to cancel this notification if the order is paid or canceled before this period.
You can configure your reaction definition so that its produces reactions are canceled automatically when another event
occurs. To enable this, you need to specify the cancelOnEventType
field in the reaction definition.
The example below shows how to create a reaction definition that schedules a reaction to trigger 7 days after an order is placed, and automatically cancels the reaction if the order is paid or canceled before this period.
ReactionDefinition request = ReactionDefinition.newReactionDefinition("payment-reminder") .feed("order") .reactOnEventType("OrderPlaced") .action(httpAction(URI.create("https://some-server.com")).build()) .offset("P7D") .triggerTimeField("placedAt") .cancelOnEventType("OrderCanceled", "OrderFullyPaid") .build(); reactionClient.createDefinition(request);
Offset patterns
The offset pattern is defined in the ISO-8601 duration format (PnDTnHnMn.nS). Below is a table of examples of supported patterns:
Pattern | Description |
---|---|
PT20.345S | parses as "20.345 seconds" |
PT15M | parses as "15 minutes" (where a minute is 60 seconds) |
PT10H | parses as "10 hours" (where an hour is 3600 seconds) |
P2D | parses as "2 days" (where a day is 24 hours or 86400 seconds) |
P2DT3H4M | parses as "2 days, 3 hours and 4 minutes" |
PT-6H3M | parses as "-6 hours and +3 minutes" |
PT-6H3M | parses as "-6 hours and +3 minutes" |
-PT6H3M | parses as "-6 hours and -3 minutes" |
-PT-6H+3M | parses as "+6 hours and -3 minutes" |