Spring Boot集成Reactor事件处理框架的简单示例

时间:2023-03-10 02:28:45
Spring Boot集成Reactor事件处理框架的简单示例

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事件,监听器即可接收到相关的事件并处理