google guava EventBus使用(二)

一、写在前面的话,guava的坐标为:
version=21.0
groupId=com.google.guava
artifactId=guava

整体交互过程见下:


guava EventBus.png
UML.png

二、登录日志示例实现
这里以登录为示例,讲述异步记录登录日志的事件驱动。

1、定义事件分发器接口

/**
* 事件分发器的接口
*/
public interface EventDispatcher {

    void register(Object object);

    void unregister(Object object);

    void post(Object event);
}

2、实现事件分发器接口

import com.google.common.eventbus.EventBus;
import org.springframework.stereotype.Service;

/**
 * 事件分发器,使用google guava EventBus机制
 */
@Service
public class EventDispatcherImpl implements EventDispatcher {

    private EventBus eventBus = new EventBus();

    @Override
    public void register(Object object) {
        eventBus.register(object);
    }

    @Override
    public void unregister(Object object) {
        eventBus.unregister(object);
    }

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

这里引用下EventBus类的上述三个方法的源码

 public void register(Object object) {
        this.subscribers.register(object);
    }

    public void unregister(Object object) {
        this.subscribers.unregister(object);
    }

    public void post(Object event) {
        Iterator eventSubscribers = this.subscribers.getSubscribers(event);
        if (eventSubscribers.hasNext()) {
            this.dispatcher.dispatch(event, eventSubscribers);
        } else if (!(event instanceof DeadEvent)) {
            this.post(new DeadEvent(this, event));
        }

    }

3、定义事件生产者接口。

public interface ApplicationEvents {

    /**
     * 登录日志
     */
    void loginLog(Long userId);

}

4、实现事件生产者代码

import com.xxx.event.LoginLogEvent;
import com.xxx.event.ApplicationEvents;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 事件生产者
 **/
@Service
public class ApplicationEventsImpl implements ApplicationEvents {

    @Autowired
    private EventDispatcher eventDispatcher;

    @Override
    public void loginLog(Long userId) {
        LoginLogEvent event = LoginLogEvent.builder()
                .userId(userId)           
                .build();
        eventDispatcher.post(event);
    }
}

5、定义事件LoginLogEvent
一个普通的java类

package com.xxx.event.LoginLogEvent;

public class LoginLogEvent implements Serializable {
    private static final long serialVersionUID = -1L;
    private Long userId;
}

6、定义事件消费者,也做监听者

import com.google.common.eventbus.Subscribe;
import com.xxx.event.LoginLogEvent;
import com.xxx.event.EventDispatcher;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

/**
 * 事件监听者
 **/
@Service
@Slf4j
public class LoginLogSubscriber {

    @Autowired
    private EventDispatcher eventDispatcher;

    @PostConstruct
    public void startup() {
        eventDispatcher.register(this);
    }

    @PreDestroy
    public void destroy() {
        eventDispatcher.unregister(this);
    }

    @Subscribe
    public void loginLog(LoginLogEvent event) {
            log.info("用户登录日志成功 [{}]", event);      
    }
}

三、总结


image.png

你可能感兴趣的:(google guava EventBus使用(二))