MessageBus/README.md

191 lines
9.1 KiB
Markdown
Raw Normal View History

2013-01-10 13:18:01 +01:00
MBassador
2012-10-23 09:27:32 +02:00
=========
2013-01-10 13:18:01 +01:00
MBassador is a very light-weight message (event) bus implementation following the publish subscribe pattern. It is designed
2012-12-25 16:52:37 +01:00
for ease of use and aims to be feature rich and extensible while preserving resource efficiency and performance. It uses a specialized
data structure to allow high throughput for concurrent access.
2012-11-22 14:41:51 +01:00
2012-11-21 00:02:00 +01:00
Read this documentation to get an overview of its features and how cool this message (event) bus actually is.
2012-11-20 14:12:57 +01:00
You can also check out the <a href="http://codeblock.engio.net/?p=37" target="_blank">performance comparison</a>
2012-11-21 00:02:00 +01:00
which also contains a partial list of the features of the compared implementations.
2012-11-20 14:12:57 +01:00
2013-01-10 13:18:01 +01:00
The current version is 1.1.0 and it is available from the Maven Central Repository. See the release notes for more details.
2012-11-22 14:41:51 +01:00
2012-11-20 14:12:57 +01:00
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)
2012-12-25 16:52:37 +01:00
+ [Release Notes](#releasenotes)
2012-11-20 14:12:57 +01:00
+ [Roadmap](#roadmap)
+ [Credits](#credits)
2012-11-21 00:00:18 +01:00
+ [Contribute](#contribute)
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:15:15 +01:00
<h2 name="features">Features</h2>
2012-11-20 14:03:30 +01:00
2012-11-21 00:00:18 +01:00
At its core MBassador offers the following features:
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-12-12 14:56:28 +01:00
+ <em><strong>Message envelopes</em></strong>: Message handlers can declare to receive an enveloped message. The envelope can wrap around different
types of messages. This allows for a single handler to handle multiple message types
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:15:15 +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-11-23 04:41:12 +01:00
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
}
2012-12-25 17:19:58 +01:00
// this handler will receive messages of type SubTestEvent
// or any of its sub types that passe the given filter(s)
2012-11-19 12:34:53 +01:00
@Listener(priority = 10,
dispatch = Mode.Synchronous,
2012-12-25 17:19:58 +01:00
filters = {@Filter(Filters.SpecialEvent.class)})
2012-10-28 09:11:45 +01:00
public void handleFiltered(SubTestEvent event) {
//do something special here
}
2012-12-25 17:19:58 +01:00
@Listener(dispatch = Mode.Synchronous, rejectSubtypes = true)
2012-12-12 14:56:28 +01:00
@Enveloped(messages = {TestEvent.class, TestEvent2.class})
2012-12-25 17:22:07 +01:00
public void handleVariousEvents(MessageEnvelope envelope) {
2012-12-25 17:19:58 +01:00
// the envelope will contain either an instance of TestEvent or TestEvent2
// if rejectSubtypes were set to 'false' (default) also subtypes of TestEvent or TestEvent2 would be allowed
2012-12-12 14:56:28 +01:00
}
2012-10-28 09:11:45 +01:00
Creation of message bus and registration of listeners:
// create as many instances as necessary
// bind it to any upper bound
2012-12-08 09:14:59 +01:00
MBassador<TestEvent> bus = new MBassador<TestEvent>(BusConfiguration.Default());
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:15:15 +01:00
<h2>Installation</h2>
2013-01-10 13:18:01 +01:00
Beginning with version 1.1.0 MBassador is available from the Maven Central Repository (Hooray!). Older versions are
still available from the included maven repository in this github repo but will be deleted in the future.
The preferred way of using MBassador is to simply add the dependency as shown in step two. Step one is only necessary
if you want to use an older version that is not available in the central repository.
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;
2013-01-10 13:18:01 +01:00
&lt;groupId&gt;net.engio&lt;/groupId&gt;
2012-11-19 16:36:24 +01:00
&lt;artifactId&gt;mbassador&lt;/artifactId&gt;
2013-01-10 13:18:01 +01:00
&lt;version&gt;1.1.0&lt;/version&gt;
2012-11-19 16:36:24 +01:00
&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-12-25 16:52:37 +01:00
<h2>Release Notes</h2>
2013-01-09 17:43:35 +01:00
<h3>1.1.0</h3>
First stable release!
+ Refactoring and repackaging
+ More exhaustive unit tests
2013-01-10 13:18:01 +01:00
+ Installation from the central repository
2013-01-09 17:43:35 +01:00
2012-12-25 16:52:37 +01:00
<h3>1.0.6.RC</h3>
+ Fixed behaviour with capacity bound blocking queue such that there now are two methods to schedule a message
asynchronously. One will block until capacity becomes available, the other will timeout after a specified amount of
time.
+ Added unit tests
<h3>1.0.5.RC</h3>
+ Added MessageEnvelope and @Enveloped annotation to configure handlers that might receive arbitrary message type
+ Added handler configuration property to @Listener annotation to move from message filtering to more specific implementation
of this feature
<h3>1.0.4.RC</h3>
+ Introduced BusConfiguration as a central class to encapsulate configurational aspects
2012-11-20 14:15:15 +01:00
<h2>Roadmap</h2>
2012-11-13 19:39:23 +01:00
+ Spring integration with support for conditional message dispatch in transactional context (dispatch only after
2012-11-21 00:46:23 +01:00
successful commit etc.). Currently in beta, see <a href="https://github.com/bennidi/mbassador-spring">this</a> repository
2012-11-20 14:12:57 +01:00
2012-11-20 14:15:15 +01:00
<h2>Credits</h2>
2012-11-20 14:12:57 +01:00
The initial inspiration for creating this component came from looking at Google Guava's event bus implementation. Since
2012-12-25 16:52:37 +01:00
it did not provide all the features we needed in our project, I decided to create my own implementation. It matured to be
2013-01-09 17:43:35 +01:00
quite a feature rich and yet very efficient and performant implementation.
2012-12-25 16:52:37 +01:00
I want to thank the development team from friendsurance (www.friendsurance.de) for their support and feedback on the bus
implementation and the management of friendsurance for allowing me to publish the component as an open source project.
2012-11-20 14:18:03 +01:00
2012-11-21 00:00:18 +01:00
<h2>Contribute</h2>
Any feature requests and feedback are more than welcome. You may suggest improvements either by submitting an
issue or by forking the repo and creating a pull request. I will try to respond as quickly as possible.
2012-10-23 10:37:19 +02:00
2012-11-20 14:15:15 +01:00
<h2>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
2012-11-21 00:00:18 +01:00