<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>
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;@SpringBootApplicationpublic 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); }}
package event;import org.springframework.stereotype.Component;import com.google.common.eventbus.Subscribe;@Componentpublic 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; } }
package event;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;import com.google.common.eventbus.EventBus;@Componentpublic class EventRegister { @Autowired public EventRegister(EventBus eventBus,MultipleListener listener) { // TODO Auto-generated constructor stub eventBus.register(listener); } }
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;@RestControllerpublic 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; } }