Lightweight Message Bus library [closed]

限于喜欢 提交于 2019-11-27 02:10:28

问题


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".


回答1:


Have a look at eventbus.

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




回答2:


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.




回答3:


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.




回答4:


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




回答5:


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



来源:https://stackoverflow.com/questions/1953380/lightweight-message-bus-library

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!