17

I will be starting a small Java (GWT really) project in the near future and I am at "information gathering" phase.

Q: Is there a lightweight Message Bus library written in Java?

My requirements are lightweight too :-)

  1. async (no need for sync)
  2. multicast and point-to-point
  3. no strict message ordering
  4. message "envelope" ideally "owned" by Message Bus (i.e. in terms of life-cycle management)
  5. localized delivery (i.e. not inter-process nor inter-node)

Update: It seems that GWT now supports an integrated "event bus".

jldupont
  • 93,734
  • 56
  • 203
  • 318
  • You can look here https://github.com/javaplugs/minibus it is extremely lightweight and simple event bus. – rumatoest Feb 14 '16 at 11:03

5 Answers5

12

Have a look at eventbus.

(Link fixed; thanks to jldupont to point that out).

Community
  • 1
  • 1
Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
10

I know this is an old question but I think a more modern solution is to use Guava's Event Bus (granted I'm not sure if it will work for GWT but both your title and tags don't say GWT).

I actually have a custom RabbitMQ simple message container that will automatically create queue bindings and on messages received will then dispatch to Guava EventBus. Its incredible elegant, and scales superbly.

You can easily use your DI framework to register the Subscribers. For Spring I create BeanPostProcessor that will automatically registers beans with @Subscribe.

Below is the Spring BeanPostProcessor:

package com.snaphop.spring;

import java.lang.reflect.Method;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;

@Component
public class EventBusBeanPostProcessor implements BeanPostProcessor {

    private EventBus eventBus;

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if (isApplicable(bean)) {
            eventBus.register(bean);
        }
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }

    protected boolean isApplicable(Object bean) {
        for(Method m : bean.getClass().getMethods()) {
            if (m.isAnnotationPresent(Subscribe.class))
                return true;
        }
        return false;
    }

    @Autowired
    public void setEventBus(EventBus eventBus) {
        this.eventBus = eventBus;
    }

}

I'm sure is trivial to do something similar in Guice.

Adam Gent
  • 47,843
  • 23
  • 153
  • 203
  • I have tried to use google guavas event bus and got stuck because it uses strong references to the listeners. I wanted to use it in a spring environment and just stuff all the beans into it using a post processor. This won't work with guava. I created my own solution which is hosted on github https://github.com/bennidi/mbassador. It's quite stable now (used in production for half a year) and is faster than the google bus. It also offers more features but is as easy to use. – bennidi Oct 23 '12 at 09:25
  • I made my own implementation likewise but mainly because Guava uses JULI and I didn't like it catching the exception. I'll have to check out yours... I also noticed that Guava uses a collection of Class> which can potential lead to permgen leaks. – Adam Gent Oct 23 '12 at 12:36
  • 1
    What do you mean by collection of Class> could lead to permgen leaks? Besides, I would really appreciate Feedback on MBassador. If it lacks a feature that you would require you can file an issue on github. – bennidi Oct 28 '12 at 14:01
  • Actually, explicit checking for @Subscribe Annotations is not necessary with the Guava EventBus: https://github.com/google/guava/wiki/EventBusExplained#what-happens-if-i-register-a-listener-without-any-handler-methods – Alex Jul 05 '17 at 12:07
5

If you happen to be using Spring already, then a handy sort-of-hidden feature of Spring is the ApplicationEventMulticaster interface, which is a very simple API for publishing and subscribing to application-generated events. The implementations use the TaskExecutor framework, which means they can be sync or async as desired. Furthermore, every ApplicationContext has a publishEvent method, so it's comically easy to set it up for Spring-managed classes.

So yes, if you already use Spring, there's no need to for another utility to do this, it's built in already.

skaffman
  • 398,947
  • 96
  • 818
  • 769
0

Not sure if it is really lightweight. But it does fit the rest of your description: ActiveMQ

Toad
  • 15,593
  • 16
  • 82
  • 128
  • @reinier: I know about AMQP based solutions: (1) not lightweight (of course it is relative ;-) (2) not really applicable for a GWT based project. – jldupont Dec 23 '09 at 15:41
  • @jldupont: ah right... it all has to run in the same process in the browser right? Yes in that case ignore this idea ;^) – Toad Dec 23 '09 at 15:58
  • @reinier: then delete it as you'll likely get some down-votes at some point (not from me). – jldupont Dec 23 '09 at 16:02
  • 3
    @jldupont: They tend to do that. ;^) Still my wrong solution does add to the conversation for anyone else who might have come up with the same response – Toad Dec 23 '09 at 19:20
  • @reinier: have it your way then. Cheers :-) – jldupont Dec 23 '09 at 20:26
  • you only need this connector for gwt. http://code.google.com/p/gwt-activemq/ – MarianP Oct 12 '11 at 20:10
0

Maybe you could try some jabber (XMPP) based solution. What about openfire?

marcospereira
  • 12,045
  • 3
  • 46
  • 52
  • @marcospereira: are you serious? it is not because I don't down-vote (often) that one can abuse here! – jldupont Dec 23 '09 at 16:55
  • jldupont: yes, I'm serious. And I can't understand why you consider my comment as an abuse. If you think it is a wrong solution, put some arguments on the table. ;-) – marcospereira Jan 01 '10 at 18:48
  • actually xmpp is pretty cool, but maybe it has too much overhead – Leo Aug 18 '16 at 03:52