美文网首页
Guava中EventBus使用

Guava中EventBus使用

作者: CoderZzbJohn | 来源:发表于2019-01-12 22:02 被阅读0次

1.使用的好处:将发生事件的代码和处理事件的代码进行了解耦。比如发送消息。当某一件事情发生了之后,需要触发消息通知。可能需要短信,微信,邮件同时通知。在处理事件的代码中,可以配置三个Subscribe同时处理发消息类型的事件。另外,AsyncEventBus可以很方便的进行异步处理。

注册中心。EventBus中注册监听类。当有消息过来时,会根据消息的类型来消费。

@Component
public class EventBusCenter {

    private EventBus eventBus = new EventBus();

    private AsyncEventBus asyncEventBus = new AsyncEventBus(Executors.newCachedThreadPool());

    public void postSync(Object event) {
        eventBus.post(event);
    }

    public void postAsync(Object event) {
        asyncEventBus.post(event);
    }

    @PostConstruct
    public void init() {
        Map<String, Object> data = SpringContextUtil.getBeanByAnnotation(EventBusLisener.class);
        for (Map.Entry entry : data.entrySet()) {
            eventBus.register(entry.getValue());
            asyncEventBus.register(entry.getValue());
        }
        System.out.println("1");
    }
}

消息对象。

public class OrderCreateEvent {
    private long orderId;
    private long userId;
}

往EventBus中post消息。

@RequestMapping(value = "event")
@Controller
public class EventController {

    @Autowired
    EventBusCenter eventBusCenter;

    @RequestMapping(value = "create")
    public void test() {
        OrderCreateEvent event = new OrderCreateEvent(888L, 999L);
        eventBusCenter.postSync(event);
        System.out.println("zzz");
    }

    @RequestMapping(value = "message")
    public void test1() {
        MessageEvent event = new MessageEvent("elephant", "qqqqqqqqq");
        EventBus eventBus = new EventBus();
        // 需要注册实例。从spring环境中获取,否则无效
        //eventBus.register(MessageLisener.class);
        eventBus.register(SpringContextUtil.getBean(MessageLisener.class));
        eventBus.post(event);
        System.out.println("zzzz");
    }
}

工具类。

@Component
public class SpringContextUtil implements BeanFactoryPostProcessor {

    private static ConfigurableListableBeanFactory factory;

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        factory = beanFactory;
    }

    public static Map<String, Object> getBeanByAnnotation(Class<? extends Annotation> annotationType) {
        Map<String, Object> data = factory.getBeansWithAnnotation(annotationType);
        return data;
    }

    public static Object getBean(Class c) {
        return factory.getBean(c);
    }
}

定义注解。将所有加了该注解的类标识为监听类。加入到注册中心。

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface EventBusLisener {
}

相关文章

  • Guava中EventBus使用

    1.使用的好处:将发生事件的代码和处理事件的代码进行了解耦。比如发送消息。当某一件事情发生了之后,需要触发消息通知...

  • guava EventBus使用

    EventBus是Guava的事件处理机制,是设计模式中的观察者模式(生产/消费者编程模型)的优雅实现。对于事件监...

  • Guava之EventBus源码

    最近需要使用事件驱动,打算使用EventBus管理事件的注册和分发。于是仔细阅读了下Guava的EventBus实...

  • Guava - EventBus(事件总线)

    Guava在guava-libraries中为我们提供了事件总线EventBus库,它是事件发布订阅模式的实现,让...

  • guava EventBus的使用

    前沿 EventBus 是 Guava 的事件处理机制,是观察者模式(生产/消费模型)的一种实现。 观察者模式在我...

  • Guava EventBus

    http://www.baeldung.com/guava-eventbus

  • Guava EventBus

    我称其为单块架构的利器 前言 在设计模式中, 有一种叫做发布/订阅模式, 即某事件被发布, 订阅该事件的角色将自动...

  • Guava——EventBus

    EventBus是Guava的事件处理机制,是设计模式中的观察者模式(生产/消费者编程模型)的优雅实现。对于事件监...

  • Guava-EventBus使用详解

    在使用ApplicationEvent和Listener快速实现业务解耦中提到了用Spring提供的观察者设计模式...

  • google guava EventBus的使用

    事件机制包括三个部分:事件、事件监听器、事件源。 在Spring cloud环境下,使用google公司开源的gu...

网友评论

      本文标题:Guava中EventBus使用

      本文链接:https://www.haomeiwen.com/subject/fychdqtx.html