| 1 | # Events |
| 2 | |
| 3 | EDC provides an eventing system that permits to developers to write extensions that could react to events that are |
| 4 | emitted from the core of the EDC and also emit custom events. |
| 5 | |
| 6 | ## Subscribe to events |
| 7 | The entry point for event listening is the `EventRouter` component, on which an `EventSubscriber` can be registered. |
| 8 | |
| 9 | Actually, there are two ways to register an `EventSubscriber`: |
| 10 | - **async**: every event will be sent to the subscriber in an asynchronous way. Features: |
| 11 | - fast, as the main thread won't be blocked during dispatchment. |
| 12 | - not-reliable, as an eventual subscriber dispatch failure won't get handled. |
| 13 | - to be used for notifications and for send-and-forget event dispatchment. |
| 14 | - **sync**: every event will be sent to the subscriber in a synchronous way. Features: |
| 15 | - slow, as the subscriber will block the main thread until the event is dispatched |
| 16 | - reliable, an eventual exception will be thrown to the caller, and it could make a transactional context fail |
| 17 | - to be used for event persistence and to satisfy the "at-least-one" rule. |
| 18 | |
| 19 | The `EventSubscriber` is typed over the event kind (Class), and it will be invoked only if the type of the event matches |
| 20 | the published one (instanceOf). The base class for all events is `Event`. |
| 21 | |
| 22 | Extension example: |
| 23 | ```java |
| 24 | public class ExampleEventSubscriptionExtension implements ServiceExtension { |
| 25 | @Inject |
| 26 | private EventRouter eventRouter; |
| 27 | |
| 28 | @Override |
| 29 | public void initialize(ServiceExtensionContext context) { |
| 30 | eventRouter.register(Event.class, new ExampleEventSubscriber()); // asynchronous dispatch |
| 31 | eventRouter.registerSync(Event.class, new ExampleEventSubscriber()); // synchronous dispatch |
| 32 | } |
| 33 | } |
| 34 | ``` |
| 35 | |
| 36 | Then the `EventSubscriber` subscription will receive all the events emitted from the EDC and react to them: |
| 37 | |
| 38 | ```java |
| 39 | public class ExampleEventSubscriber implements EventSubscriber<Event> { |
| 40 | |
| 41 | public void on(EventEnvelope<Event> event) { |
| 42 | // react to event |
| 43 | } |
| 44 | |
| 45 | } |
| 46 | ``` |
| 47 | |
| 48 | The `EventEnvelope` is used as a container for the `Event` itself. It will also have additional fields like |
| 49 | |
| 50 | - id: unique identifier of the event (set by default at a random UUID) |
| 51 | - at: creation timestamp |
| 52 | |
| 53 | |
| 54 | To filter events, the classes of the events can be used. There are 5 "Intermediate superclasses" (AssetEvent, |
| 55 | ContractDefinitionEvent, ContractNegotiationEvent, PolicyDefinitionEvent, TransferProcessEvent) of Event. |
| 56 | Thus, in addition to filtering on a specific event, such as TransferProcessCompleted, it is possible to react to a group of events |
| 57 | that generally have to do with Assets, ContractDefinition, ContractNegotiation, PolicyDefinition and TransferProcess. |
| 58 | |
| 59 | In the example below the subscriber is interested in all events by using the type `Event`. In this case a manual filter with |
| 60 | `instanceOf` the event kind is needed: |
| 61 | |
| 62 | ```java |
| 63 | public class ExampleEventSubscriber implements EventSubscriber<Event> { |
| 64 | |
| 65 | public void on(EventEnvelope<Event> event) { |
| 66 | var payload = event.getPayload(); |
| 67 | if (payload instanceof AssetCreated) { |
| 68 | // react only to AssetCreated events |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | } |
| 73 | ``` |
| 74 | |
| 75 | To subscribe a particular type of event, a specific class is needed like in this example: |
| 76 | |
| 77 | ```java |
| 78 | public class ExampleEventSubscriber implements EventSubscriber<AssetCreated> { |
| 79 | |
| 80 | public void on(EventEnvelope<AssetCreated> event) { |
| 81 | // Typed |
| 82 | AssetCreated payload = event.getPayload(); |
| 83 | } |
| 84 | } |
| 85 | ``` |
| 86 | |
| 87 | This works also for group of events using "intermediate superclasses" such as `AssetEvent`, `ContractDefinitionEvent`, etc. |
| 88 | |
| 89 | The dispatcher will take care of calling the right subscribers based on their expressed type. |
| 90 | |
| 91 | ## Emit custom events |
| 92 | It's also possible to create and publish custom events on top of the EDC eventing system. |
| 93 | To define the event, extend the `Event` class. |
| 94 | |
| 95 | > Rule of thumb: events should be named at past tense, as they describe something that's already happened |
| 96 | |
| 97 | ```java |
| 98 | public class SomethingHappened extends Event { |
| 99 | |
| 100 | private String description; |
| 101 | |
| 102 | public String getDescription() { |
| 103 | return description; |
| 104 | } |
| 105 | |
| 106 | private SomethingHappened() { |
| 107 | } |
| 108 | |
| 109 | public static class Builder { |
| 110 | |
| 111 | private SomethingHappened event; |
| 112 | |
| 113 | public static Builder newInstance() { |
| 114 | return new Builder(); |
| 115 | } |
| 116 | |
| 117 | private Builder() { |
| 118 | event = SomethingHappened(); |
| 119 | } |
| 120 | |
| 121 | public Builder description(String description) { |
| 122 | event.description = description; |
| 123 | return this; |
| 124 | } |
| 125 | |
| 126 | public SomethingHappened build() { |
| 127 | Objects.requireNonNull(event.payload.description); |
| 128 | return event; |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | ``` |
| 133 | |
| 134 | All the data regarding an event should be contained in the `Event` class. |
| 135 | |
| 136 | As you may notice, we use the builder pattern to construct objects, as stated in |
| 137 | the [Architecture Principles document](architecture/coding-principles.md). |
| 138 | The extended builder will inherit all the builder method from the superclass. |
| 139 | |
| 140 | Once the event is created, it can be published it through the `EventRouter` component: |
| 141 | |
| 142 | ```java |
| 143 | public class ExampleBusinessLogic { |
| 144 | public void doSomething() { |
| 145 | // some business logic that does something |
| 146 | var event = SomethingHappened.Builder.newInstance() |
| 147 | .description("something interesting happened") |
| 148 | .build(); |
| 149 | |
| 150 | var envelope = EventEnvelope.Builder.newInstance() |
| 151 | .at(clock.millis()) |
| 152 | .payload(event) |
| 153 | .build(); |
| 154 | |
| 155 | eventRouter.publish(envelope); |
| 156 | } |
| 157 | } |
| 158 | ``` |
| 159 | |
| 160 | Please note that the `at` field is a timestamp that every event has, and it's mandatory |
| 161 | (please use the `Clock` service to get the current timestamp). |
| 162 | |
| 163 | ## Serialization / Deserialization |
| 164 | |
| 165 | By default, events must be serializable, because of this, every class that extends `Event` will be serializable to json by default |
| 166 | (through the `TypeManager` service). |
| 167 | The json will contain an additional field called `type` that describes the name of the event class. For example, a serialized `EventEnvelope<SomethingHappened>` |
| 168 | event will look like: |
| 169 | |
| 170 | |
| 171 | ```json |
| 172 | { |
| 173 | "type": "SomethingHappened", |
| 174 | "at": 1654764642188, |
| 175 | "payload": { |
| 176 | "description": "something interesting happened" |
| 177 | } |
| 178 | } |
| 179 | ``` |
| 180 | |
| 181 | To make such an event deserializable by the `TypeManager`, is necessary to register the type: |
| 182 | |
| 183 | ```java |
| 184 | typeManager.registerTypes(new NamedType(SomethingHappened.class, SomethingHappened.class.getSimpleName())); |
| 185 | ``` |
| 186 | |
| 187 | doing so, the event can be deserialized using the `EvenEnvelope` class as type: |
| 188 | |
| 189 | ``` |
| 190 | var deserialized = typeManager.readValue(json, EventEnvelope.class); |
| 191 | // deserialized will have the `EventEnvelope<SomethingHappened>` type at runtime |
| 192 | ``` |
| 193 | (please take a look at the [`EventTest`](../../spi/common/core-spi/src/test/java/org/eclipse/edc/spi/event/EventTest.java) class for a serialization/deserialization example) |