Guava之EventBus实战

EventBus是Guava的事件处理机制,是设计模式中的观察者模式(生产/消费者编程模型)的优雅实现。对于事件监听和发布订阅模式,EventBus是一个非常优雅和简单解决方案,我们不用创建复杂的类和接口层次结构。

定义EventBus

@Configuration
public class EventBusConfig {

    @Autowired
    private ApplicationContext context;

    /**
     * 功能描述: 
* ConditionalOnMissingBean判断是否执行初始化代码,即如果用户已经创建bean,则相关的初始化代码不再执行 */ @Bean @ConditionalOnMissingBean(AsyncEventBus.class) AsyncEventBus createEventBus() { // 定义一个EventBus对象 AsyncEventBus eventBus = new AsyncEventBus(Executors.newFixedThreadPool(5)); Reflections reflections = new Reflections("com.google.guava.listener", new MethodAnnotationsScanner()); Set methods = reflections.getMethodsAnnotatedWith(Subscribe.class); if (null != methods) { for (Method method : methods) { try { System.out.println("注册监听器:" + context.getBean(method.getDeclaringClass())); eventBus.register(context.getBean(method.getDeclaringClass())); } catch (Exception e) { e.printStackTrace(); } } } return eventBus; } }

定义Event

public interface IEvent {
    void print();
}

public class LoginEvent implements IEvent{
    @Override
    public void print() {
        System.out.println("login ......");
    }
}

public class LogoutEvent implements IEvent {
    @Override
    public void print() {
        System.out.println("logout ......");
    }
}

public class BusinessEvent implements IEvent{
    @Override
    public void print() {
        System.out.println("business ......");
    }
}

定义Listener

public interface IEventListener {
    void login(LoginEvent event);
    void business(BusinessEvent event);
    void logout(LogoutEvent event);
}

/**
 * 同一个监听者可以监听多种类型的事件
 */
@Component
public class LoginEventListener extends EventListenerAdapter {

    @Subscribe
    @Override
    public void login(LoginEvent event) {
        System.out.println("login receive event ...");
        event.print();
    }

    @Subscribe
    @Override
    public void business(BusinessEvent event) {
        System.out.println("business receive event ...");
        event.print();
    }
}

/**
 * 同一个监听器多次监听同一个事件
 */
@Component
public class LogoutEventListener extends EventListenerAdapter{

    @Subscribe
    @Override
    public void logout(LogoutEvent event) {
        System.out.println("logout receive event ...");
        event.print();
    }

    @Subscribe
    public void logoutT(LogoutEvent event) {
        System.out.println("logoutT receive event ...");
        event.print();
    }
}


public abstract class EventListenerAdapter implements IEventListener{

    @Override
    public void login(LoginEvent event) {
    }

    @Override
    public void business(BusinessEvent enent) {
    }

    @Override
    public void logout(LogoutEvent event) {
    }
}

定义事件发布接口

public interface IBusinessService {
    void post(IEvent event);
}

@Service
public class BusinessServiceImpl implements IBusinessService {

    @Autowired
    private AsyncEventBus eventBus;

    @Override
    public void post(IEvent event) {
        if (null != event) {
            // 事件发布
            eventBus.post(event);
        }
    }
}

启动触发事件

@Component
public class StartRunner implements CommandLineRunner {

    @Autowired
    private IBusinessService businessService;

    @Override
    public void run(String... args) throws Exception {
        IEvent loginEvent = new LoginEvent();
        businessService.post(loginEvent);
        IEvent logoutEvent = new LogoutEvent();
        businessService.post(logoutEvent);
        IEvent businessEvent = new BusinessEvent();
        businessService.post(businessEvent);
    }
}

启动类

@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})
public class EventBusApplication {

    public static void main(String[] args) {
        try {
            SpringApplication.run(EventBusApplication.class, args);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

原理分析
https://www.jianshu.com/p/4bddd45a8e7a

你可能感兴趣的:(阻塞队列)