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

   

posted on   荣锋亮  阅读(385)  评论(0编辑  收藏  举报

编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2014-12-22 OLEDB操作Excel
2013-12-22 Where is Silverlight now?
2013-12-22 A glance at C# vNext

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示