首页 > 代码库 > EventBus
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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。