google guava EventBus使用(一)

事件机制包括三个部分:事件、事件监听器、事件源。

在Spring cloud环境下,使用google公司开源的guava工具类EventBus。
一、引入guava的jar包
二、在config下新建一个类EventBusConfig.java

import com.google.common.eventbus.AsyncEventBus;
import com.google.common.eventbus.Subscribe;
import org.reflections.Reflections;
import org.reflections.scanners.MethodAnnotationsScanner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.Set;
import java.util.concurrent.Executors;

@Component
public class EventBusConfig {

   @Autowired
   private ApplicationContext context;

   @Bean
   @ConditionalOnMissingBean(AsyncEventBus.class)
   AsyncEventBus createEventBus() {
       AsyncEventBus eventBus = new AsyncEventBus(Executors.newFixedThreadPool(5));
       Reflections reflections = new Reflections("com.xxx", new MethodAnnotationsScanner());
       Set<Method> methods = reflections.getMethodsAnnotatedWith(Subscribe.class);
       if (null != methods ) {
           for(Method method : methods) {
               try {
                   eventBus.register(context.getBean(method.getDeclaringClass()));
               }catch (Exception e ) {
                   //register subscribe class error
               }
           }
       }

       return eventBus;
   }
}

三、利用接口封装事件发送
1、定义接口LocalEventBus.java

public interface LocalEventBus {
    void post(Event event);
}

2、定义实现类LocalEventBusImpl.java

import com.google.common.eventbus.AsyncEventBus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class LocalEventBusImpl implements LocalEventBus {

    @Autowired
    private AsyncEventBus eventBus;

    @Override
    public void post(Event event) {
        if (null != event) {
            eventBus.post(event);
        }
    }
}

3、接口Event.class

public interface Event<T> {
    T getContent();
}

四、在业务工程里使用:
需要定义事件、消息体、订阅者、发送者。

1、定义login事件

public class LoginEvent implements Event<LoginMsg> {

    private LoginMsg loginMsg;

    public LoginEvent(LoginMsg loginMsg) {
        this.loginMsg = loginMsg;
    }

    @Override
    public LoginMsg getContent() {
        return this.loginMsg;
    }
}

2、定义消息体

public class LoginMsg {
    private Long uid;
    private String mobile;
    private String ip;
    private String osVersion;
    private String deviceModel;
    private String deviceToken;
}

3、定义订阅者

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class LoginSubscriber {

    @Subscribe
    public void onLogin(LoginEvent event) throws BizException {
        LoginMsg msg = event.getContent();
        Long uid = msg.getUid();
        // 具体业务
    }
}

4、定义发送者,把消息发送到EventBus。

@Autowired
private LocalEventBus localEventBus;

LoginMsg msg = new LoginMsg(uid, mobile, ip, osVersion, deviceModel, deviceToken); 

localEventBus.post(new LoginEvent(msg));
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,314评论 19 139
  • 通过之前的两篇我们能在本地搭建单一和集群两种方式的dubbo服务,这篇我们来看 springmvc+spring+...
    安琪拉_4b7e阅读 2,273评论 0 6
  • 今天是星期五明天就放假了,心里很高兴,我先写完作业,才回奶奶家,奶奶一定会给我准备好多好吃的,我已经很想奶奶了,我...
    王文哲同学阅读 221评论 0 0
  • 前些天,好多人都在晒着自己曾经芳华十八时的照片。热衷晒的,都是自己的十八岁早已远去、逝去的那些人。真正十八岁的那些...
    七月风阅读 316评论 0 8
  • 今天才有寒冬的感觉,在外面走走,寒气向脸上扑,一会鼻子脸就冰凉冰凉,在多走一段时间,身上不仅没发热后背还凉嗖嗖。看...
    月光影城阅读 128评论 0 1

友情链接更多精彩内容