Thursday, February 25, 2010

Event-Driven development with Mycila Event

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

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
  • High performance event dispatcher
  • Customizable dispatching strategies
  • Customizable threading model
  • 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 and easier 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 it on the project page at:

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

Mat'

3 comments:

  1. Hi Mathieu, I'm now investigating Mycila Event for my project. I'd like to know if it works nicely in Web environment? Let's say I have 2 user sessions, what I want is events published from this session can only be received by subscribers in the same session.
    FYI, I'm using Vaadin framework for view layer of my Web app project and I want to use Mycila to help communicating between components in different views.
    ReplyDelete
  2. Hi,

    Yes: we are using it in two big applications:

    - one which is completely event-based, and uses mycila event, jms and jgroups.
    - we are also using it currently in a web plateform for gaming on cell phones: we are using long polling connections (atmoshpere + jersey) and we are communicating through events.

    The think you will have to take care about is to be careful about registration: in webapp, some objects, components or contexts have a lifetime smaller than the Dispatcher. So don't forget to unsubscribe. Otherwise you can use a weak subscribtion if possible for you.

    If you plan to integrate it on your project, I strongly suggest you point to the latest snapshot since we improved the API a lot and some changed occured since 3.x releases. I think it is quite stable and ready to release: we haven't made any change since 5 weeks.
    ReplyDelete
  3. Thanks a lot for your information Mathieu.
    I'm still evaluating Mycila for my project and the progress looks very promising.
    ReplyDelete