From 7857dd2d7230ab48c53e8648c23647e3b793b895 Mon Sep 17 00:00:00 2001 From: Brian Clozel Date: Fri, 21 Jun 2019 11:21:13 +0200 Subject: [PATCH] Update after RSocket changes in Spring Framework Spring Framework recently removed the `MessageHandlerAcceptor` (which is a sub-class of `RSocketMessageHandler`). Instead of implementing directly the `SocketAcceptor` contract, it now exposes them through `clientAcceptor()` and `serverAcceptor()` methods. See gh-17280 --- .../RSocketMessagingAutoConfiguration.java | 12 +++++----- .../RSocketServerAutoConfiguration.java | 13 +++++------ .../RSocketWebSocketNettyRouteProvider.java | 10 ++++---- ...SocketMessagingAutoConfigurationTests.java | 23 +++++++++---------- .../RSocketServerAutoConfigurationTests.java | 12 +++++----- 5 files changed, 34 insertions(+), 36 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/rsocket/RSocketMessagingAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/rsocket/RSocketMessagingAutoConfiguration.java index 999138b33d..4d31d2a83c 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/rsocket/RSocketMessagingAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/rsocket/RSocketMessagingAutoConfiguration.java @@ -25,9 +25,9 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.messaging.rsocket.MessageHandlerAcceptor; import org.springframework.messaging.rsocket.RSocketRequester; import org.springframework.messaging.rsocket.RSocketStrategies; +import org.springframework.messaging.rsocket.annotation.support.RSocketMessageHandler; import org.springframework.util.ClassUtils; import org.springframework.web.util.pattern.PathPatternParser; import org.springframework.web.util.pattern.PathPatternRouteMatcher; @@ -48,15 +48,15 @@ public class RSocketMessagingAutoConfiguration { @Bean @ConditionalOnMissingBean - public MessageHandlerAcceptor messageHandlerAcceptor(RSocketStrategies rSocketStrategies) { - MessageHandlerAcceptor acceptor = new MessageHandlerAcceptor(); - acceptor.setRSocketStrategies(rSocketStrategies); + public RSocketMessageHandler messageHandler(RSocketStrategies rSocketStrategies) { + RSocketMessageHandler messageHandler = new RSocketMessageHandler(); + messageHandler.setRSocketStrategies(rSocketStrategies); if (ClassUtils.isPresent(PATHPATTERN_ROUTEMATCHER_CLASS, null)) { PathPatternParser parser = new PathPatternParser(); parser.setSeparator('.'); - acceptor.setRouteMatcher(new PathPatternRouteMatcher(parser)); + messageHandler.setRouteMatcher(new PathPatternRouteMatcher(parser)); } - return acceptor; + return messageHandler; } } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/rsocket/RSocketServerAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/rsocket/RSocketServerAutoConfiguration.java index dcb5e36e85..b02aae7f49 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/rsocket/RSocketServerAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/rsocket/RSocketServerAutoConfiguration.java @@ -19,7 +19,6 @@ package org.springframework.boot.autoconfigure.rsocket; import java.util.stream.Collectors; import io.rsocket.RSocketFactory; -import io.rsocket.SocketAcceptor; import io.rsocket.transport.netty.server.TcpServerTransport; import reactor.netty.http.server.HttpServer; @@ -42,8 +41,8 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Configuration; import org.springframework.http.client.reactive.ReactorResourceFactory; -import org.springframework.messaging.rsocket.MessageHandlerAcceptor; import org.springframework.messaging.rsocket.RSocketStrategies; +import org.springframework.messaging.rsocket.annotation.support.RSocketMessageHandler; /** * {@link EnableAutoConfiguration Auto-configuration} for RSocket servers. In the case of @@ -57,7 +56,7 @@ import org.springframework.messaging.rsocket.RSocketStrategies; */ @Configuration(proxyBeanMethods = false) @ConditionalOnClass({ RSocketFactory.class, RSocketStrategies.class, HttpServer.class, TcpServerTransport.class }) -@ConditionalOnBean(MessageHandlerAcceptor.class) +@ConditionalOnBean(RSocketMessageHandler.class) @AutoConfigureAfter(RSocketStrategiesAutoConfiguration.class) @EnableConfigurationProperties(RSocketProperties.class) public class RSocketServerAutoConfiguration { @@ -68,9 +67,9 @@ public class RSocketServerAutoConfiguration { @Bean public RSocketWebSocketNettyRouteProvider rSocketWebsocketRouteProvider(RSocketProperties properties, - MessageHandlerAcceptor messageHandlerAcceptor) { + RSocketMessageHandler messageHandler) { return new RSocketWebSocketNettyRouteProvider(properties.getServer().getMappingPath(), - messageHandlerAcceptor); + messageHandler.serverAcceptor()); } } @@ -100,8 +99,8 @@ public class RSocketServerAutoConfiguration { @Bean public RSocketServerBootstrap rSocketServerBootstrap(RSocketServerFactory rSocketServerFactory, - SocketAcceptor socketAcceptor) { - return new RSocketServerBootstrap(rSocketServerFactory, socketAcceptor); + RSocketMessageHandler rSocketMessageHandler) { + return new RSocketServerBootstrap(rSocketServerFactory, rSocketMessageHandler.serverAcceptor()); } } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/rsocket/RSocketWebSocketNettyRouteProvider.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/rsocket/RSocketWebSocketNettyRouteProvider.java index df29a7928a..1eeeb9d2ea 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/rsocket/RSocketWebSocketNettyRouteProvider.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/rsocket/RSocketWebSocketNettyRouteProvider.java @@ -17,12 +17,12 @@ package org.springframework.boot.autoconfigure.rsocket; import io.rsocket.RSocketFactory; +import io.rsocket.SocketAcceptor; import io.rsocket.transport.ServerTransport; import io.rsocket.transport.netty.server.WebsocketRouteTransport; import reactor.netty.http.server.HttpServerRoutes; import org.springframework.boot.web.embedded.netty.NettyRouteProvider; -import org.springframework.messaging.rsocket.MessageHandlerAcceptor; /** * {@link NettyRouteProvider} that configures an RSocket Websocket endpoint. @@ -33,16 +33,16 @@ class RSocketWebSocketNettyRouteProvider implements NettyRouteProvider { private final String mappingPath; - private final MessageHandlerAcceptor messageHandlerAcceptor; + private final SocketAcceptor socketAcceptor; - RSocketWebSocketNettyRouteProvider(String mappingPath, MessageHandlerAcceptor messageHandlerAcceptor) { + RSocketWebSocketNettyRouteProvider(String mappingPath, SocketAcceptor socketAcceptor) { this.mappingPath = mappingPath; - this.messageHandlerAcceptor = messageHandlerAcceptor; + this.socketAcceptor = socketAcceptor; } @Override public HttpServerRoutes apply(HttpServerRoutes httpServerRoutes) { - ServerTransport.ConnectionAcceptor acceptor = RSocketFactory.receive().acceptor(this.messageHandlerAcceptor) + ServerTransport.ConnectionAcceptor acceptor = RSocketFactory.receive().acceptor(this.socketAcceptor) .toConnectionAcceptor(); return httpServerRoutes.ws(this.mappingPath, WebsocketRouteTransport.newHandler(acceptor)); } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/rsocket/RSocketMessagingAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/rsocket/RSocketMessagingAutoConfigurationTests.java index e83698504f..bfb22af520 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/rsocket/RSocketMessagingAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/rsocket/RSocketMessagingAutoConfigurationTests.java @@ -24,8 +24,8 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.codec.CharSequenceEncoder; import org.springframework.core.codec.StringDecoder; -import org.springframework.messaging.rsocket.MessageHandlerAcceptor; import org.springframework.messaging.rsocket.RSocketStrategies; +import org.springframework.messaging.rsocket.annotation.support.RSocketMessageHandler; import org.springframework.web.util.pattern.PathPatternRouteMatcher; import static org.assertj.core.api.Assertions.assertThat; @@ -44,8 +44,8 @@ class RSocketMessagingAutoConfigurationTests { @Test void shouldCreateDefaultBeans() { this.contextRunner.run((context) -> { - assertThat(context).getBeans(MessageHandlerAcceptor.class).hasSize(1); - assertThat(context.getBean(MessageHandlerAcceptor.class).getRouteMatcher()) + assertThat(context).getBeans(RSocketMessageHandler.class).hasSize(1); + assertThat(context.getBean(RSocketMessageHandler.class).getRouteMatcher()) .isInstanceOf(PathPatternRouteMatcher.class); }); } @@ -61,10 +61,9 @@ class RSocketMessagingAutoConfigurationTests { } @Test - void shouldUseCustomMessageHandlerAcceptor() { - this.contextRunner.withUserConfiguration(CustomMessageHandlerAcceptor.class) - .run((context) -> assertThat(context).getBeanNames(MessageHandlerAcceptor.class) - .containsOnly("customMessageHandlerAcceptor")); + void shouldUseCustomSocketAcceptor() { + this.contextRunner.withUserConfiguration(CustomMessageHandler.class).run((context) -> assertThat(context) + .getBeanNames(RSocketMessageHandler.class).containsOnly("customMessageHandler")); } @Configuration(proxyBeanMethods = false) @@ -79,15 +78,15 @@ class RSocketMessagingAutoConfigurationTests { } @Configuration(proxyBeanMethods = false) - static class CustomMessageHandlerAcceptor { + static class CustomMessageHandler { @Bean - public MessageHandlerAcceptor customMessageHandlerAcceptor() { - MessageHandlerAcceptor acceptor = new MessageHandlerAcceptor(); + public RSocketMessageHandler customMessageHandler() { + RSocketMessageHandler messageHandler = new RSocketMessageHandler(); RSocketStrategies strategies = RSocketStrategies.builder().encoder(CharSequenceEncoder.textPlainOnly()) .decoder(StringDecoder.textPlainOnly()).build(); - acceptor.setRSocketStrategies(strategies); - return acceptor; + messageHandler.setRSocketStrategies(strategies); + return messageHandler; } } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/rsocket/RSocketServerAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/rsocket/RSocketServerAutoConfigurationTests.java index 21c66152c2..f3f0ebd25a 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/rsocket/RSocketServerAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/rsocket/RSocketServerAutoConfigurationTests.java @@ -28,8 +28,8 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.codec.CharSequenceEncoder; import org.springframework.core.codec.StringDecoder; -import org.springframework.messaging.rsocket.MessageHandlerAcceptor; import org.springframework.messaging.rsocket.RSocketStrategies; +import org.springframework.messaging.rsocket.annotation.support.RSocketMessageHandler; import static org.assertj.core.api.Assertions.assertThat; @@ -91,11 +91,11 @@ class RSocketServerAutoConfigurationTests { static class BaseConfiguration { @Bean - public MessageHandlerAcceptor messageHandlerAcceptor() { - MessageHandlerAcceptor messageHandlerAcceptor = new MessageHandlerAcceptor(); - messageHandlerAcceptor.setRSocketStrategies(RSocketStrategies.builder() - .encoder(CharSequenceEncoder.textPlainOnly()).decoder(StringDecoder.textPlainOnly()).build()); - return messageHandlerAcceptor; + public RSocketMessageHandler messageHandler() { + RSocketMessageHandler messageHandler = new RSocketMessageHandler(); + messageHandler.setRSocketStrategies(RSocketStrategies.builder().encoder(CharSequenceEncoder.textPlainOnly()) + .decoder(StringDecoder.textPlainOnly()).build()); + return messageHandler; } }