Event Driven Architecture Example
In this tutorial, we are going to discuss about the Event Driven Architecture Example. Exploring an Event Driven Architecture (EDA) theoretically gives us a good understanding of its principles and operation. But, there’s nothing quite like a hands-on example to cement these concepts. In this tutorial, we’ll explore a simple EDA using Java, covering the creation of event producers, event consumers, the event bus, and event channels.
Creating an Event
Our journey begins with an Event. In Java, we can represent an event as a simple class. This class would typically hold information relevant to the event. For our example, let’s create a basic VehicleEvent
for a traffic management system.
public class VehicleEvent {
private final String vehicleId;
private final String type;
public VehicleEvent(String vehicleId, String type) {
this.vehicleId = vehicleId;
this.type = type;
}
public String getVehicleId() {
return vehicleId;
}
public String getType() {
return type;
}
}
This event represents a vehicle’s action (like ‘entering’ or ‘leaving’ a zone) in our system.
Creating an Event Producer
An Event Producer generates events. In our example, let’s consider sensors at traffic signals as event producers. Every time a vehicle passes, the sensor generates a VehicleEvent
.
public class Sensor implements EventProducer {
private EventBus eventBus;
public Sensor(EventBus eventBus) {
this.eventBus = eventBus;
}
public void vehicleDetected(String vehicleId, String action) {
VehicleEvent event = new VehicleEvent(vehicleId, action);
eventBus.publish(event);
}
}
In the vehicleDetected
method, we generate a new VehicleEvent
and publish it to the EventBus
.
Creating an Event Bus and Event Channels
The EventBus
is the main communication channel in our system. It holds EventChannels
and provides a publish mechanism for producers. An EventChannel
, in turn, holds a list of consumers and a method to add an event to the channel.
public interface EventBus {
void publish(VehicleEvent event);
void addChannel(EventChannel channel);
}
public class SimpleEventBus implements EventBus {
private List<EventChannel> channels;
public SimpleEventBus() {
this.channels = new ArrayList<>();
}
@Override
public void publish(VehicleEvent event) {
for (EventChannel channel : channels) {
channel.add(event);
}
}
@Override
public void addChannel(EventChannel channel) {
channels.add(channel);
}
}
public interface EventChannel {
void add(VehicleEvent event);
void addConsumer(EventConsumer consumer);
}
public class SimpleEventChannel implements EventChannel {
private List<EventConsumer> consumers;
public SimpleEventChannel() {
this.consumers = new ArrayList<>();
}
@Override
public void add(VehicleEvent event) {
for (EventConsumer consumer : consumers) {
consumer.onEvent(event);
}
}
@Override
public void addConsumer(EventConsumer consumer) {
consumers.add(consumer);
}
}
In our simple implementation, when an event is published to the EventBus
, it is added to all the EventChannels
on the bus. Each EventChannel
then passes the event to all its consumers.
Creating an Event Consumer
Finally, we need Event Consumers that will process the events. In our traffic management system, let’s consider traffic lights as consumers. They respond to vehicle events by adjusting their timings.
public class TrafficLight implements EventConsumer {
@Override
public void onEvent(VehicleEvent event) {
if ("entering".equals(event.getType())) {
System.out.println("Vehicle " + event.getVehicleId() + " is entering. Adjusting traffic light timings.");
} else if ("leaving".equals(event.getType())) {
System.out.println("Vehicle " + event.getVehicleId() + " has left. Resetting traffic light timings.");
}
}
}
Our TrafficLight
consumer reacts to VehicleEvent
s by adjusting its timings.
With this, our basic Event Driven Architecture Example in Java is complete. We have created events, producers, consumers, an event bus, and event channels.
Have you grasped the flow of events in our traffic management system? Can you appreciate how this code reflects the principles of the Event Driven Architecture we have been discussing? Let’s move on to explore the issues, special considerations, and performance implications of this pattern.
That’s all about the Event Driven Architecture Example in java. If you have any queries or feedback, please write us email at contact@waytoeasylearn.com. Enjoy learning, Enjoy Microservices..!!