I'm designing the backend for a new Java web app and am trying to decide whether or not to use an Event Bus; specifically the Guava EventBus
.
Most server-side requests will be synchronous: that is, the user is requesting data and needs a response within a few seconds. However, there are also quite a few number of requests that can be asynchronous and that are "fire and forget" on the client-side. So long as they eventually are processed, the client could care less if it takes 2 secondds to process or 2 hours.
For these asynchronous requests, I plan to have the servlet listening at the mapped URL publish the requests to a queue. A consumer will then dequeue each request and route it on to the appropriate handler. This is where the EventBus
may or may not come into play. The business logic for routing the request to the correct handler is pretty complex. Ordinarily a Camel route would be the perfect solution. In my use case, I'm wondering if I can just wire up a bunch of "processors" (event handlers) to the same event bus, and then have each one firing and receiving events to and from each other until an end result is produced.
I would like to say that I already explored using Apache Camel, and that I do believe that Camel's the right tool for the job here. Sadly, for reasons outside the scope of this question, I'm not going to use it. So I started conjuring up Camel-like solutions, which is how I arrived at Guava's EventBus
. But it just might not be a suitable replacement.
I guess I'm looking for the problem classification that the Event Bus
pattern solves, and then I need to determine if that matches my use case here.
EventBus is an open-source library for Android and Java using the publisher/subscriber pattern for loose coupling. EventBus enables central communication to decoupled classes with just a few lines of code – simplifying the code, removing dependencies, and speeding up app development.
The event bus is a special Vue instance that allows the passing of data from one component to another. It emits events in one component, then listens and reacts to the emitted event in another component without the help of its parent component.
There are many patterns devoted to reducing component coupling. An event bus is one such pattern where objects can "subscribe" to receive certain specific "events" from the bus. As an event is "published" to the event bus, it will be propagated to any subscriber which is interested in the event type.
EventBus is a software bus that allows you to register certain events and get notified when something related to that event is published. Events are in the form of classes. First, you register your class to the bus and then define methods that use the Subscribe annotation in order to register to an event.
Here is the problem statement that EventBus
solves in a nutshell:
"I want an easy, centralized way to notify code that's interested in specific types of events when those events occur without any direct coupling between the code that publishes an event and the code that receives it."
When you say "the business logic for routing the request to the correct handler is pretty complex", I suspect that EventBus
may not be what you're looking for, as it routes based on Java object class only (though you can do some fancy stuff with implementing interfaces on your event classes and having subscribers subscribe to specific interfaces).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With