1. Reactor简介

Reactor 是 Spring 社区发布的基于事件驱动的异步框架,不仅解耦了程序之间的强调用关系,而且有效提升了系统的多线程并发处理能力。

2. Spring Boot集成Reactor的pom.xml

 <?xml version="1.0" encoding="UTF-8"?>
<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>org.springframework</groupId>
<artifactId>spring-boot-reactor</artifactId>
<version>0.1.0</version> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>0.5.0.M2</version>
</parent> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectreactor</groupId>
<artifactId>reactor-spring</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
</dependencies> <properties>
<start-class>org.springboot.reactor.example.App</start-class>
</properties> <build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> <repositories>
<repository>
<id>spring-snapshots2</id>
<url>http://repo.springsource.org/libs-snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-release</id>
<url>http://repo.springsource.org/libs-release</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<url>http://repo.springsource.org/libs-snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</project>

3. Spring Boot的主程序

 package org.springboot.reactor.example;

 import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; @Configuration
@EnableAutoConfiguration
@ComponentScan
public class App{
public static void main( String[] args ) {
SpringApplication.run(App.class, args);
}
}

4. 领域数据User

 package org.springboot.reactor.example;

 public class User {

 	String firstName;

 	String lastName;

 	String address;

 	String city;

 }
 

5. 实例化Reactor Bean,这里采用内部 Bean 方式实现,其他方式也可以,保证能够供其它类注入即可

 package org.springboot.reactor.example;

 import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import reactor.core.Environment;
import reactor.core.Reactor;
import reactor.core.spec.Reactors; @Configuration
public class Configure {
@Bean
Environment env() {
return new Environment();
} @Bean
Reactor createReactor(Environment env) {
return Reactors.reactor()
.env(env)
.dispatcher(Environment.THREAD_POOL)
.get();
}
}

6. Controller层用于通过reactor.notify发送事件

 package org.springboot.reactor.example;

 import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import reactor.core.Reactor;
import reactor.event.Event; @RestController
@RequestMapping("/api")
public class ReactorController { @Autowired
private Reactor reactor; @RequestMapping("/test")
public void test() throws InterruptedException{
User user = new User();
user.firstName = "Chetan";
user.lastName = "Birajdar";
user.address = "410 S Hauser";
user.city = "Los Angeles";
reactor.notify("eventHandler", Event.wrap(user));
System.out.println("y0, I sent something for you!!");
}
}

7. 事件的监听器,以便于接收发送的事件并处理。需要实现 Consumer<Event<T>> 接口,其中 T 是处理程序接收的数据类型,要根据具体业务设置

 package org.springboot.reactor.example;

 import org.springframework.stereotype.Service;

 import reactor.event.Event;
import reactor.function.Consumer; @Service
public class AppListener implements Consumer<Event<User>>{ public void accept(Event<User> event) {
System.out.println("Received user object with "
+ "first name:"
+ event.getData().firstName
+ ", last name:"
+ event.getData().lastName
+ ", address:"
+ event.getData().address
+ ", city:"
+ event.getData().city
+ "");
}
}

8. ReactorService,实现InitializingBean接口,以便将发送的事件绑定到指定的监听器

 package org.springboot.reactor.example;

 import static reactor.event.selector.Selectors.$;

 import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import reactor.core.Reactor; @Service
public class ReactorService implements InitializingBean{ @Autowired
private Reactor reactor; @Autowired
private AppListener appListener; @Override
public void afterPropertiesSet() throws Exception {
reactor.on($("eventHandler"), appListener);
}
}

9. 当启动应用时,调用/api/test接口,向eventHandler的topic发送User事件,监听器即可接收到相关的事件并处理

最新文章

  1. 求两条直线相交点 AS3代码
  2. BZOJ4116 : [Wf2015]Tours
  3. JS传中文到后台需要的处理
  4. 部署PDA程序的时候存储不足的解决办法
  5. 你应该知道的jQuery技巧
  6. Android 使用 Gmail 来发送邮件
  7. ArrayList和LinkedList
  8. virtual box 改变已经创建的虚拟系统分配的硬盘
  9. linux内核源码阅读之facebook硬盘加速利器flashcache
  10. Yomob广告在cocos2dx安卓平台的Demo
  11. Cinnamon桌面是怎么回事儿
  12. mqtt实现自动监听服务器消息
  13. IDEA安装教程
  14. WPF ViewBox中的TextBlock自适应
  15. error “Device supports x86, but APK only supports armeabi-v7a”
  16. JS &quot;trycatch工厂模式&quot;
  17. sql查询一列 重复的数据
  18. object Add(object Before, object After, object Count, object Type);
  19. .Net Entity Framework Core 用 HasColumnType 配置浮点数精度
  20. Flume-NG源码阅读之Interceptor(原创)

热门文章

  1. 如何破解IDEA
  2. AtCoder Grand Contest 011D(思维,规律,异或)
  3. Shell-4-让文本飞
  4. Jmeter-逻辑控制器之Foreach
  5. rest framework认证组件和django自带csrf组件区别详解
  6. SQL基础(一)
  7. [POI2009]KAM-Pebbles BZOJ1115 [ 待填坑 ] 博弈
  8. ERROR (UnicodeEncodeError): &#39;ascii&#39; codec can&#39;t encode character u&#39;\uff08&#39; in position 9: ordinal not in range(128)
  9. C#中web项目使用log4net日志
  10. Spring Boot学习资料汇总