JAVA-14288 Rename spring-5-reactive-modules to spring-reactive-modules (#12659)
* JAVA-14288 Rename spring-5-reactive-modules to spring-reactive-modules * JAVA-14288 Remove failing module * JAVA-14288 Revert commenting spring-cloud-openfeign-2 module
This commit is contained in:
12
spring-reactive-modules/spring-5-reactive-3/.gitignore
vendored
Normal file
12
spring-reactive-modules/spring-5-reactive-3/.gitignore
vendored
Normal file
@@ -0,0 +1,12 @@
|
||||
#folders#
|
||||
.idea
|
||||
/target
|
||||
/neoDb*
|
||||
/data
|
||||
/src/main/webapp/WEB-INF/classes
|
||||
*/META-INF/*
|
||||
|
||||
# Packaged files #
|
||||
*.jar
|
||||
*.war
|
||||
*.ear
|
||||
7
spring-reactive-modules/spring-5-reactive-3/README.md
Normal file
7
spring-reactive-modules/spring-5-reactive-3/README.md
Normal file
@@ -0,0 +1,7 @@
|
||||
## Spring 5 Reactive Project
|
||||
|
||||
This module contains articles about reactive Spring 5.
|
||||
|
||||
- [Logging a Reactive Sequence](https://www.baeldung.com/spring-reactive-sequence-logging)
|
||||
- [Reading Flux Into a Single InputStream Using Spring Reactive WebClient](https://www.baeldung.com/spring-reactive-read-flux-into-inputstream)
|
||||
- More articles: [[<-- prev]](../spring-5-reactive-2)
|
||||
44
spring-reactive-modules/spring-5-reactive-3/pom.xml
Normal file
44
spring-reactive-modules/spring-5-reactive-3/pom.xml
Normal file
@@ -0,0 +1,44 @@
|
||||
<?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>
|
||||
<artifactId>spring-5-reactive-3</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>spring-5-reactive-3</name>
|
||||
<packaging>jar</packaging>
|
||||
<description>spring 5 sample project about new features</description>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung.spring.reactive</groupId>
|
||||
<artifactId>spring-reactive-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-webflux</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectreactor</groupId>
|
||||
<artifactId>reactor-spring</artifactId>
|
||||
<version>${reactor-spring.version}</version>
|
||||
</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>
|
||||
|
||||
<properties>
|
||||
<reactor-spring.version>1.0.1.RELEASE</reactor-spring.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,94 @@
|
||||
package com.baeldung.databuffer;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.core.io.buffer.DataBuffer;
|
||||
import org.springframework.core.io.buffer.DataBufferUtils;
|
||||
import org.springframework.web.reactive.function.BodyExtractors;
|
||||
import org.springframework.web.reactive.function.client.WebClient;
|
||||
|
||||
import reactor.core.publisher.Flux;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.PipedInputStream;
|
||||
import java.io.PipedOutputStream;
|
||||
|
||||
public class DataBufferToInputStream {
|
||||
private static final Logger logger = LoggerFactory.getLogger(DataBufferToInputStream.class);
|
||||
private static final String REQUEST_ENDPOINT = "https://gorest.co.in/public/v2/users";
|
||||
|
||||
private static WebClient getWebClient() {
|
||||
WebClient.Builder webClientBuilder = WebClient.builder();
|
||||
return webClientBuilder.build();
|
||||
}
|
||||
|
||||
public static InputStream getResponseAsInputStream(WebClient client, String url) throws IOException, InterruptedException {
|
||||
|
||||
PipedOutputStream pipedOutputStream = new PipedOutputStream();
|
||||
PipedInputStream pipedInputStream = new PipedInputStream(1024 * 10);
|
||||
pipedInputStream.connect(pipedOutputStream);
|
||||
|
||||
Flux<DataBuffer> body = client.get()
|
||||
.uri(url)
|
||||
.exchangeToFlux(clientResponse -> {
|
||||
return clientResponse.body(BodyExtractors.toDataBuffers());
|
||||
})
|
||||
.doOnError(error -> {
|
||||
logger.error("error occurred while reading body", error);
|
||||
})
|
||||
.doFinally(s -> {
|
||||
try {
|
||||
pipedOutputStream.close();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
})
|
||||
.doOnCancel(() -> {
|
||||
logger.error("Get request is cancelled");
|
||||
});
|
||||
|
||||
DataBufferUtils.write(body, pipedOutputStream)
|
||||
.log("Writing to output buffer")
|
||||
.subscribe();
|
||||
return pipedInputStream;
|
||||
}
|
||||
|
||||
private static String readContentFromPipedInputStream(PipedInputStream stream) throws IOException {
|
||||
StringBuffer contentStringBuffer = new StringBuffer();
|
||||
try {
|
||||
Thread pipeReader = new Thread(() -> {
|
||||
try {
|
||||
contentStringBuffer.append(readContent(stream));
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
});
|
||||
pipeReader.start();
|
||||
pipeReader.join();
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
} finally {
|
||||
stream.close();
|
||||
}
|
||||
|
||||
return String.valueOf(contentStringBuffer);
|
||||
}
|
||||
|
||||
private static String readContent(InputStream stream) throws IOException {
|
||||
StringBuffer contentStringBuffer = new StringBuffer();
|
||||
byte[] tmp = new byte[stream.available()];
|
||||
int byteCount = stream.read(tmp, 0, tmp.length);
|
||||
logger.info(String.format("read %d bytes from the stream\n", byteCount));
|
||||
contentStringBuffer.append(new String(tmp));
|
||||
return String.valueOf(contentStringBuffer);
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws IOException, InterruptedException {
|
||||
WebClient webClient = getWebClient();
|
||||
InputStream inputStream = getResponseAsInputStream(webClient, REQUEST_ENDPOINT);
|
||||
Thread.sleep(3000);
|
||||
String content = readContentFromPipedInputStream((PipedInputStream) inputStream);
|
||||
logger.info("response content: \n{}", content.replace("}", "}\n"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.webflux.logging;
|
||||
|
||||
import reactor.core.publisher.Flux;
|
||||
|
||||
public class WebFluxLoggingExample {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Flux<Integer> reactiveStream = Flux.range(1, 5).log();
|
||||
|
||||
reactiveStream.subscribe();
|
||||
|
||||
reactiveStream = Flux.range(1, 5).log().take(3);
|
||||
|
||||
reactiveStream.subscribe();
|
||||
|
||||
reactiveStream = Flux.range(1, 5).take(3).log();
|
||||
|
||||
reactiveStream.subscribe();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
# application properties
|
||||
@@ -0,0 +1,77 @@
|
||||
package databuffer;
|
||||
|
||||
import com.baeldung.databuffer.DataBufferToInputStream;
|
||||
|
||||
import io.restassured.internal.util.IOUtils;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mockito;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.reactive.function.client.ClientResponse;
|
||||
import org.springframework.web.reactive.function.client.ExchangeFunction;
|
||||
import org.springframework.web.reactive.function.client.WebClient;
|
||||
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||
|
||||
class DataBufferToInputStreamUnitTest {
|
||||
private String getResponseStub() throws IOException {
|
||||
InputStream inputStream = null;
|
||||
BufferedReader reader = null;
|
||||
String content = null;
|
||||
try {
|
||||
inputStream = this.getClass()
|
||||
.getClassLoader()
|
||||
.getResourceAsStream("user-response.json");
|
||||
if (inputStream != null) {
|
||||
reader = new BufferedReader(new InputStreamReader(inputStream));
|
||||
content = reader.lines()
|
||||
.collect(Collectors.joining(System.lineSeparator()));
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
throw new RuntimeException("exception caught while getting response stub");
|
||||
} finally {
|
||||
reader.close();
|
||||
inputStream.close();
|
||||
}
|
||||
return content;
|
||||
}
|
||||
|
||||
private InputStream getResponseStubAsInputStream() {
|
||||
return this.getClass()
|
||||
.getClassLoader()
|
||||
.getResourceAsStream("user-response.json");
|
||||
}
|
||||
|
||||
private WebClient getMockWebClient() throws IOException {
|
||||
String content = getResponseStub();
|
||||
ClientResponse clientResponse = ClientResponse.create(HttpStatus.OK)
|
||||
.header("Content-Type", "application/json")
|
||||
.body(content)
|
||||
.build();
|
||||
|
||||
ExchangeFunction exchangeFunction = clientRequest -> Mono.just(clientResponse);
|
||||
|
||||
WebClient.Builder webClientBuilder = WebClient.builder()
|
||||
.exchangeFunction(exchangeFunction);
|
||||
WebClient webClient = webClientBuilder.build();
|
||||
return webClient;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testResponseAsInputStream() throws IOException, InterruptedException {
|
||||
String mockUrl = Mockito.anyString();
|
||||
WebClient mockWebClient = getMockWebClient();
|
||||
InputStream inputStream = DataBufferToInputStream.getResponseAsInputStream(mockWebClient, mockUrl);
|
||||
byte[] expectedBytes = IOUtils.toByteArray(getResponseStubAsInputStream());
|
||||
byte[] actualBytes = IOUtils.toByteArray(inputStream);
|
||||
assertArrayEquals(expectedBytes, actualBytes);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<include resource="org/springframework/boot/logging/logback/base.xml" />
|
||||
|
||||
<root level="info">
|
||||
<appender-ref ref="CONSOLE" />
|
||||
</root>
|
||||
</configuration>
|
||||
@@ -0,0 +1,72 @@
|
||||
[
|
||||
{
|
||||
"id": 2683,
|
||||
"name": "Maheswar Kocchar",
|
||||
"email": "maheswar_kocchar@kihn.info",
|
||||
"gender": "male",
|
||||
"status": "active"
|
||||
},
|
||||
{
|
||||
"id": 2680,
|
||||
"name": "Lakshminath Khan",
|
||||
"email": "lakshminath_khan@barrows-cormier.biz",
|
||||
"gender": "female",
|
||||
"status": "inactive"
|
||||
},
|
||||
{
|
||||
"id": 2679,
|
||||
"name": "Tarun Arora",
|
||||
"email": "tarun_arora@rolfson.net",
|
||||
"gender": "female",
|
||||
"status": "inactive"
|
||||
},
|
||||
{
|
||||
"id": 2678,
|
||||
"name": "Agnivesh Dubashi",
|
||||
"email": "dubashi_agnivesh@senger.name",
|
||||
"gender": "male",
|
||||
"status": "inactive"
|
||||
},
|
||||
{
|
||||
"id": 2677,
|
||||
"name": "Dhanu Gowda",
|
||||
"email": "gowda_dhanu@hayes.org",
|
||||
"gender": "male",
|
||||
"status": "active"
|
||||
},
|
||||
{
|
||||
"id": 2675,
|
||||
"name": "Harinakshi Pilla Jr.",
|
||||
"email": "pilla_jr_harinakshi@rutherford-monahan.com",
|
||||
"gender": "female",
|
||||
"status": "inactive"
|
||||
},
|
||||
{
|
||||
"id": 2673,
|
||||
"name": "Kalpana Prajapat",
|
||||
"email": "prajapat_kalpana@wilkinson-schaefer.net",
|
||||
"gender": "female",
|
||||
"status": "active"
|
||||
},
|
||||
{
|
||||
"id": 2672,
|
||||
"name": "Chakradhar Jha",
|
||||
"email": "jha_chakradhar@baumbach.info",
|
||||
"gender": "male",
|
||||
"status": "active"
|
||||
},
|
||||
{
|
||||
"id": 2670,
|
||||
"name": "Divaakar Deshpande Jr.",
|
||||
"email": "deshpande_jr_divaakar@mertz.info",
|
||||
"gender": "female",
|
||||
"status": "inactive"
|
||||
},
|
||||
{
|
||||
"id": 2669,
|
||||
"name": "Prasanna Mehra",
|
||||
"email": "prasanna_mehra@ruecker-larkin.name",
|
||||
"gender": "female",
|
||||
"status": "active"
|
||||
}
|
||||
]
|
||||
Reference in New Issue
Block a user