Mathieu has posted 10 posts at DZone. View Full User Profile

Event-Driven Development With Mycila Event

02.26.2010
| 5409 views |
  • submit to reddit

I am working on the backend of a high performance clustered system involving JGroups, JMS, and other very cool technologies. To achieve best performance, the application is totally event-driven, by using a very cool library i Open-Sourced: Mycila Event. I needed an intra-process Event Dispatching System capable of handling million events in a few second, while having a totally customizable threading model. In this application, this event system is internally bridged with JMS and JGroups to provide access to both messaging systems from Mycila Event.

I firstly considered using Event-Bus, a well known intra-process event library largely used in Swing applications. But it miss a lot of things we needed: a very good annotation support, a pluggable API, threading model customization and better topic matching strategies. I also really considered using Esper. Esper is a very good CEP engine capable of processing events (aggregate, filter, ...) in addition to being a very good event dispatching system. But what we needed is to depend on a high level Event Dispatching API with a very good annotation support, with the possibility to plug any Event Dispacthing library. We also didn't need the processing features of Esper, but instead we needed a very high performance dispatcher, only working in-memory.

Thus, Mycila-Event stands between Event-Bus and Esper. It brings you a common Event Dispatching API with a very good annotation support, plus provides you with a default implementation with several threading models, and you can create your own ones. Its goal is not to be an event-processor system (event if it actually could be integrated with Esper with the common API we designed). Instead, its goal is to be the best customizable dispatching system.

Here are some of the features Mycila Event brings:

  • High level Event API with Annotation support, compatible with any event dispacther (event-bus, Esper, ...)
  • High performance event dispatcher
  • Customizable dispatching strategies (control how dispatching is done)
  • Customizable threading model (provide your own Executors)
  • Asynchronous mode (publishing/subscribing)
  • Synchronous mode (requesting/replying) with timeout support
  • Mixed mode: requesting with asynchronous replies
  • Topic matchers and composition functions (i.e. subscribe to all topics app/events/system/**)
  • Exception handling is made possible to react upon subscriber errors
  • Automatic generation of publishers
  • Very good integration with Google Guice
  • Possible integration with Spring, Esper, Camel, JMS, ...

Everybody knows the publisher subscriber pattern. But here is a sample how to write a request/reply which goes through the event dispatcher:

static abstract class DU {
@Request(topic = "system/mult")
abstract int mult(int p1, int p2);

@Answers(topics = "system/mult")
int multRequest(int p1, int p2) {
return p1 * p2;
}
}

Dispatcher dispatcher = Dispatchers.synchronousSafe(ErrorHandlers.rethrow());
AnnotationProcessor processor = AnnotationProcessors.create(dispatcher);
Du du = processor.proxy(DU.class);
assertEquals(30, du.mult(5, 6))

Without annotations, you can publish a message like this:

Dispatcher dispatcher = Dispatchers.synchronousUnsafe(ErrorHandlers.rethrow());
TopicMatcher matcher = only("app/events/swing/button").or(matching("app/events/swing/fields/**"));

dispatcher.subscribe(matcher, String.class, new Subscriber<String>() {
public void onEvent(Event<String> event) throws Exception {
System.out.println("Received: " + event.getSource());
}
});

dispatcher.publish(topic("app/events/swing/button"), "Hello !");

You can read the full documentation and download Mycila Event from project page at:

http://code.mycila.com/wiki/MycilaEvent

Math'

Published at DZone with permission of its author, Mathieu Carbou.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)