> ## Documentation Index
> Fetch the complete documentation index at: https://docs.activeviam.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Send events to your connected server

> How to send custom or pre-configured events from Atoti Limits to a connected Atoti Server instance using IServerEventIssuerService, and how to listen for events in the connected server using Spring event listeners

## Overview

The general flow of sending custom events to your connected server is as follows:

1. You create your own event, or utilize our pre-configured events.
2. In Atoti Limits, you trigger sending the event using
   the `IServerEventIssuerService::sendServerEvent`
   method.
3. In your connected server, the auto-configured `LimitsEventListenerRestService` receives the event and
   broadcasts it to your connected server.
4. Any
   Spring [Event Listeners](https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/event/EventListener.)
   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](https://www.baeldung.com/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:

```java theme={"languages":{"custom":["/engine/python-sdk/0.9/languages/pycon.tmLanguage.json"]}}
@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(final 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`).
</Note>

### 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](../custom-evaluation/custom-evaluation-service)
to send the event as follows:

```java theme={"languages":{"custom":["/engine/python-sdk/0.9/languages/pycon.tmLanguage.json"]}}
void postEvaluation(Collection<Incident> incidents) {
  // Create an event using the latest incidents as input
  final 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:

```java theme={"languages":{"custom":["/engine/python-sdk/0.9/languages/pycon.tmLanguage.json"]}}
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(final 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:

<table><thead><tr><th>Event</th><th>Description</th></tr></thead><tbody><tr><td><code>LimitsCreatedEvent</code></td><td>Sent when limits are created.</td></tr><tr><td><code>LimitsUpdatedEvent</code></td><td>Sent when limits are updated.</td></tr><tr><td><code>LimitsDeletedEvent</code></td><td>Sent when limits are deleted.</td></tr><tr><td><code>LimitsKpiUpdatedEvent</code></td><td>Sent when KPIs are refreshed.</td></tr></tbody></table>
