EventBus
Posted rongfengliang-荣锋亮
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了EventBus相关的知识,希望对你有一定的参考价值。
1. 场景
减少业务处理的复杂性
2. 使用基于spring boot
简单,便捷
3. 项目创建基于maven
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.dalong.eventbus</groupId>
<artifactId>event</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>20.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version><!--$NO-MVN-MAN-VER$-->
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
4. 代码编写
spring boot 启动类
package event;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import com.google.common.eventbus.EventBus;
@SpringBootApplication
public class Application {
@Bean
public EventBus eventBus(){
return new EventBus("test");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
SpringApplication.run(Application.class, args);
}
}
5. Event 创建
package event;
import org.springframework.stereotype.Component;
import com.google.common.eventbus.Subscribe;
@Component
public class MultipleListener {
public Integer lastInteger;
public Long lastLong;
@Subscribe
public void listenInteger(Integer event) {
lastInteger = event;
System.out.println("event Integer:"+lastInteger);
}
@Subscribe
public void listenLong(Long event) {
lastLong = event;
System.out.println("event Long:"+lastLong);
}
public Integer getLastInteger() {
return lastInteger;
}
public Long getLastLong() {
return lastLong;
}
}
6. Event 事件绑定
package event;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.google.common.eventbus.EventBus;
@Component
public class EventRegister {
@Autowired
public EventRegister(EventBus eventBus,MultipleListener listener) {
// TODO Auto-generated constructor stub
eventBus.register(listener);
}
}
7. 使用
package event;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.google.common.collect.Range;
import com.google.common.eventbus.EventBus;
@RestController
public class ServiceController {
@Autowired
public EventBus eventBus;
@Autowired
public MultipleListener listener;
@RequestMapping("/list")
public Object list() {
Result result = new Result();
eventBus.post(new TestEvent(200));
result.setCode(11);
result.setMess("is null");
eventBus.post(new Integer(100));
eventBus.post(new Integer(200));
eventBus.post(new Integer(300));
eventBus.post(new Long(800));
eventBus.post(new Long(800990));
eventBus.post(new Long(800882934));
result.setData(listener.getLastLong());
System.out.println(Range.closed(3, 5).span(Range.open(5, 10)).toString());
System.out.println("LastInteger:" + listener.getLastInteger());
System.out.println("LastLong:" + listener.getLastLong());
return result;
}
}
8. 参考说明
https://github.com/google/guava
以上是关于EventBus的主要内容,如果未能解决你的问题,请参考以下文章