MessageBus/README.md

140 lines
7.1 KiB
Markdown
Raw Normal View History

2012-10-23 10:31:12 +02:00
Mbassador
2012-10-23 09:27:32 +02:00
=========
2012-11-19 12:34:53 +01:00
Mbassador is a very light-weight message bus (event bus) implementation following the publish subscribe pattern. It is designed
for ease of use and aims to be feature rich, extensible while preserving resource efficiency and performance.
2012-11-20 13:58:36 +01:00
2012-11-20 14:12:57 +01:00
Read this documentation to get an overview of its features and how cool this event bus actually is.
You can also check out the <a href="http://codeblock.engio.net/?p=37" target="_blank">performance comparison</a>
which also reviews part of the features of the compared implementations.
Table of contents:
2012-11-20 14:03:30 +01:00
+ [Features](#features)
2012-11-20 14:12:57 +01:00
+ [Usage](#usage)
+ [Installation](#installation)
+ [Roadmap](#roadmap)
+ [Credits](#credits)
2012-11-20 14:03:30 +01:00
+ [License](#license)
2012-11-20 13:58:36 +01:00
2012-11-19 12:34:53 +01:00
2012-11-20 14:12:57 +01:00
<h2 name="features">Feature overview</h2>
2012-11-20 14:03:30 +01:00
2012-11-19 12:34:53 +01:00
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-11-19 12:34:53 +01:00
+ <em><strong>Handler priorities</em></strong>: A listener can be associated with a priority to influence the order of the message delivery
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-11-20 14:12:57 +01:00
<h2 name="usage">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
2012-11-19 12:34:53 +01:00
@Listener(dispatch = Mode.Asynchronous)
2012-10-28 09:11:45 +01:00
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-11-19 12:34:53 +01:00
@Listener(priority = 10,
dispatch = Mode.Synchronous,
filters = {@Filter(MessageFilter.None.class),@Filter(MessageFilter.All.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
2012-11-19 12:34:53 +01:00
MBassador<TestEvent> bus = new MBassador<TestEvent>();
2012-10-28 09:11:45 +01:00
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
2012-11-13 19:39:23 +01:00
Message publication:
2012-10-28 09:11:45 +01:00
TestEvent event = new TestEvent();
TestEvent subEvent = new SubTestEvent();
bus.publishAsync(event); //returns immediately, publication will continue asynchronously
2012-11-19 12:34:53 +01:00
bus.post(event).asynchronously(); // same as above
2012-10-28 09:11:45 +01:00
bus.publish(subEvent); // will return after each handler has been invoked
2012-11-19 12:34:53 +01:00
bus.post(subEvent).now(); // same as above
2012-10-28 09:11:45 +01:00
2012-11-20 14:12:57 +01:00
<h2 name="installation">Installation</h2>
2012-11-19 16:10:40 +01:00
This project contains a maven repository that will allow you to import MBassador as a dependency into your maven project.
2012-11-20 14:03:30 +01:00
Currently this is all that will be provided because publishing to a central repository requires extra project setup that
will be done as soon as enough people use this component. Until then, the following steps are necessary:
2012-10-28 09:19:55 +01:00
2012-11-19 16:10:40 +01:00
1. Add the repository location to your pom.xml
2012-11-19 16:36:24 +01:00
<pre><code class="xml">
&lt;repositories&gt;
&lt;repository&gt;
&lt;id&gt;mbassador-github-repo&lt;/id&gt;
&lt;url&gt;https://raw.github.com/bennidi/mbassador/master/maven &lt;/url&gt;
&lt;/repository&gt;
&lt;/repositories&gt;
</pre></code>
2012-11-19 16:10:40 +01:00
2. Add the MBassador dependency to your pom.xml. You can check which versions are available by browsing
the git repository online.
2012-11-19 16:28:08 +01:00
<pre><code class="xml">
2012-11-19 16:36:24 +01:00
&lt;dependency&gt;
&lt;groupId&gt;org.mbassy&lt;/groupId&gt;
&lt;artifactId&gt;mbassador&lt;/artifactId&gt;
&lt;version&gt;1.0.0.RC&lt;/version&gt;
&lt;/dependency&gt;
2012-11-19 16:28:08 +01:00
</pre></code>
2012-11-19 16:10:40 +01:00
3. Run mvn clean package to have maven download and install the required version into your local repository
2012-11-20 14:12:57 +01:00
Of course you can always clone the repository and build from source
2012-11-19 16:10:40 +01:00
2012-11-20 14:12:57 +01:00
<h2 name="roadmap">Feature Roadmap</h2>
+ Make MBassador available in one of the official maven repositories as soon as the user base is big enough
2012-11-13 19:39:23 +01:00
+ Spring integration with support for conditional message dispatch in transactional context (dispatch only after
2012-11-19 16:10:40 +01:00
successful commit etc.) -> coming in december 2012
2012-11-20 14:12:57 +01:00
+ MessageEnvelope for each dispatch that is passed to the handler and can be used for communication between handlers
during the running dispatch
<h2 name="credits">Credits</h2>
The initial inspiration for creating this component came from looking at Google Guava's event bus implementation. Since
it did not provide all the features we needed in our project, I decided to create my own implementation. When I saw that
it outperformed the Guava implementation by far, I decided to share it with the community (I was longing to give something
back to the community for quite a while).
2012-10-23 10:37:19 +02:00
2012-11-20 14:03:30 +01:00
<h2 name="license">License</h2>
2012-10-30 14:22:56 +01:00
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