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.
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).