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 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 getContent();
}

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

1、定义login事件

public class LoginEvent implements Event {

    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));

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