Created
February 13, 2018 14:49
-
-
Save IvanZelenskyy/7a54090cc850883c67a4d314a8ea5c97 to your computer and use it in GitHub Desktop.
Spring boot webflux test1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.demo; | |
import lombok.AllArgsConstructor; | |
import lombok.Data; | |
import org.reactivestreams.Processor; | |
import org.reactivestreams.Publisher; | |
import org.reactivestreams.Subscriber; | |
import org.reactivestreams.Subscription; | |
import org.springframework.boot.SpringApplication; | |
import org.springframework.boot.autoconfigure.SpringBootApplication; | |
import org.springframework.context.ApplicationContext; | |
import org.springframework.http.MediaType; | |
import org.springframework.http.server.reactive.HttpHandler; | |
import org.springframework.http.server.reactive.ReactorHttpHandlerAdapter; | |
import org.springframework.web.reactive.function.server.*; | |
import reactor.core.publisher.Flux; | |
import reactor.core.publisher.Mono; | |
import reactor.core.publisher.ReplayProcessor; | |
import reactor.core.publisher.TopicProcessor; | |
import reactor.ipc.netty.http.server.HttpServer; | |
import java.util.Optional; | |
import static org.springframework.web.reactive.function.server.RequestPredicates.*; | |
import static org.springframework.web.reactive.function.BodyInserters.*; | |
import static org.springframework.web.reactive.function.server.RouterFunctions.*; | |
//@SpringBootApplication | |
public class DemoApplication { | |
public static void main(String[] args) throws InterruptedException { | |
// ApplicationContext ctx = SpringApplication.run(DemoApplication.class, args); | |
ReplayProcessor<ResponseData> processor = ReplayProcessor.create();//"words", 1024); | |
; | |
Flux<ResponseData> allFromProcessor = Flux.from(processor).cache(); | |
HandlerFunction words = request -> { | |
return ServerResponse.ok() | |
.contentType(MediaType.APPLICATION_STREAM_JSON) | |
.body(processor.replay().autoConnect(), ResponseData.class); | |
}; | |
HandlerFunction echo = request -> { | |
Optional<String> said = request.queryParam("phrase"); | |
said.ifPresent(s->processor.onNext(new ResponseData(s))); | |
return ServerResponse.ok().body(fromObject(new ResponseData(request.queryParam("phrase").orElse(null)))); | |
}; | |
// HandlerFunction words = request -> { | |
// return ServerResponse.ok().body(processor, ResponseData.class); | |
// }; | |
RouterFunction router = route(GET("/"), request -> ServerResponse.notFound().build()) | |
.and(route(GET("/say"), echo)) | |
.and(route(GET("/words"),words)); | |
HttpHandler httpHandler = RouterFunctions.toHttpHandler(router); | |
HttpServer | |
.create("localhost", 8081) | |
.newHandler(new ReactorHttpHandlerAdapter(httpHandler)) | |
.block(); | |
Thread.currentThread().join(); | |
} | |
} | |
@Data | |
@AllArgsConstructor | |
class ResponseData{ | |
String s; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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>com.example</groupId> | |
<artifactId>demo</artifactId> | |
<version>0.0.1-SNAPSHOT</version> | |
<packaging>jar</packaging> | |
<name>demo</name> | |
<description>Demo project for Spring Boot</description> | |
<parent> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-parent</artifactId> | |
<version>2.0.0.RC1</version> | |
<relativePath/> <!-- lookup parent from repository --> | |
</parent> | |
<properties> | |
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> | |
<java.version>1.8</java.version> | |
</properties> | |
<dependencies> | |
<!--<dependency>--> | |
<!--<groupId>org.springframework.boot</groupId>--> | |
<!--<artifactId>spring-boot-starter-integration</artifactId>--> | |
<!--</dependency>--> | |
<dependency> | |
<groupId>org.projectlombok</groupId> | |
<artifactId>lombok</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-webflux</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-test</artifactId> | |
<scope>test</scope> | |
</dependency> | |
<dependency> | |
<groupId>io.projectreactor</groupId> | |
<artifactId>reactor-test</artifactId> | |
<scope>test</scope> | |
</dependency> | |
</dependencies> | |
<build> | |
<plugins> | |
<plugin> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-maven-plugin</artifactId> | |
</plugin> | |
</plugins> | |
</build> | |
<repositories> | |
<repository> | |
<id>spring-snapshots</id> | |
<name>Spring Snapshots</name> | |
<url>https://repo.spring.io/snapshot</url> | |
<snapshots> | |
<enabled>true</enabled> | |
</snapshots> | |
</repository> | |
<repository> | |
<id>spring-milestones</id> | |
<name>Spring Milestones</name> | |
<url>https://repo.spring.io/milestone</url> | |
<snapshots> | |
<enabled>false</enabled> | |
</snapshots> | |
</repository> | |
</repositories> | |
<pluginRepositories> | |
<pluginRepository> | |
<id>spring-snapshots</id> | |
<name>Spring Snapshots</name> | |
<url>https://repo.spring.io/snapshot</url> | |
<snapshots> | |
<enabled>true</enabled> | |
</snapshots> | |
</pluginRepository> | |
<pluginRepository> | |
<id>spring-milestones</id> | |
<name>Spring Milestones</name> | |
<url>https://repo.spring.io/milestone</url> | |
<snapshots> | |
<enabled>false</enabled> | |
</snapshots> | |
</pluginRepository> | |
</pluginRepositories> | |
</project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment