设计模式之发布订阅模式(4) Guava Eventbus 事件处理

栏目: Java · 发布时间: 4年前

内容简介:EventBus是Guava实现的的事件处理机制,是设计模式中的发布订/阅模式的优雅和简单的解决方案。通过这种方式,我们既不需要创建复杂的类结构,也没有复杂的接口层次结构,就可以快速实现基于事件模型的发布/订阅模式。如果对事件监听和发布/订阅模式的实现,只能推荐一种的话,那么首选就是Guava的EventBus。集成Guava的EventBus非常简单,只需要把自定义的

EventBus是Guava实现的的事件处理机制,是 设计模式 中的发布订/阅模式的优雅和简单的解决方案。通过这种方式,我们既不需要创建复杂的类结构,也没有复杂的接口层次结构,就可以快速实现基于事件模型的发布/订阅模式。

如果对事件监听和发布/订阅模式的实现,只能推荐一种的话,那么首选就是Guava的EventBus。

集成过程

集成Guava的EventBus非常简单,只需要把自定义的 EventEventListener 放入 EventBus ,然后你就可以通过EventBus来发布消息了。下面一步步讲解一下。

:tractor: 本文源码Github地址

增加Guava依赖

首先为项目增加guava依赖,同时我们引入了Lombok来简化JavaBean的定义。

<dependency>
  <groupId>com.google.guava</groupId>
  <artifactId>guava</artifactId>
  <version>27.1-jre</version>
</dependency>

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
  </dependency>
复制代码

自定义Event

你可以看到我们自定义的Event不需要实现Guava的任何接口,只要把自己需要用到的事件参数定义好就行了。这里我们只提供一个message参数。

package net.ijiangtao.tech.designpattern.pubsub.guava;

import lombok.AllArgsConstructor;
import lombok.Data;

/**
 * CustomEvent
 *
 * @author ijiangtao
 * @create 2019-05-02 18:21
 **/
@AllArgsConstructor
@Data
public class CustomEvent {
    private String message;
}
复制代码

自定义EventListener

通过在方法上添加Guava的 @Subscribe 注解,我们可以让方法监听某个Event。

package net.ijiangtao.tech.designpattern.pubsub.guava;

import com.google.common.eventbus.Subscribe;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * EventListener
 *
 * @author ijiangtao
 * @create 2019-05-02 18:15
 **/
@Slf4j
@Data
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class CustomEventListener {

    private List<String> listenedMessageList;

    @Subscribe
    public void onEvent(CustomEvent event) {
        log.info("Guava EventListener listened one message : {}", event.getMessage());
        listenedMessageList.add(event.getMessage());
    }

}
复制代码

事件注册、发布和取消注册

EventBusregister 方法可以将前面定义好的监听器注册到 EventBus 中, post 方法可以发布事件并通知到所有订阅该事件的监听器, unregister 方法可以把指定的监听器从 EventBus 中移除。

package net.ijiangtao.tech.designpattern.pubsub.guava;

import com.google.common.eventbus.EventBus;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.ArrayList;
import java.util.List;

/**
 * guava event bus tests
 *
 * @author ijiangtao
 * @create 2019-05-02 18:24
 **/
@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class GuavaEventBusTests {

    @Test
    public void test() {

        EventBus eventBus = new EventBus();

        List<String> listenedMessageList = new ArrayList<>();
        CustomEventListener customEventListener = new CustomEventListener(listenedMessageList);

        eventBus.register(customEventListener);

        eventBus.post(new CustomEvent("post a custom event ---- 1"));

        eventBus.unregister(customEventListener);

        eventBus.post(new CustomEvent("post a custom event ---- 2"));

    }

}
复制代码

总结

设计模式之发布订阅模式(4) Guava Eventbus 事件处理

这篇文章,我们快速演示了如何使用Guava的EventBus来实现发布/订阅模式。EventBus作为一种事件机制的轻量、简单、低侵入的实现方式,在简单的事件处理场景下是非常推荐使用的。

如果你的事件处理机制有分布式或者条件过滤等要求,可以考虑使用之前介绍的 Redis 发布/订阅模式 或者 Spring Events 事件驱动模型

设计模式之发布订阅模式(4) Guava Eventbus 事件处理

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Beginning XML with DOM and Ajax

Beginning XML with DOM and Ajax

Sas Jacobs / Apress / 2006-06-05 / USD 39.99

Don't waste time on 1,000-page tomes full of syntax; this book is all you need to get ahead in XML development. Renowned web developer Sas Jacobs presents an essential guide to XML. Beginning XML with......一起来看看 《Beginning XML with DOM and Ajax》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具