Christopher Anabo
Christopher Anabo
Senior Tech Lead
Christopher Anabo

Notes

Inprocess Event Listener using Eventbux

Inprocess Event Listener using Eventbux

Yes, you can implement event-driven architecture with other frameworks as well. Here are a few examples using different frameworks:

### 1. **Google Guava EventBus**

Google Guava provides an `EventBus` for event-driven programming. Here is how you can use it:

1. **Add Dependency**:

   ```xml
  
       com.google.guava
       guava
       31.0.1-jre
  

   ```

2. **Define an Event Class**:

   ```java
   public class CustomObjectChangeEvent {
       private final String message;

       public CustomObjectChangeEvent(String message) {
           this.message = message;
       }

       public String getMessage() {
           return message;
       }
   }
   ```

3. **Set up EventBus**:

   ```java
   import com.google.common.eventbus.EventBus;
   import com.google.common.eventbus.Subscribe;

   public class EventBusExample {

       public static void main(String[] args) {
           EventBus eventBus = new EventBus();

           CustomObjectChangeListener listener = new CustomObjectChangeListener();
           eventBus.register(listener);

           CustomObjectChangePublisher publisher = new CustomObjectChangePublisher(eventBus);
           publisher.publishEvent("Change 1");
           publisher.publishEvent("Change 2");
           publisher.publishEvent("Change 3");
       }

       public static class CustomObjectChangeListener {
           @Subscribe
           public void handleCustomObjectChangeEvent(CustomObjectChangeEvent event) {
               System.out.println("Received event: " + event.getMessage());
           }
       }

       public static class CustomObjectChangePublisher {
           private final EventBus eventBus;

           public CustomObjectChangePublisher(EventBus eventBus) {
               this.eventBus = eventBus;
           }

           public void publishEvent(String message) {
               eventBus.post(new CustomObjectChangeEvent(message));
           }
       }
   }
   ```

### 2. **Akka**

Akka is a toolkit and runtime for building highly concurrent, distributed, and resilient message-driven applications on the JVM.

1. **Add Dependency**:

   ```xml
  
       com.typesafe.akka
       akka-actor_2.13
       2.6.17
  

   ```

2. **Define Actor and Messages**:

   ```java
   import akka.actor.AbstractActor;
   import akka.actor.ActorRef;
   import akka.actor.ActorSystem;
   import akka.actor.Props;

   public class AkkaExample {

       public static void main(String[] args) {
           ActorSystem system = ActorSystem.create("event-system");
           ActorRef listener = system.actorOf(Props.create(CustomObjectChangeListener.class), "listener");
           ActorRef publisher = system.actorOf(Props.create(CustomObjectChangePublisher.class, listener), "publisher");

           publisher.tell(new CustomObjectChangeEvent("Change 1"), ActorRef.noSender());
           publisher.tell(new CustomObjectChangeEvent("Change 2"), ActorRef.noSender());
           publisher.tell(new CustomObjectChangeEvent("Change 3"), ActorRef.noSender());

           system.terminate();
       }

       public static class CustomObjectChangeEvent {
           private final String message;

           public CustomObjectChangeEvent(String message) {
               this.message = message;
           }

           public String getMessage() {
               return message;
           }
       }

       public static class CustomObjectChangeListener extends AbstractActor {
           @Override
           public Receive createReceive() {
               return receiveBuilder()
                       .match(CustomObjectChangeEvent.class, event -> {
                           System.out.println("Received event: " + event.getMessage());
                       })
                       .build();
           }
       }

       public static class CustomObjectChangePublisher extends AbstractActor {
           private final ActorRef listener;

           public CustomObjectChangePublisher(ActorRef listener) {
               this.listener = listener;
           }

           @Override
           public Receive createReceive() {
               return receiveBuilder()
                       .match(CustomObjectChangeEvent.class, event -> {
                           listener.tell(event, getSelf());
                       })
                       .build();
           }
       }
   }
   ```

### 3. **Vert.x EventBus**

Vert.x provides an event bus for event-driven programming. Here is how you can use it:

1. **Add Dependency**:

   ```xml
  
       io.vertx
       vertx-core
       4.2.1
  

   ```

2. **Set up Vert.x**:

   ```java
   import io.vertx.core.AbstractVerticle;
   import io.vertx.core.Vertx;
   import io.vertx.core.eventbus.EventBus;
   import io.vertx.core.eventbus.Message;

   public class VertxExample {

       public static void main(String[] args) {
           Vertx vertx = Vertx.vertx();
           EventBus eventBus = vertx.eventBus();

           vertx.deployVerticle(new CustomObjectChangeListener());
           vertx.deployVerticle(new CustomObjectChangePublisher());

           eventBus.publish("custom.object.change", "Change 1");
           eventBus.publish("custom.object.change", "Change 2");
           eventBus.publish("custom.object.change", "Change 3");
       }

       public static class CustomObjectChangeListener extends AbstractVerticle {
           @Override
           public void start() {
               vertx.eventBus().consumer("custom.object.change", this::handleCustomObjectChangeEvent);
           }

           private void handleCustomObjectChangeEvent(Message message) {
               System.out.println("Received event: " + message.body());
           }
       }

       public static class CustomObjectChangePublisher extends AbstractVerticle {
           @Override
           public void start() {
               EventBus eventBus = vertx.eventBus();
               eventBus.publish("custom.object.change", "Change 1");
               eventBus.publish("custom.object.change", "Change 2");
               eventBus.publish("custom.object.change", "Change 3");
           }
       }
   }
   ```

These examples demonstrate how to implement event-driven architectures using different frameworks in Java. Each framework has its own strengths and use cases, so you can choose the one that best fits your requirements.