Skip to content

Commit 3e5ac1d

Browse files
committed
BAEL-1475: code formatting
1 parent aa12596 commit 3e5ac1d

2 files changed

Lines changed: 18 additions & 32 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 & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,21 @@
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 {
31-
32-
eventFlux = Flux.generate(e -> {
33-
Event event = new Event(UUID.randomUUID()
34-
.toString(),
35-
LocalDateTime.now()
36-
.toString());
37-
e.next(event);
38-
});
27+
private Flux<Event> intervalFlux = Flux.interval(Duration.ofMillis(1000L)).zipWith(eventFlux, (time, event) -> event);
3928

40-
intervalFlux = Flux.interval(Duration.ofMillis(1000L))
41-
.zipWith(eventFlux, (time, event) -> event);
42-
43-
}
29+
private ObjectMapper json = new ObjectMapper();
4430

4531
/**
4632
* On each new client session, send the message flux to the client.
@@ -50,7 +36,7 @@ private void init() throws InterruptedException {
5036
*/
5137
@Override
5238
public Mono<Void> handle(WebSocketSession webSocketSession) {
53-
ObjectMapper json = new ObjectMapper();
39+
5440
return webSocketSession.send(intervalFlux.map(event -> {
5541
try {
5642
String jsonEvent = json.writeValueAsString(event);
@@ -60,12 +46,9 @@ public Mono<Void> handle(WebSocketSession webSocketSession) {
6046
e.printStackTrace();
6147
return "";
6248
}
63-
})
64-
.map(webSocketSession::textMessage))
49+
}).map(webSocketSession::textMessage))
6550

66-
.and(webSocketSession.receive()
67-
.map(WebSocketMessage::getPayloadAsText)
68-
.log());
51+
.and(webSocketSession.receive().map(WebSocketMessage::getPayloadAsText).log());
6952
}
7053

7154
}

0 commit comments

Comments
 (0)