Spring Boot集成Reactor事件处理框架的简单示例
2024-08-21 01:08:02
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事件,监听器即可接收到相关的事件并处理
最新文章
- 求两条直线相交点 AS3代码
- BZOJ4116 : [Wf2015]Tours
- JS传中文到后台需要的处理
- 部署PDA程序的时候存储不足的解决办法
- 你应该知道的jQuery技巧
- Android 使用 Gmail 来发送邮件
- ArrayList和LinkedList
- virtual box 改变已经创建的虚拟系统分配的硬盘
- linux内核源码阅读之facebook硬盘加速利器flashcache
- Yomob广告在cocos2dx安卓平台的Demo
- Cinnamon桌面是怎么回事儿
- mqtt实现自动监听服务器消息
- IDEA安装教程
- WPF ViewBox中的TextBlock自适应
- error “Device supports x86, but APK only supports armeabi-v7a”
- JS ";trycatch工厂模式";
- sql查询一列 重复的数据
- object Add(object Before, object After, object Count, object Type);
- .Net Entity Framework Core 用 HasColumnType 配置浮点数精度
- Flume-NG源码阅读之Interceptor(原创)
热门文章
- 如何破解IDEA
- AtCoder Grand Contest 011D(思维,规律,异或)
- Shell-4-让文本飞
- Jmeter-逻辑控制器之Foreach
- rest framework认证组件和django自带csrf组件区别详解
- SQL基础(一)
- [POI2009]KAM-Pebbles BZOJ1115 [ 待填坑 ] 博弈
- ERROR (UnicodeEncodeError): &#39;ascii&#39; codec can&#39;t encode character u&#39;\uff08&#39; in position 9: ordinal not in range(128)
- C#中web项目使用log4net日志
- Spring Boot学习资料汇总