轻量级消息总线库

我将在不久的将来开始一个小型Java(GWT真正)项目,我正处于“信息收集”阶段。

问:是否有用Java编写的轻量级消息总线库?

我的要求也很轻量级:-)

  1. 异步(无需同步)
  2. 多播和点对点
  3. 没有严格的消息排序
  4. 消息总线理想地“拥有”消息“信封”(即在生命周期管理方面)
  5. 本地化传递(即不是进程间或节点间)

更新 :似乎GWT现在支持集成的“事件总线” 。

看看eventbus 。

(链接修复;感谢jldupont指出这一点)。

我知道这是一个老问题,但我认为更现代的解决方案是使用Guava的Event Bus (我不确定它是否适用于GWT,但你的标题和标签都没有说GWT)。

我实际上有一个自定义RabbitMQ简单消息容器 ,它将自动创建队列绑定,然后收到的消息将分派给Guava EventBus。 它令人难以置信的优雅,和卓越的鳞片。

您可以轻松使用DI框架注册订阅者。 对于Spring,我创建了BeanPostProcessor,它将使用@Subscribe自动注册bean。

下面是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; } } 

我肯定在Guice做类似的事情是微不足道的。

如果你恰好使用Spring,那么Spring的一个方便的隐藏function就是ApplicationEventMulticaster接口,它是一个非常简单的API,用于发布和订阅应用程序生成的事件。 这些实现使用TaskExecutor框架,这意味着它们可以根据需要同步或异步。 此外,每个ApplicationContext都有一个publishEvent方法,因此很容易为Spring管理的类设置它。

所以,是的,如果你已经使用了Spring,那么就没有必要使用另一个实用程序了,它已经内置了。

不确定它是否真的很轻巧。 但它确实适合您的描述的其余部分: ActiveMQ

也许你可以尝试一些基于jabber(XMPP)的解决方案。 那么openfire呢?