轻量级消息总线库

发布于 2024-08-15 15:39:26 字数 1539 浏览 2 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

沙与沫 2024-08-22 15:39:26

看看 eventbus

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

Have a look at eventbus.

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

涙—继续流 2024-08-22 15:39:26

我知道这是一个老问题,但我认为更现代的解决方案是使用 Guava 的事件总线(当然,我不确定它是否适用于 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 中做类似的事情是微不足道的。

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.

折戟 2024-08-22 15:39:26

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

所以,是的,如果您已经使用 Spring,则无需使用其他实用程序来执行此操作,它已经内置了。

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.

∞梦里开花 2024-08-22 15:39:26

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

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

两相知 2024-08-22 15:39:26

也许您可以尝试一些基于 jabber (XMPP) 的解决方案。开火呢?

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文