From 06adaba4bff95db43cd45eac8b7b3ea7a35a8067 Mon Sep 17 00:00:00 2001 From: mikr Date: Tue, 31 Aug 2021 14:54:12 +0200 Subject: [PATCH 1/2] JAVA-6006 Upgrade Spring 5 Webflux module --- spring-5-webflux/pom.xml | 4 -- .../rsocket/client/ClientConfiguration.java | 31 ++++++++-------- .../CustomNettyWebServerFactory.java | 9 ++--- .../MarketDataRSocketControllerLiveTest.java | 37 ++++++++++++------- 4 files changed, 42 insertions(+), 39 deletions(-) diff --git a/spring-5-webflux/pom.xml b/spring-5-webflux/pom.xml index ad1a66943c..b37e93ded8 100644 --- a/spring-5-webflux/pom.xml +++ b/spring-5-webflux/pom.xml @@ -65,8 +65,4 @@ - - 2.3.3.RELEASE - - \ No newline at end of file diff --git a/spring-5-webflux/src/main/java/com/baeldung/spring/rsocket/client/ClientConfiguration.java b/spring-5-webflux/src/main/java/com/baeldung/spring/rsocket/client/ClientConfiguration.java index abfe2e7807..2e2c309240 100644 --- a/spring-5-webflux/src/main/java/com/baeldung/spring/rsocket/client/ClientConfiguration.java +++ b/spring-5-webflux/src/main/java/com/baeldung/spring/rsocket/client/ClientConfiguration.java @@ -1,30 +1,29 @@ package com.baeldung.spring.rsocket.client; -import io.rsocket.RSocket; -import io.rsocket.RSocketFactory; -import io.rsocket.frame.decoder.PayloadDecoder; -import io.rsocket.transport.netty.client.TcpClientTransport; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.messaging.rsocket.RSocketRequester; -import org.springframework.messaging.rsocket.RSocketStrategies; import org.springframework.util.MimeTypeUtils; +import reactor.util.retry.Retry; + +import java.time.Duration; @Configuration public class ClientConfiguration { @Bean - public RSocket rSocket() { - return RSocketFactory.connect() - .mimeType(MimeTypeUtils.APPLICATION_JSON_VALUE, MimeTypeUtils.APPLICATION_JSON_VALUE) - .frameDecoder(PayloadDecoder.ZERO_COPY) - .transport(TcpClientTransport.create(7000)) - .start() - .block(); - } + public RSocketRequester getRSocketRequester(){ - @Bean - RSocketRequester rSocketRequester(RSocketStrategies rSocketStrategies) { - return RSocketRequester.wrap(rSocket(), MimeTypeUtils.APPLICATION_JSON, MimeTypeUtils.APPLICATION_JSON, rSocketStrategies); + RSocketRequester.Builder builder = RSocketRequester.builder(); + + return builder + .rsocketConnector( + rSocketConnector -> + rSocketConnector.reconnect( + Retry.fixedDelay(2, Duration.ofSeconds(2)) + ) + ) + .dataMimeType(MimeTypeUtils.APPLICATION_JSON) + .tcp("localhost", 7000); } } diff --git a/spring-5-webflux/src/main/java/com/baeldung/spring/serverconfig/CustomNettyWebServerFactory.java b/spring-5-webflux/src/main/java/com/baeldung/spring/serverconfig/CustomNettyWebServerFactory.java index f9de3b4006..2d11a51160 100644 --- a/spring-5-webflux/src/main/java/com/baeldung/spring/serverconfig/CustomNettyWebServerFactory.java +++ b/spring-5-webflux/src/main/java/com/baeldung/spring/serverconfig/CustomNettyWebServerFactory.java @@ -25,12 +25,9 @@ public class CustomNettyWebServerFactory { @Override public HttpServer apply(HttpServer httpServer) { - EventLoopGroup parentGroup = new NioEventLoopGroup(); - EventLoopGroup childGroup = new NioEventLoopGroup(); - return httpServer - .tcpConfiguration(tcpServer -> tcpServer.bootstrap( - serverBootstrap -> serverBootstrap.group(parentGroup, childGroup).channel(NioServerSocketChannel.class) - )); + EventLoopGroup eventLoopGroup = new NioEventLoopGroup(); + eventLoopGroup.register(new NioServerSocketChannel()); + return httpServer.runOn(eventLoopGroup); } } } diff --git a/spring-5-webflux/src/test/java/com/baeldung/spring/rsocket/server/MarketDataRSocketControllerLiveTest.java b/spring-5-webflux/src/test/java/com/baeldung/spring/rsocket/server/MarketDataRSocketControllerLiveTest.java index 40ddc732ac..7d8ed1f22d 100644 --- a/spring-5-webflux/src/test/java/com/baeldung/spring/rsocket/server/MarketDataRSocketControllerLiveTest.java +++ b/spring-5-webflux/src/test/java/com/baeldung/spring/rsocket/server/MarketDataRSocketControllerLiveTest.java @@ -1,15 +1,8 @@ package com.baeldung.spring.rsocket.server; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.verify; - import com.baeldung.spring.rsocket.model.MarketData; import com.baeldung.spring.rsocket.model.MarketDataRequest; import io.rsocket.RSocket; -import io.rsocket.RSocketFactory; -import io.rsocket.frame.decoder.PayloadDecoder; -import io.rsocket.transport.netty.client.TcpClientTransport; -import java.time.Duration; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -23,6 +16,12 @@ import org.springframework.messaging.rsocket.RSocketStrategies; import org.springframework.test.context.TestPropertySource; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.util.MimeTypeUtils; +import reactor.util.retry.Retry; + +import java.time.Duration; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.verify; @RunWith(SpringRunner.class) @SpringBootTest @@ -81,12 +80,24 @@ public class MarketDataRSocketControllerLiveTest { @Bean @Lazy public RSocket rSocket() { - return RSocketFactory.connect() - .mimeType(MimeTypeUtils.APPLICATION_JSON_VALUE, MimeTypeUtils.APPLICATION_JSON_VALUE) - .frameDecoder(PayloadDecoder.ZERO_COPY) - .transport(TcpClientTransport.create(7000)) - .start() - .block(); + + RSocketRequester.Builder builder = RSocketRequester.builder(); + + return builder + .rsocketConnector( + rSocketConnector -> + rSocketConnector.reconnect(Retry.fixedDelay(2, Duration.ofSeconds(2)))) + .dataMimeType(MimeTypeUtils.APPLICATION_JSON) + .tcp("localhost", 7000) + .rsocket(); +// .connec/t(TcpClientTransport.create(6565)); + +// return RSocketFactory.connect() +// .mimeType(MimeTypeUtils.APPLICATION_JSON_VALUE, MimeTypeUtils.APPLICATION_JSON_VALUE) +// .frameDecoder(PayloadDecoder.ZERO_COPY) +// .transport(TcpClientTransport.create(7000)) +// .start() +// .block(); } @Bean From 69d65ee12afde97ba354defb6652703d8c69b677 Mon Sep 17 00:00:00 2001 From: mikr Date: Thu, 2 Sep 2021 09:16:14 +0200 Subject: [PATCH 2/2] JAVA-6006 Remove commented code --- .../server/MarketDataRSocketControllerLiveTest.java | 8 -------- 1 file changed, 8 deletions(-) diff --git a/spring-5-webflux/src/test/java/com/baeldung/spring/rsocket/server/MarketDataRSocketControllerLiveTest.java b/spring-5-webflux/src/test/java/com/baeldung/spring/rsocket/server/MarketDataRSocketControllerLiveTest.java index 7d8ed1f22d..98d604b178 100644 --- a/spring-5-webflux/src/test/java/com/baeldung/spring/rsocket/server/MarketDataRSocketControllerLiveTest.java +++ b/spring-5-webflux/src/test/java/com/baeldung/spring/rsocket/server/MarketDataRSocketControllerLiveTest.java @@ -90,14 +90,6 @@ public class MarketDataRSocketControllerLiveTest { .dataMimeType(MimeTypeUtils.APPLICATION_JSON) .tcp("localhost", 7000) .rsocket(); -// .connec/t(TcpClientTransport.create(6565)); - -// return RSocketFactory.connect() -// .mimeType(MimeTypeUtils.APPLICATION_JSON_VALUE, MimeTypeUtils.APPLICATION_JSON_VALUE) -// .frameDecoder(PayloadDecoder.ZERO_COPY) -// .transport(TcpClientTransport.create(7000)) -// .start() -// .block(); } @Bean