MessageBus/README.md

85 lines
4.4 KiB
Markdown
Raw Normal View History

2012-10-23 10:31:12 +02:00
Mbassador
2012-10-23 09:27:32 +02:00
=========
2012-10-23 10:31:12 +02:00
Mbassador is a very light-weight message bus implementation following the publish subscribe pattern. It is designed
for ease of use and aims to be resource efficient and very fast. It was inspired by google guava's event bus which lacked some
2012-10-23 11:05:38 +02:00
features like weak references at that time. At its core it offers the following:
2012-10-23 10:31:12 +02:00
2012-10-28 09:42:28 +01:00
+ <em><strong>Annotation driven</em></strong>: To define and customize a message handler simply mark it with @Listener annotation
2012-10-28 09:41:12 +01:00
+ <em><strong>Delivers everything</em></strong>: Messages must not implement any interface and can be of any type (-> message bus is typed using generics with upper
2012-10-28 09:42:28 +01:00
bound being Object.class). The class hierarchy of a message is considered during message delivery. This means that listeners will also receive
2012-10-23 11:02:37 +02:00
subtypes of the message type they are listening for, e.g. a listener for Object.class receives everything.
2012-10-28 09:41:12 +01:00
+ <em><strong>Synchronous and asynchronous message delivery</em></strong>: A handler can be invoked to handle a message either synchronously or
2012-10-23 10:31:12 +02:00
asynchronously. This is configurable for each handler via annotations. Message publication itself supports synchronous (method
blocks until messages are delivered to all handlers) or asynchronous (fire and forget) dispatch
2012-10-28 09:41:12 +01:00
+ <em><strong>Weak references</em></strong>: Mbassador uses weak references to all listening objects to relieve the programmer of the burden to explicitly unregister
2012-10-23 10:31:12 +02:00
listeners that are not used anymore (of course it is also possible to explicitly unregister a listener if needed). This is very comfortable
in certain environments where objects are created by frameworks, i.e. spring, guice etc. Just stuff everything into the message bus, it will
ignore objects without message handlers and automatically clean-up orphaned weak references after the garbage collector has done its job.
2012-10-28 09:41:12 +01:00
+ <em><strong>Filtering</em></strong>: Mbassador offers static message filtering. Filters are configured using annotations and multiple filters can be attached to
2012-10-23 10:31:12 +02:00
a single message handler
2012-10-28 09:41:12 +01:00
+ <em><strong>Error handling</em></strong>: Errors during message delivery are sent to an error handler of which a custom implementation can easily be plugged-in.
+ <em><strong>Ease of Use</em></strong>: Using Mbassador in your project is very easy. Create as many instances of Mbassador as you like (usually a singleton will do),
2012-10-23 10:31:12 +02:00
mark and configure your message handlers with @Listener annotations and finally register the listeners at any Mbassador instance. Start
sending messages to your listeners using one of Mbassador's publication methods (sync or async). Done!
2012-10-28 09:11:45 +01:00
2012-10-28 09:19:55 +01:00
2012-10-28 09:16:47 +01:00
<h2>Usage</h2>
2012-10-28 09:11:45 +01:00
2012-10-28 09:16:47 +01:00
Listener definition (in any bean):
2012-10-28 09:11:45 +01:00
// every event of type TestEvent or any subtype will be delivered
// to this handler
@Listener
public void handleTestEvent(TestEvent event) {
// do something
}
// this handler will be invoked asynchronously
@Listener(mode = Listener.Dispatch.Asynchronous)
public void handleSubTestEvent(SubTestEvent event) {
// do something more expensive here
}
// this handler will receive events of type SubTestEvent
// or any subtabe and that passes the given filter(s)
2012-10-28 09:27:41 +01:00
@Listener({@Filter(SpecialEventsOnly.class),@Filter(AnotherFilter.class)})
2012-10-28 09:11:45 +01:00
public void handleFiltered(SubTestEvent event) {
//do something special here
}
Creation of message bus and registration of listeners:
// create as many instances as necessary
// bind it to any upper bound
MBassador<TestEvent> bus = new MBassador<TestEvent();
ListeningBean listener = new ListeningBean();
2012-10-28 09:16:47 +01:00
// the listener will be registered using a weak-reference
bus.subscribe(listener);
// objects without handlers will be ignored
bus.subscribe(new ClassWithoutAnyDefinedHandlers());
2012-10-28 09:11:45 +01:00
Message puclication:
TestEvent event = new TestEvent();
TestEvent subEvent = new SubTestEvent();
bus.publishAsync(event); //returns immediately, publication will continue asynchronously
bus.publish(subEvent); // will return after each handler has been invoked
2012-10-28 09:19:55 +01:00
2012-10-28 09:18:17 +01:00
<h2>Planned features</h2>
2012-10-23 10:37:19 +02:00
+ Maven dependency: Add Mbassador to your project using maven. Coming soon!
+ Message handler priority: Message handlers can specify priority to influence order of message delivery
2012-10-30 14:22:56 +01:00
<h2>License</h2>
This project is distributed under the terms of the MIT License. See file "LICENSE" for further reference.
2012-10-23 10:37:19 +02:00