EventBus

Posted rongfengliang-荣锋亮

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了EventBus相关的知识,希望对你有一定的参考价值。

1. 场景

    减少业务处理的复杂性

2. 使用基于spring boot  
  
    简单,便捷

3.  项目创建基于maven

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>com.dalong.eventbus</groupId>
  5. <artifactId>event</artifactId>
  6. <version>0.0.1-SNAPSHOT</version>
  7. <parent>
  8. <groupId>org.springframework.boot</groupId>
  9. <artifactId>spring-boot-starter-parent</artifactId>
  10. <version>1.4.2.RELEASE</version>
  11. </parent>
  12. <dependencies>
  13. <dependency>
  14. <groupId>com.google.guava</groupId>
  15. <artifactId>guava</artifactId>
  16. <version>20.0</version>
  17. </dependency>
  18. <dependency>
  19. <groupId>org.springframework.boot</groupId>
  20. <artifactId>spring-boot-starter-web</artifactId>
  21. </dependency>
  22. </dependencies>
  23. <build>
  24. <plugins>
  25. <plugin>
  26. <artifactId>maven-compiler-plugin</artifactId>
  27. <version>3.1</version><!--$NO-MVN-MAN-VER$-->
  28. <configuration>
  29. <source>1.8</source>
  30. <target>1.8</target>
  31. </configuration>
  32. </plugin>
  33. </plugins>
  34. </build>
  35. </project>
4. 代码编写

  spring  boot  启动类
  
  1. package event;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.context.annotation.Bean;
  5. import com.google.common.eventbus.EventBus;
  6. @SpringBootApplication
  7. public class Application {
  8. @Bean
  9. public EventBus eventBus(){
  10. return new EventBus("test");
  11. }
  12. public static void main(String[] args) {
  13. // TODO Auto-generated method stub
  14. SpringApplication.run(Application.class, args);
  15. }
  16. }
5.  Event 创建

   
  1. package event;
  2. import org.springframework.stereotype.Component;
  3. import com.google.common.eventbus.Subscribe;
  4. @Component
  5. public class MultipleListener {
  6. public Integer lastInteger;
  7. public Long lastLong;
  8. @Subscribe
  9. public void listenInteger(Integer event) {
  10. lastInteger = event;
  11. System.out.println("event Integer:"+lastInteger);
  12. }
  13. @Subscribe
  14. public void listenLong(Long event) {
  15. lastLong = event;
  16. System.out.println("event Long:"+lastLong);
  17. }
  18. public Integer getLastInteger() {
  19. return lastInteger;
  20. }
  21. public Long getLastLong() {
  22. return lastLong;
  23. }
  24. }

6. Event 事件绑定

    
  1. package event;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.stereotype.Component;
  4. import com.google.common.eventbus.EventBus;
  5. @Component
  6. public class EventRegister {
  7. @Autowired
  8. public EventRegister(EventBus eventBus,MultipleListener listener) {
  9. // TODO Auto-generated constructor stub
  10. eventBus.register(listener);
  11. }
  12. }
7. 使用

   
  1. package event;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.web.bind.annotation.RequestMapping;
  4. import org.springframework.web.bind.annotation.RestController;
  5. import com.google.common.collect.Range;
  6. import com.google.common.eventbus.EventBus;
  7. @RestController
  8. public class ServiceController {
  9. @Autowired
  10. public EventBus eventBus;
  11. @Autowired
  12. public MultipleListener listener;
  13. @RequestMapping("/list")
  14. public Object list() {
  15. Result result = new Result();
  16. eventBus.post(new TestEvent(200));
  17. result.setCode(11);
  18. result.setMess("is null");
  19. eventBus.post(new Integer(100));
  20. eventBus.post(new Integer(200));
  21. eventBus.post(new Integer(300));
  22. eventBus.post(new Long(800));
  23. eventBus.post(new Long(800990));
  24. eventBus.post(new Long(800882934));
  25. result.setData(listener.getLastLong());
  26. System.out.println(Range.closed(3, 5).span(Range.open(5, 10)).toString());
  27. System.out.println("LastInteger:" + listener.getLastInteger());
  28. System.out.println("LastLong:" + listener.getLastLong());
  29. return result;
  30. }
  31. }
8. 参考说明
    https://github.com/google/guava

   

以上是关于EventBus的主要内容,如果未能解决你的问题,请参考以下文章

EventBus 知识点总结

EventBus原理以及源代码分析

源码解析-EventBus

当EventBus遇上RxJava

EventBus使用详解——初步使用EventBus

EventBus3.0源码解析