Event handling in a multi-threaded application

I am developing a standalone multi-threaded Java application. I am trying to choose the best event handling solution for my project.

I have 1-3 threads that generate events (for example, comm thread completes the file upload), while other threads can be registered to notify of this event. I want event generation and event listening to be decoupled as much as possible.

What are you offering?

+4
source share
5 answers

Use the event bus .

The event bus can be considered as a replacement for the observer pattern, where in the observer pattern, each component observes the observed directly. In the case of a bus, each component simply subscribes to the event bus and waits for its event notification methods when interesting events have occurred. Thus, the event bus can be thought of as an observer pattern with an additional decoupling layer.

Here's a good presentation on using the event bus in GWT. This should give you a good idea of ​​the benefits (and this is also pretty funny).

EDIT

The first link is mainly an example. It is really not that difficult to implement something like this as you wish, which suits your needs.

+8
source

I would use ExecutorServices to manage thread pools. Thus, when you have an event listener, you can make sure that the event is added to the desired service either using a proxy or with hande encoding. eg.

public void onEventOne(final Type parameter) { executorService.submit(new Runnable() { public void run() { wrappedListener.onEventOne(parameter); } } } 

You can pass this listener shell like this and make sure that the event will be processed using the desired thread pool.

Using a proxy avoids this type of boiler plate code .;)

+1
source

Do you really need a solution in which each thread can register as a listener for each type of event? If so, use an event bus type solution (or centralized monitoring with typed events).

If you do not need this flexibility, you may need to set up a manager-manager when the manager receives event notifications (for example: "I have finished my work") and can start workers if necessary.

+1
source

Using an event bus is definitely the right choice. There are various solutions. You can also check out MBassador https://github.com/bennidi/mbassador .

It is annotation-driven, very lightweight and uses weak references (therefore, it is easy to integrate them in environments where the life cycle of objects is managed using a framework such as spring or guice or somethign). It provides a mechanism for filtering objects and synchronous or asynchronous processing of sending / processing messages. And it is very fast!

Google Guava also has an event bus, but uses strong links that can be sick if you do not have full control over the object's life cycle (for example, the spring environment)

EDIT: I created a comparison of performance and features to select the available event bus implementations, including Guava, MBassador, and some others. The results are quite interesting. Check it out here http://codeblock.engio.net/?p=37

+1
source

use command design template for decoupling

0
source

All Articles