java 轻量级消息总线库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1953380/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Lightweight Message Bus library
提问by jldupont
I will be starting a small Java (GWT really) project in the near future and I am at "information gathering" phase.
我将在不久的将来开始一个小型 Java(实际上是 GWT)项目,我正处于“信息收集”阶段。
Q: Is there a lightweight Message Bus library written in Java?
问:是否有用 Java 编写的轻量级消息总线库?
My requirements are lightweight too :-)
我的要求也很轻:-)
- async (no need for sync)
- multicast and point-to-point
- no strict message ordering
- message "envelope" ideally "owned" by Message Bus (i.e. in terms of life-cycle management)
- localized delivery (i.e. not inter-process nor inter-node)
- 异步(不需要同步)
- 多播和点对点
- 没有严格的消息排序
- 消息“信封”理想情况下由消息总线“拥有”(即在生命周期管理方面)
- 本地化交付(即不是进程间也不是节点间)
Update: It seems that GWT now supports an integrated "event bus".
更新:似乎 GWT 现在支持集成的“事件总线”。
采纳答案by Aaron Digulla
回答by Adam Gent
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).
我知道这是一个老问题,但我认为更现代的解决方案是使用Guava 的事件总线(当然我不确定它是否适用于 GWT,但您的标题和标签都没有说 GWT)。
I actually have a custom RabbitMQ simple message containerthat will automatically create queue bindings and on messages received will then dispatch to Guava EventBus. Its incredible elegant, and scales superbly.
我实际上有一个自定义的RabbitMQ 简单消息容器,它将自动创建队列绑定,然后收到的消息将分派到 Guava EventBus。其令人难以置信的优雅,以及极好的比例。
You can easily use your DI framework to register the Subscribers. For Spring I create BeanPostProcessor that will automatically registers beans with @Subscribe.
您可以轻松地使用您的 DI 框架来注册订阅者。对于 Spring,我创建了 BeanPostProcessor,它将自动使用@Subscribe.
Below is the Spring BeanPostProcessor:
下面是 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.
我确信在 Guice 中做类似的事情是微不足道的。
回答by skaffman
If you happen to be using Spring already, then a handy sort-of-hidden feature of Spring is the ApplicationEventMulticasterinterface, which is a very simple API for publishing and subscribing to application-generated events. The implementations use the TaskExecutorframework, which means they can be sync or async as desired. Furthermore, every ApplicationContexthas a publishEventmethod, so it's comically easy to set it up for Spring-managed classes.
如果您碰巧已经在使用 Spring,那么 Spring 的一个方便的隐藏特性是ApplicationEventMulticaster接口,这是一个非常简单的 API,用于发布和订阅应用程序生成的事件。实现使用TaskExecutor框架,这意味着它们可以根据需要同步或异步。此外,每个ApplicationContext都有一个publishEvent方法,因此为 Spring 管理的类设置它非常容易。
So yes, if you already use Spring, there's no need to for another utility to do this, it's built in already.
所以是的,如果您已经使用 Spring,则无需使用其他实用程序来执行此操作,它已经内置了。
回答by Toad
回答by marcospereira
Maybe you could try some jabber (XMPP) based solution. What about openfire?
也许您可以尝试一些基于 jabber (XMPP) 的解决方案。开火呢?

