Google Guava:EventBus源码解析和发布订阅消息的简单使用示例

EventBus是Guava中对于事件发布订阅功能的实现,是设计模式中的发布/订阅模式的一种实现方案。

功能概括:

通过eventBus.register注册订阅者,通过eventBus.post方法发布事件,然后根据发布事件的类型(classType),执行所有订阅者中被@Subcribe注解标记的且参数类型一致的方法,从而实现发布、订阅功能。

源码解读:

源码基于如下版本:


    com.google.guava
    guava
    25.1-jre

1. 通过eventBus.register()方法注册订阅者

Google Guava:EventBus源码解析和发布订阅消息的简单使用示例_第1张图片

2. 将该订阅者注册到该eventBus中,源码见findAllSubcribers(listener)方法。在这个过程中,会扫描到被注册类中被@Subcribe注解标记的方法,并将这些方法按照 入参类型 分类,以入参类型为key添加到Multimap, Subcriber>中,入参类型可以使用自定义对象。

Google Guava:EventBus源码解析和发布订阅消息的简单使用示例_第2张图片

3. 通过eventBus.post()方法发布事件,根据事件类型找到所有对应subcriber,遍历;然后由eventBus中的线程池执行订阅者中与入参类型匹配的方法。

Google Guava:EventBus源码解析和发布订阅消息的简单使用示例_第3张图片

Google Guava:EventBus源码解析和发布订阅消息的简单使用示例_第4张图片

简单使用示例:

在springboot中,使用自动注入的方式来实现

目录结构:

Google Guava:EventBus源码解析和发布订阅消息的简单使用示例_第5张图片

代码如下:

package com.example.demo.eventbus.config;

import com.google.common.eventbus.EventBus;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 注入eventBus bean
 *
 */
@Configuration
public class EventBusConfig {

    @Bean("eventBus")
    public EventBus eventBus() {
        return new EventBus("demo");
    }
}


package com.example.demo.eventbus.listener;

import com.google.common.eventbus.EventBus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Map;

/**
 * 注册eventListener
 *
 */
@Component
public class ApplicationListenerRegister implements ApplicationListener {

    @Autowired
    private EventBus eventBus;

    @Autowired
    private ConfigurableApplicationContext context;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        Map listenerMap = context.getBeansOfType(IEventListener.class);
        if (!CollectionUtils.isEmpty(listenerMap)) {
            for (Map.Entry entry : listenerMap.entrySet()) {
                eventBus.register(entry.getValue());
                System.out.println("eventListener registed: " + entry.getValue().getClass());
            }
        }
    }
}


package com.example.demo.eventbus.listener;

/**
 * 事件监听者接口
 *
 */
public interface IEventListener {
}


package com.example.demo.eventbus.listener;

import com.google.common.eventbus.Subscribe;
import org.springframework.stereotype.Component;

/**
 * 监听Integer
 *
 */
@Component
public class ListenerInt implements IEventListener{

    @Subscribe
    public int call(Integer num) {
        System.out.println("ListenerInt call(): " + num);
        return num;
    }

}


package com.example.demo.eventbus.listener;

import com.google.common.eventbus.Subscribe;
import org.springframework.stereotype.Component;

/**
 * 监听字符串事件
 *
 */
@Component
public class ListenerStr implements IEventListener{

    @Subscribe
    public String call(String str) {
        System.out.println("ListenerStr call() : " + str);
        return str;
    }

    @Subscribe
    public String call2(String str) {
        System.out.println("ListenerStr call2() : " + str);
        return str;
    }
}


package com.example.demo.eventbus;

import com.google.common.eventbus.EventBus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * eventBus测试
 *
 */
@RestController
@RequestMapping("/event")
public class EventBusTestController {


    @Autowired
    private EventBus eventBus;

    @GetMapping("/eventTest")
    public void eventTest() {
        System.out.println("eventTest started");
        eventBus.post("string test");

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        eventBus.post(111);
        System.out.println("eventTest end");
    }
}

运行结果:

 

补充:

别的博客中看到过这样一段总结,感觉不错,摘录下:(出自:https://blog.csdn.net/yanghua_kobe/article/details/46317297)

总结

Guava的EventBus源码还是比较简单、清晰的。从源码来看,它一番常用的Observer的设计方式,放弃采用统一的接口、统一的事件对象类型。转而采用基于注解扫描的绑定方式。

其实无论是强制实现统一的接口,还是基于注解的实现方式都是在构建一种关联关系(或者说满足某种契约)。很明显接口的方式是编译层面上强制的显式契约,而注解的方式则是运行时动态绑定的隐式契约关系。接口的方式是传统的方式,编译时确定观察者关系,清晰明了,但通常要求有一致的事件类型、方法签名。而基于注解实现的机制,刚好相反,编译时因为没有接口的语法层面上的依赖关系,显得不那么清晰,至少静态分析工具很难展示观察者关系,但无需一致的方法签名、事件参数,至于多个订阅者类之间的继承关系,可以继承接收事件的通知,可以看作既是其优点也是其缺点。

你可能感兴趣的:(Java)