Skip to content

Commit 026ff3b

Browse files
authored
Merge pull request eugenp#3488 from felipeazv/master
BAEL-1475: code formatting
2 parents 0585764 + c38de16 commit 026ff3b

2 files changed

Lines changed: 18 additions & 38 deletions

File tree

‎spring-5-reactive/src/main/java/com/baeldung/reactive/websocket/ReactiveJavaClientWebSocket.java‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,25 @@
22

33
import java.net.URI;
44
import java.time.Duration;
5-
import org.springframework.boot.autoconfigure.SpringBootApplication;
65
import org.springframework.web.reactive.socket.WebSocketMessage;
76
import org.springframework.web.reactive.socket.client.ReactorNettyWebSocketClient;
87
import org.springframework.web.reactive.socket.client.WebSocketClient;
98

109
import reactor.core.publisher.Mono;
1110

12-
@SpringBootApplication
1311
public class ReactiveJavaClientWebSocket {
14-
public static void main(String[] args) throws InterruptedException {
12+
public static void main(String[] args) throws InterruptedException {
13+
1514
WebSocketClient client = new ReactorNettyWebSocketClient();
16-
client.execute(URI.create("ws://localhost:8080/event-emitter"), session -> session.send(Mono.just(session.textMessage("event-me-from-spring-reactive-client")))
15+
client.execute(
16+
URI.create("ws://localhost:8080/event-emitter"),
17+
session -> session.send(
18+
Mono.just(session.textMessage("event-spring-reactive-client-websocket")))
1719
.thenMany(session.receive()
18-
.map(WebSocketMessage::getPayloadAsText)
19-
.log())
20+
.map(WebSocketMessage::getPayloadAsText)
21+
.log())
2022
.then())
2123
.block(Duration.ofSeconds(10L));
2224
}
25+
2326
}

‎spring-5-reactive/src/main/java/com/baeldung/reactive/websocket/ReactiveWebSocketHandler.java‎

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -12,45 +12,25 @@
1212
import reactor.core.publisher.Flux;
1313
import reactor.core.publisher.Mono;
1414

15-
import javax.annotation.PostConstruct;
1615
import java.time.Duration;
1716
import java.time.LocalDateTime;
1817
import java.util.UUID;
1918

2019
@Component
2120
public class ReactiveWebSocketHandler implements WebSocketHandler {
2221

23-
private Flux<Event> eventFlux;
24-
private Flux<Event> intervalFlux;
22+
private Flux<Event> eventFlux = Flux.generate(e -> {
23+
Event event = new Event(UUID.randomUUID().toString(), LocalDateTime.now().toString());
24+
e.next(event);
25+
});
2526

26-
/**
27-
* Here we prepare a Flux that will emit a message every second
28-
*/
29-
@PostConstruct
30-
private void init() throws InterruptedException {
27+
private Flux<Event> intervalFlux = Flux.interval(Duration.ofMillis(1000L)).zipWith(eventFlux, (time, event) -> event);
3128

32-
eventFlux = Flux.generate(e -> {
33-
Event event = new Event(UUID.randomUUID()
34-
.toString(),
35-
LocalDateTime.now()
36-
.toString());
37-
e.next(event);
38-
});
29+
private ObjectMapper json = new ObjectMapper();
3930

40-
intervalFlux = Flux.interval(Duration.ofMillis(1000L))
41-
.zipWith(eventFlux, (time, event) -> event);
42-
43-
}
44-
45-
/**
46-
* On each new client session, send the message flux to the client.
47-
* Spring subscribes to the flux and send every new flux event to the WebSocketSession object
48-
* @param session
49-
* @return Mono<Void>
50-
*/
5131
@Override
5232
public Mono<Void> handle(WebSocketSession webSocketSession) {
53-
ObjectMapper json = new ObjectMapper();
33+
5434
return webSocketSession.send(intervalFlux.map(event -> {
5535
try {
5636
String jsonEvent = json.writeValueAsString(event);
@@ -60,12 +40,9 @@ public Mono<Void> handle(WebSocketSession webSocketSession) {
6040
e.printStackTrace();
6141
return "";
6242
}
63-
})
64-
.map(webSocketSession::textMessage))
43+
}).map(webSocketSession::textMessage))
6544

66-
.and(webSocketSession.receive()
67-
.map(WebSocketMessage::getPayloadAsText)
68-
.log());
45+
.and(webSocketSession.receive().map(WebSocketMessage::getPayloadAsText).log());
6946
}
7047

7148
}

0 commit comments

Comments
 (0)