Skip to content

Commit 21527b3

Browse files
committed
Added event streaming example with WebFlux.
1 parent 4b9154b commit 21527b3

5 files changed

Lines changed: 129 additions & 0 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.baeldung.reactive.eventstreaming.client;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
import org.springframework.boot.ApplicationRunner;
6+
import org.springframework.context.annotation.Bean;
7+
import org.springframework.context.annotation.Configuration;
8+
import org.springframework.web.reactive.function.client.WebClient;
9+
10+
import com.baeldung.reactive.eventstreaming.model.Notification;
11+
12+
@Configuration
13+
public class NotificationWebClient {
14+
15+
Logger logger = LoggerFactory.getLogger(NotificationWebClient.class);
16+
17+
@Bean
18+
WebClient client() {
19+
return WebClient.create("http://localhost:8080");
20+
}
21+
22+
@Bean
23+
ApplicationRunner runner(WebClient webClient) {
24+
return args -> webClient.get()
25+
.uri("/stream-notifications")
26+
.retrieve()
27+
.bodyToFlux(Notification.class)
28+
.subscribe(data -> logger.info(data.toString()));
29+
}
30+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.baeldung.reactive.eventstreaming.endpoint;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.http.MediaType;
5+
import org.springframework.stereotype.Component;
6+
import org.springframework.web.reactive.function.server.ServerRequest;
7+
import org.springframework.web.reactive.function.server.ServerResponse;
8+
9+
import com.baeldung.reactive.eventstreaming.model.Notification;
10+
import com.baeldung.reactive.eventstreaming.service.NotificationService;
11+
12+
import reactor.core.publisher.Mono;
13+
14+
@Component
15+
public class RequestHandler {
16+
17+
@Autowired
18+
private NotificationService notificationService;
19+
20+
public Mono<ServerResponse> streamNotification(ServerRequest request) {
21+
22+
return ServerResponse.ok()
23+
.contentType(MediaType.TEXT_EVENT_STREAM)
24+
.body(notificationService
25+
.streamNotification(), Notification.class);
26+
}
27+
28+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.reactive.eventstreaming.endpoint;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.web.reactive.function.server.RequestPredicates;
6+
import org.springframework.web.reactive.function.server.RouterFunction;
7+
import org.springframework.web.reactive.function.server.RouterFunctions;
8+
9+
@Configuration
10+
public class RequestRouter {
11+
12+
@Bean
13+
RouterFunction<?> routes(RequestHandler requestHandler) {
14+
return RouterFunctions
15+
.route(RequestPredicates
16+
.GET("/stream-notifications"),
17+
requestHandler::streamNotification);
18+
}
19+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.baeldung.reactive.eventstreaming.model;
2+
3+
import java.time.LocalTime;
4+
5+
import lombok.AllArgsConstructor;
6+
import lombok.Data;
7+
import lombok.NoArgsConstructor;
8+
9+
@AllArgsConstructor
10+
@Data
11+
@NoArgsConstructor
12+
public class Notification {
13+
14+
private String text;
15+
private LocalTime time;
16+
17+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.baeldung.reactive.eventstreaming.service;
2+
3+
import com.baeldung.reactive.eventstreaming.model.Notification;
4+
5+
import java.time.Duration;
6+
import java.time.LocalTime;
7+
import java.util.Random;
8+
import java.util.stream.Stream;
9+
10+
import org.springframework.stereotype.Service;
11+
12+
import reactor.core.publisher.Flux;
13+
14+
@Service
15+
public class NotificationService {
16+
17+
public Flux<Notification> streamNotification() {
18+
Flux<Long> interval = Flux.interval(Duration.ofSeconds(1));
19+
20+
Flux<Notification> events = Flux.fromStream(Stream.generate(
21+
()->new Notification( getText(),
22+
LocalTime.now())));
23+
24+
return Flux.zip(events, interval, (key, value) -> key);
25+
}
26+
27+
private String getText() {
28+
int randomNumber = new Random().nextInt(9) + 1;
29+
30+
String text = String.format("You have %d new like(s) on your post...", randomNumber);
31+
32+
return text;
33+
}
34+
35+
}

0 commit comments

Comments
 (0)