Sending events to your Connected Server
Overview
The general flow of sending custom events to your connected server is as follows:
- You create your own event, or utilize our pre-configured events.
- In Atoti Limits, you trigger sending the event using
the
IServerEventIssuerService::sendServerEvent
method. - In your connected server, the auto-configured
LimitsEventListenerRestService
receives the event and broadcasts it to your connected server. - Any Spring Event Listeners you have registered in your connected server for the event will invoke some action in the connected server.
See Baeldung’s article on Spring Events for more information on how they work.
Implementation
The following steps are used to send events from Atoti Limits to your connected server.
1. Define your event
You must define your event to be picked up within your connected server. Your event must
implement ILimitApplicationEvent
and be imported as a Spring Bean in your configuration. We
recommend that implementations extend ADefaultLimitApplicationEvent
. A sample event looks as follows:
@Data
@EqualsAndHashCode(callSuper = true)
@Component
public class CustomEvent extends ADefaultLimitApplicationEvent {
// The no-args constructor is important for deserialization. Note that the constructor argument may be any object
public CustomEvent() {
super(new ArrayList<>());
}
public CustomEvent(List<CustomEventData> data) {
super(data);
}
@SuppressWarnings("unchecked")
public List<CustomEventData> getCustomEventData() {
return (List<CustomEventData>) getSource();
}
@JsonIgnore // important to avoid serialization of the transient source field
@Override
public Object getSource() {
return super.getSource();
}
/**
* <b>CustomEventData</b>
*
* <p>This may be any java object
*
* @author ActiveViam
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public static class CustomEventData {
protected int id;
protected String name;
protected LocalDateTime creationTime;
protected List<String> tags;
}
}
note
The EventObject
class’s source
field is marked as transient
. To correctly derserialize
your custom event it is important that you override the getter for this field and mark it as
ignored (via @JsonIgnore
).
2. Send your event from Atoti Limits to your connected server
This is done by invoking the Spring service
method IServerEventIssuerService::sendServerEvent
at any point in the Atoti Limits code.
For pre-configured events, Atoti Limits sends the events at the appropriate point in time. For custom events, you need to override the Atoti Limits code via Spring in order to send the events at the required time. For example, if you wanted to send your custom event when an evaluation request has completed, you can add custom evaluation logic to send the event as follows:
void postEvaluation(Collection<Incident> incidents) {
// Create an event using the latest incidents as input
CustomEvent incidentsEvent = createCustomEventFromIncidents(incidents);
// Send the event to your connected server
serverEventIssuerService.sendServerEvent("yourServer", incidentsEvent);
}
3. Listen for your event in your connected server
Add a Spring @EventListener
method in your connected server to listen
for the events sent by Atoti Limits. From here, you can invoke actions in your connected server that are
dependent on events that have been triggered in Atoti Limits.
For pre-configured events, you may implement your own IEventListenerService
, which already has
methods enabled to listen for the events Atoti Limits sends. To override existing logic Atoti Limits triggers, create an implementation that extends DefaultEventListenerService
, for example:
public class MyCustomLimitsEventService extends DefaultEventListenerService {
// a counter on the number of limits that exist
protected final int limitsCounter = new AtomicInteger(0);
/**
* Invoke the default handler and update the counter
*
* @param limitsCreatedEvent
*/
void onLimitsCreated(LimitsCreatedEvent limitsCreatedEvent){
super.onLimitsCreated(limitsCreatedEvent);
limitsCounter.getAndAdd(limitsCreatedEvent.getLimits().size());
}
/**
* Invoke the default handler and update the counter
*
* @param limitsDeletedEvent
*/
void onLimitsDeleted(LimitsDeletedEvent limitsDeletedEvent){
super.onLimitsCreated(limitsDeletedEvent);
limitsCounter.getAndAdd(-1 * limitsDeletedEvent.getLimits().size());
}
/**
* Log the customEvent
*
* @param CustomEvent
*/
@EventListener(CustomEvent.class)
void onLimitsDeleted(CustomEvent customEvent) {
log.info("Custom event received : {}", customEvent);
}
}
Pre-configured events
The following events are bundled in Atoti Limits, which you may listen for in your connected server:
Event | Description |
---|---|
LimitsCreatedEvent |
Sent when limits are created. |
LimitsUpdatedEvent |
Sent when limits are updated. |
LimitsDeletedEvent |
Sent when limits are deleted. |