Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
An event bus aiming at ease of use and performance. Features: Declarative listener definition via annotations, sync and/or async event delivery, weak-references, message filtering
Java Shell

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
maven/org/mbassy/mbassador
src
.gitignore
LICENSE
README.md
build.gradle
pom.xml

README.md

Mbassador

Mbassador is a very light-weight message (event) bus implementation following the publish subscribe pattern. It is designed 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.

Read this documentation to get an overview of its features and how cool this message (event) bus actually is. You can also check out the performance comparison which also contains a partial list of the features of the compared implementations.

The current version is 1.0.6.RC, see the release notes for more details.

Table of contents:

Features

At its core MBassador offers the following features:

  • Annotation driven: To define and customize a message handler simply mark it with @Listener annotation
  • Delivers everything: Messages must not implement any interface and can be of any type (-> message bus is typed using generics with upper bound being Object.class). The class hierarchy of a message is considered during message delivery. This means that listeners will also receive subtypes of the message type they are listening for, e.g. a listener for Object.class receives everything.
  • Synchronous and asynchronous message delivery: A handler can be invoked to handle a message either synchronously or 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
  • Weak references: Mbassador uses weak references to all listening objects to relieve the programmer of the burden to explicitly unregister 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.
  • Filtering: Mbassador offers static message filtering. Filters are configured using annotations and multiple filters can be attached to a single message handler
  • Message envelopes: 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
  • Handler priorities: A listener can be associated with a priority to influence the order of the message delivery
  • Error handling: Errors during message delivery are sent to an error handler of which a custom implementation can easily be plugged-in.
  • Ease of Use: Using Mbassador in your project is very easy. Create as many instances of Mbassador as you like (usually a singleton will do), 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!

Usage

Listener definition (in any bean):

    // 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(dispatch = Mode.Asynchronous)
    public void handleSubTestEvent(SubTestEvent event) {
        // do something more expensive here
    }

    // this handler will receive messages of type SubTestEvent
    // or any of its sub types that passe the given filter(s)
    @Listener(priority = 10,
              dispatch = Mode.Synchronous,
              filters = {@Filter(Filters.SpecialEvent.class)})
    public void handleFiltered(SubTestEvent event) {
       //do something special here
    }

    @Listener(dispatch = Mode.Synchronous, rejectSubtypes = true)
    @Enveloped(messages = {TestEvent.class, TestEvent2.class})
    public void handleVariousEvents(MessageEnvelope envelope) {
        // 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
    }

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>(BusConfiguration.Default());
    ListeningBean listener = new ListeningBean();
    // the listener will be registered using a weak-reference
    bus.subscribe(listener);
    // objects without handlers will be ignored
    bus.subscribe(new ClassWithoutAnyDefinedHandlers());

Message publication:

    TestEvent event = new TestEvent();
    TestEvent subEvent = new SubTestEvent();

    bus.publishAsync(event); //returns immediately, publication will continue asynchronously
    bus.post(event).asynchronously(); // same as above
    bus.publish(subEvent);   // will return after each handler has been invoked
    bus.post(subEvent).now(); // same as above

Installation

This project contains a maven repository that will allow you to import MBassador as a dependency into your maven project. 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:

  1. Add the repository location to your pom.xml
    
    <repositories>
        <repository>
            <id>mbassador-github-repo</id>
            <url>https://raw.github.com/bennidi/mbassador/master/maven </url>
        </repository>
    </repositories>
    
  2. Add the MBassador dependency to your pom.xml. You can check which versions are available by browsing the git repository online.
    
        <dependency>
            <groupId>org.mbassy</groupId>
            <artifactId>mbassador</artifactId>
            <version>1.0.6.RC</version>
        </dependency>
    
  3. Run mvn clean package to have maven download and install the required version into your local repository

Of course you can always clone the repository and build from source

Release Notes

1.0.6.RC

  • 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

1.0.5.RC

  • 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

1.0.4.RC

  • Introduced BusConfiguration as a central class to encapsulate configurational aspects

Roadmap

  • Checkout MBassador from one of the official maven repositories (as soon as the user base is big enough)
  • Spring integration with support for conditional message dispatch in transactional context (dispatch only after successful commit etc.). Currently in beta, see this repository

Credits

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. It matured to be quite a feature rich and yet very efficient and performant message bus.

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.

Contribute

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.

License

This project is distributed under the terms of the MIT License. See file "LICENSE" for further reference.

Something went wrong with that request. Please try again.