Skip to content

Commit b7d4a00

Browse files
committed
A simple Real Time streaming example with Spring Webflux.
1. Added an API to generate a real time stream returning numbers. 2. Added Test case to consume that API with a webTestClient. 3. Added a client to consume that API, over the network.
1 parent add380d commit b7d4a00

8 files changed

Lines changed: 258 additions & 0 deletions

File tree

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.springboot</groupId>
8+
<artifactId>sample</artifactId>
9+
<version>0.0.1-SNAPSHOT</version>
10+
<packaging>jar</packaging>
11+
12+
<name>client</name>
13+
<description>SpringFlux Sample Client</description>
14+
15+
<parent>
16+
<groupId>org.springframework.boot</groupId>
17+
<artifactId>spring-boot-starter-parent</artifactId>
18+
<version>2.0.3.RELEASE</version>
19+
<relativePath />
20+
</parent>
21+
22+
<properties>
23+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
24+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
25+
<java.version>1.8</java.version>
26+
</properties>
27+
28+
<dependencies>
29+
<dependency>
30+
<groupId>org.springframework.boot</groupId>
31+
<artifactId>spring-boot-starter-webflux</artifactId>
32+
</dependency>
33+
34+
</dependencies>
35+
36+
<build>
37+
<plugins>
38+
<plugin>
39+
<groupId>org.springframework.boot</groupId>
40+
<artifactId>spring-boot-maven-plugin</artifactId>
41+
<executions>
42+
<execution>
43+
<goals>
44+
<goal>repackage</goal>
45+
</goals>
46+
</execution>
47+
</executions>
48+
<configuration>
49+
<arguments>
50+
<argument>--spring.profiles.active=dev</argument>
51+
</arguments>
52+
</configuration>
53+
</plugin>
54+
</plugins>
55+
</build>
56+
57+
<repositories>
58+
<repository>
59+
<id>spring-releases</id>
60+
<url>https://repo.spring.io/libs-release</url>
61+
</repository>
62+
</repositories>
63+
<pluginRepositories>
64+
<pluginRepository>
65+
<id>spring-releases</id>
66+
<url>https://repo.spring.io/libs-release</url>
67+
</pluginRepository>
68+
</pluginRepositories>
69+
70+
</project>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.springwebflux.sample;
2+
3+
import org.springframework.boot.autoconfigure.SpringBootApplication;
4+
import org.springframework.scheduling.annotation.EnableAsync;
5+
import org.springframework.web.reactive.function.client.WebClient;
6+
7+
/**
8+
* @author ranjeetkaur
9+
*
10+
*/
11+
@SpringBootApplication(scanBasePackages = "com.springwebflux.*")
12+
@EnableAsync
13+
public class Client {
14+
15+
public static void main(String[] args) throws InterruptedException {
16+
17+
WebClient webClient = WebClient.builder()
18+
.baseUrl("http://localhost:8090")
19+
.build();
20+
21+
webClient.get()
22+
.uri("/v1/dice")
23+
.retrieve()
24+
.bodyToFlux(Integer.class)
25+
.log();
26+
27+
Thread.sleep(10000);
28+
}
29+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
server.port =8091
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.springboot</groupId>
8+
<artifactId>sample</artifactId>
9+
<version>0.0.1-SNAPSHOT</version>
10+
<packaging>jar</packaging>
11+
12+
<name>webflux-server</name>
13+
<description>SpringFlux Sample Server</description>
14+
15+
<parent>
16+
<groupId>org.springframework.boot</groupId>
17+
<artifactId>spring-boot-starter-parent</artifactId>
18+
<version>2.0.3.RELEASE</version>
19+
<relativePath />
20+
</parent>
21+
22+
<properties>
23+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
24+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
25+
<java.version>1.8</java.version>
26+
</properties>
27+
28+
<dependencies>
29+
<dependency>
30+
<groupId>org.springframework.boot</groupId>
31+
<artifactId>spring-boot-starter-webflux</artifactId>
32+
</dependency>
33+
34+
<dependency>
35+
<groupId>org.springframework.boot</groupId>
36+
<artifactId>spring-boot-starter-test</artifactId>
37+
<scope>test</scope>
38+
</dependency>
39+
40+
</dependencies>
41+
42+
<build>
43+
<plugins>
44+
<plugin>
45+
<groupId>org.springframework.boot</groupId>
46+
<artifactId>spring-boot-maven-plugin</artifactId>
47+
<executions>
48+
<execution>
49+
<goals>
50+
<goal>repackage</goal>
51+
</goals>
52+
</execution>
53+
</executions>
54+
<configuration>
55+
<arguments>
56+
<argument>--spring.profiles.active=dev</argument>
57+
</arguments>
58+
</configuration>
59+
</plugin>
60+
</plugins>
61+
</build>
62+
63+
<repositories>
64+
<repository>
65+
<id>spring-releases</id>
66+
<url>https://repo.spring.io/libs-release</url>
67+
</repository>
68+
</repositories>
69+
<pluginRepositories>
70+
<pluginRepository>
71+
<id>spring-releases</id>
72+
<url>https://repo.spring.io/libs-release</url>
73+
</pluginRepository>
74+
</pluginRepositories>
75+
76+
</project>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.springwebflux.controller;
2+
3+
import java.time.Duration;
4+
import java.util.Random;
5+
6+
import org.springframework.web.bind.annotation.GetMapping;
7+
import org.springframework.web.bind.annotation.RequestMapping;
8+
import org.springframework.web.bind.annotation.RestController;
9+
10+
import reactor.core.publisher.Flux;
11+
12+
/**
13+
* @author ranjeetkaur
14+
*
15+
*/
16+
@RestController
17+
@RequestMapping(value = "/v1")
18+
public class Controller {
19+
20+
private static Random random = new Random();
21+
22+
@GetMapping("/dice")
23+
public Flux<Integer> rollDice() {
24+
return Flux.interval(Duration.ofSeconds(1))
25+
.map(pulse -> random.nextInt(5) + 1);
26+
}
27+
28+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.springwebflux.sample;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
/**
7+
* @author ranjeetkaur
8+
*
9+
*/
10+
@SpringBootApplication(scanBasePackages = "com.springwebflux.*")
11+
public class Application {
12+
13+
public static void main(String[] args) {
14+
15+
SpringApplication.run(Application.class, args);
16+
}
17+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
server.port = 8090
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.springwebflux.sample;
2+
3+
import java.time.Duration;
4+
5+
import org.junit.Before;
6+
import org.junit.Test;
7+
import org.junit.runner.RunWith;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.boot.test.context.SpringBootTest;
10+
import org.springframework.test.context.junit4.SpringRunner;
11+
import org.springframework.test.web.reactive.server.WebTestClient;
12+
13+
@RunWith(SpringRunner.class)
14+
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
15+
public class ApplicationTests {
16+
17+
@Autowired
18+
private WebTestClient webTestClient;
19+
20+
@Before
21+
private void setUp() {
22+
webTestClient = webTestClient.mutate()
23+
.responseTimeout(Duration.ofMillis(10000))
24+
.build();
25+
}
26+
27+
@Test
28+
public void rollDice() throws InterruptedException {
29+
webTestClient.get()
30+
.uri("/v1/dice")
31+
.exchange()
32+
.expectStatus()
33+
.isOk()
34+
.expectBodyList(Integer.class);
35+
}
36+
}

0 commit comments

Comments
 (0)