Skip to content

Commit c456950

Browse files
committed
Add create shortcut to RSocketStrategies
Now that RSocketStrategies has default settings it makes sense to have a create() shortcut vs builder().build(). This commit also updates tests to take advantage of improvements in this and the previous two commits. See gh-23314
1 parent 91b040d commit c456950

File tree

4 files changed

+37
-44
lines changed

4 files changed

+37
-44
lines changed

spring-messaging/src/main/java/org/springframework/messaging/rsocket/RSocketStrategies.java

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,20 +114,30 @@ default <T> Decoder<T> decoder(ResolvableType elementType, @Nullable MimeType mi
114114
*/
115115
ReactiveAdapterRegistry reactiveAdapterRegistry();
116116

117+
/**
118+
* Return a builder to create a new {@link RSocketStrategies} instance
119+
* replicated from the current instance.
120+
*/
121+
default Builder mutate() {
122+
return new DefaultRSocketStrategies.DefaultRSocketStrategiesBuilder(this);
123+
}
124+
117125

118126
/**
119-
* Return a builder to build a new {@code RSocketStrategies} instance.
127+
* Create an {@code RSocketStrategies} instance with default settings.
128+
* Equivalent to {@code RSocketStrategies.builder().build()}.
120129
*/
121-
static Builder builder() {
122-
return new DefaultRSocketStrategies.DefaultRSocketStrategiesBuilder();
130+
static RSocketStrategies create() {
131+
return new DefaultRSocketStrategies.DefaultRSocketStrategiesBuilder().build();
123132
}
124133

125134
/**
126-
* Return a builder to create a new {@link RSocketStrategies} instance
127-
* replicated from the current instance.
135+
* Return a builder to build a new {@code RSocketStrategies} instance.
136+
* The builder applies default settings, see individual builder methods for
137+
* details.
128138
*/
129-
default Builder mutate() {
130-
return new DefaultRSocketStrategies.DefaultRSocketStrategiesBuilder(this);
139+
static Builder builder() {
140+
return new DefaultRSocketStrategies.DefaultRSocketStrategiesBuilder();
131141
}
132142

133143

spring-messaging/src/test/java/org/springframework/messaging/rsocket/RSocketBufferLeakTests.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import io.rsocket.AbstractRSocket;
2727
import io.rsocket.RSocket;
2828
import io.rsocket.RSocketFactory;
29+
import io.rsocket.SocketAcceptor;
2930
import io.rsocket.frame.decoder.PayloadDecoder;
3031
import io.rsocket.plugins.RSocketInterceptor;
3132
import io.rsocket.transport.netty.server.CloseableChannel;
@@ -44,8 +45,6 @@
4445
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
4546
import org.springframework.context.annotation.Bean;
4647
import org.springframework.context.annotation.Configuration;
47-
import org.springframework.core.codec.CharSequenceEncoder;
48-
import org.springframework.core.codec.StringDecoder;
4948
import org.springframework.core.io.Resource;
5049
import org.springframework.messaging.handler.annotation.MessageExceptionHandler;
5150
import org.springframework.messaging.handler.annotation.MessageMapping;
@@ -74,21 +73,21 @@ public class RSocketBufferLeakTests {
7473
@BeforeClass
7574
@SuppressWarnings("ConstantConditions")
7675
public static void setupOnce() {
76+
7777
context = new AnnotationConfigApplicationContext(ServerConfig.class);
78+
RSocketMessageHandler messageHandler = context.getBean(RSocketMessageHandler.class);
79+
SocketAcceptor responder = messageHandler.serverResponder();
7880

7981
server = RSocketFactory.receive()
8082
.frameDecoder(PayloadDecoder.ZERO_COPY)
8183
.addResponderPlugin(payloadInterceptor) // intercept responding
82-
.acceptor(context.getBean(RSocketMessageHandler.class).serverResponder())
84+
.acceptor(responder)
8385
.transport(TcpServerTransport.create("localhost", 7000))
8486
.start()
8587
.block();
8688

8789
requester = RSocketRequester.builder()
88-
.rsocketFactory(factory -> {
89-
factory.frameDecoder(PayloadDecoder.ZERO_COPY);
90-
factory.addRequesterPlugin(payloadInterceptor); // intercept outgoing requests
91-
})
90+
.rsocketFactory(factory -> factory.addRequesterPlugin(payloadInterceptor))
9291
.rsocketStrategies(context.getBean(RSocketStrategies.class))
9392
.connectTcp("localhost", 7000)
9493
.block();
@@ -215,8 +214,6 @@ public RSocketMessageHandler messageHandler() {
215214
@Bean
216215
public RSocketStrategies rsocketStrategies() {
217216
return RSocketStrategies.builder()
218-
.decoder(StringDecoder.allMimeTypes())
219-
.encoder(CharSequenceEncoder.allMimeTypes())
220217
.dataBufferFactory(new LeakAwareNettyDataBufferFactory(PooledByteBufAllocator.DEFAULT))
221218
.build();
222219
}

spring-messaging/src/test/java/org/springframework/messaging/rsocket/RSocketClientToServerIntegrationTests.java

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818

1919
import java.time.Duration;
2020

21-
import io.netty.buffer.PooledByteBufAllocator;
2221
import io.rsocket.RSocketFactory;
22+
import io.rsocket.SocketAcceptor;
2323
import io.rsocket.frame.decoder.PayloadDecoder;
2424
import io.rsocket.transport.netty.server.CloseableChannel;
2525
import io.rsocket.transport.netty.server.TcpServerTransport;
@@ -34,9 +34,6 @@
3434
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
3535
import org.springframework.context.annotation.Bean;
3636
import org.springframework.context.annotation.Configuration;
37-
import org.springframework.core.codec.CharSequenceEncoder;
38-
import org.springframework.core.codec.StringDecoder;
39-
import org.springframework.core.io.buffer.NettyDataBufferFactory;
4037
import org.springframework.messaging.handler.annotation.MessageExceptionHandler;
4138
import org.springframework.messaging.handler.annotation.MessageMapping;
4239
import org.springframework.messaging.rsocket.annotation.support.RSocketMessageHandler;
@@ -63,18 +60,20 @@ public class RSocketClientToServerIntegrationTests {
6360
@BeforeClass
6461
@SuppressWarnings("ConstantConditions")
6562
public static void setupOnce() {
63+
6664
context = new AnnotationConfigApplicationContext(ServerConfig.class);
65+
RSocketMessageHandler messageHandler = context.getBean(RSocketMessageHandler.class);
66+
SocketAcceptor responder = messageHandler.serverResponder();
6767

6868
server = RSocketFactory.receive()
6969
.addResponderPlugin(interceptor)
7070
.frameDecoder(PayloadDecoder.ZERO_COPY)
71-
.acceptor(context.getBean(RSocketMessageHandler.class).serverResponder())
71+
.acceptor(responder)
7272
.transport(TcpServerTransport.create("localhost", 7000))
7373
.start()
7474
.block();
7575

7676
requester = RSocketRequester.builder()
77-
.rsocketFactory(factory -> factory.frameDecoder(PayloadDecoder.ZERO_COPY))
7877
.rsocketStrategies(context.getBean(RSocketStrategies.class))
7978
.connectTcp("localhost", 7000)
8079
.block();
@@ -266,11 +265,7 @@ public RSocketMessageHandler messageHandler() {
266265

267266
@Bean
268267
public RSocketStrategies rsocketStrategies() {
269-
return RSocketStrategies.builder()
270-
.decoder(StringDecoder.allMimeTypes())
271-
.encoder(CharSequenceEncoder.allMimeTypes())
272-
.dataBufferFactory(new NettyDataBufferFactory(PooledByteBufAllocator.DEFAULT))
273-
.build();
268+
return RSocketStrategies.create();
274269
}
275270
}
276271

spring-messaging/src/test/java/org/springframework/messaging/rsocket/RSocketServerToClientIntegrationTests.java

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818

1919
import java.time.Duration;
2020

21-
import io.netty.buffer.PooledByteBufAllocator;
2221
import io.rsocket.RSocketFactory;
22+
import io.rsocket.SocketAcceptor;
2323
import io.rsocket.frame.decoder.PayloadDecoder;
2424
import io.rsocket.transport.netty.server.CloseableChannel;
2525
import io.rsocket.transport.netty.server.TcpServerTransport;
@@ -37,9 +37,6 @@
3737
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
3838
import org.springframework.context.annotation.Bean;
3939
import org.springframework.context.annotation.Configuration;
40-
import org.springframework.core.codec.CharSequenceEncoder;
41-
import org.springframework.core.codec.StringDecoder;
42-
import org.springframework.core.io.buffer.NettyDataBufferFactory;
4340
import org.springframework.messaging.handler.annotation.MessageMapping;
4441
import org.springframework.messaging.rsocket.annotation.ConnectMapping;
4542
import org.springframework.messaging.rsocket.annotation.support.AnnotationClientResponderConfigurer;
@@ -62,11 +59,14 @@ public class RSocketServerToClientIntegrationTests {
6259
@BeforeClass
6360
@SuppressWarnings("ConstantConditions")
6461
public static void setupOnce() {
62+
6563
context = new AnnotationConfigApplicationContext(RSocketConfig.class);
64+
RSocketMessageHandler messageHandler = context.getBean(RSocketMessageHandler.class);
65+
SocketAcceptor responder = messageHandler.serverResponder();
6666

6767
server = RSocketFactory.receive()
6868
.frameDecoder(PayloadDecoder.ZERO_COPY)
69-
.acceptor(context.getBean(RSocketMessageHandler.class).serverResponder())
69+
.acceptor(responder)
7070
.transport(TcpServerTransport.create("localhost", 0))
7171
.start()
7272
.block();
@@ -103,21 +103,16 @@ private static void connectAndRunTest(String connectionRoute) {
103103

104104
ServerController serverController = context.getBean(ServerController.class);
105105
serverController.reset();
106-
RSocketStrategies strategies = context.getBean(RSocketStrategies.class);
107106

108107
RSocketRequester requester = null;
109108
try {
110-
ClientRSocketFactoryConfigurer responderConfigurer =
111-
AnnotationClientResponderConfigurer.withHandlers(new ClientHandler());
112-
113109
requester = RSocketRequester.builder()
114110
.rsocketFactory(factory -> {
115111
factory.metadataMimeType("text/plain");
116112
factory.setupPayload(ByteBufPayload.create("", connectionRoute));
117-
factory.frameDecoder(PayloadDecoder.ZERO_COPY);
118113
})
119-
.rsocketFactory(responderConfigurer)
120-
.rsocketStrategies(strategies)
114+
.rsocketFactory(AnnotationClientResponderConfigurer.withHandlers(new ClientHandler()))
115+
.rsocketStrategies(context.getBean(RSocketStrategies.class))
121116
.connectTcp("localhost", server.address().getPort())
122117
.block();
123118

@@ -268,11 +263,7 @@ public RSocketMessageHandler serverMessageHandler() {
268263

269264
@Bean
270265
public RSocketStrategies rsocketStrategies() {
271-
return RSocketStrategies.builder()
272-
.decoder(StringDecoder.allMimeTypes())
273-
.encoder(CharSequenceEncoder.allMimeTypes())
274-
.dataBufferFactory(new NettyDataBufferFactory(PooledByteBufAllocator.DEFAULT))
275-
.build();
266+
return RSocketStrategies.create();
276267
}
277268
}
278269

0 commit comments

Comments
 (0)