Skip to content

Commit 615c6d4

Browse files
committed
Restructure RSocket packages and polish
Polish code and relocate `RSocketServerBootstrap` from `server` to `context` since it's really an `ApplicationContext` concern. Closes gh-18391
1 parent de393ab commit 615c6d4

File tree

12 files changed

+81
-81
lines changed

12 files changed

+81
-81
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/rsocket/RSocketProperties.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public static class Server {
5151
/**
5252
* RSocket transport protocol.
5353
*/
54-
private RSocketServer.TRANSPORT transport = RSocketServer.TRANSPORT.TCP;
54+
private RSocketServer.Transport transport = RSocketServer.Transport.TCP;
5555

5656
/**
5757
* Path under which RSocket handles requests (only works with websocket
@@ -75,11 +75,11 @@ public void setAddress(InetAddress address) {
7575
this.address = address;
7676
}
7777

78-
public RSocketServer.TRANSPORT getTransport() {
78+
public RSocketServer.Transport getTransport() {
7979
return this.transport;
8080
}
8181

82-
public void setTransport(RSocketServer.TRANSPORT transport) {
82+
public void setTransport(RSocketServer.Transport transport) {
8383
this.transport = transport;
8484
}
8585

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/rsocket/RSocketServerAutoConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@
3434
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
3535
import org.springframework.boot.context.properties.EnableConfigurationProperties;
3636
import org.springframework.boot.context.properties.PropertyMapper;
37+
import org.springframework.boot.rsocket.context.RSocketServerBootstrap;
3738
import org.springframework.boot.rsocket.netty.NettyRSocketServerFactory;
38-
import org.springframework.boot.rsocket.server.RSocketServerBootstrap;
3939
import org.springframework.boot.rsocket.server.RSocketServerFactory;
4040
import org.springframework.boot.rsocket.server.ServerRSocketFactoryCustomizer;
4141
import org.springframework.context.annotation.Bean;

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/rsocket/RSocketServerAutoConfigurationTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
import org.springframework.boot.autoconfigure.AutoConfigurations;
2222
import org.springframework.boot.rsocket.context.RSocketPortInfoApplicationContextInitializer;
23-
import org.springframework.boot.rsocket.server.RSocketServerBootstrap;
23+
import org.springframework.boot.rsocket.context.RSocketServerBootstrap;
2424
import org.springframework.boot.rsocket.server.RSocketServerFactory;
2525
import org.springframework.boot.rsocket.server.ServerRSocketFactoryCustomizer;
2626
import org.springframework.boot.test.context.runner.ApplicationContextRunner;

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/rsocket/context/RSocketPortInfoApplicationContextInitializer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ private static class Listener implements ApplicationListener<RSocketServerInitia
6363

6464
@Override
6565
public void onApplicationEvent(RSocketServerInitializedEvent event) {
66-
setPortProperty(this.applicationContext, event.getrSocketServer().address().getPort());
66+
setPortProperty(this.applicationContext, event.getServer().address().getPort());
6767
}
6868

6969
private void setPortProperty(ApplicationContext context, int port) {
Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,16 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.boot.rsocket.server;
17+
package org.springframework.boot.rsocket.context;
1818

1919
import io.rsocket.SocketAcceptor;
2020

21-
import org.springframework.boot.rsocket.context.RSocketServerInitializedEvent;
21+
import org.springframework.boot.rsocket.server.RSocketServer;
22+
import org.springframework.boot.rsocket.server.RSocketServerFactory;
2223
import org.springframework.context.ApplicationEventPublisher;
2324
import org.springframework.context.ApplicationEventPublisherAware;
2425
import org.springframework.context.SmartLifecycle;
26+
import org.springframework.util.Assert;
2527

2628
/**
2729
* Bootstrap an {@link RSocketServer} and start it with the application context.
@@ -31,33 +33,34 @@
3133
*/
3234
public class RSocketServerBootstrap implements ApplicationEventPublisherAware, SmartLifecycle {
3335

34-
private final RSocketServer rSocketServer;
36+
private final RSocketServer server;
3537

36-
private ApplicationEventPublisher applicationEventPublisher;
38+
private ApplicationEventPublisher eventPublisher;
3739

38-
public RSocketServerBootstrap(RSocketServerFactory serverFactoryProvider, SocketAcceptor socketAcceptor) {
39-
this.rSocketServer = serverFactoryProvider.create(socketAcceptor);
40+
public RSocketServerBootstrap(RSocketServerFactory serverFactory, SocketAcceptor socketAcceptor) {
41+
Assert.notNull(serverFactory, "ServerFactory must not be null");
42+
this.server = serverFactory.create(socketAcceptor);
4043
}
4144

4245
@Override
4346
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
44-
this.applicationEventPublisher = applicationEventPublisher;
47+
this.eventPublisher = applicationEventPublisher;
4548
}
4649

4750
@Override
4851
public void start() {
49-
this.rSocketServer.start();
50-
this.applicationEventPublisher.publishEvent(new RSocketServerInitializedEvent(this.rSocketServer));
52+
this.server.start();
53+
this.eventPublisher.publishEvent(new RSocketServerInitializedEvent(this.server));
5154
}
5255

5356
@Override
5457
public void stop() {
55-
this.rSocketServer.stop();
58+
this.server.stop();
5659
}
5760

5861
@Override
5962
public boolean isRunning() {
60-
RSocketServer server = this.rSocketServer;
63+
RSocketServer server = this.server;
6164
if (server != null) {
6265
return server.address() != null;
6366
}

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/rsocket/context/RSocketServerInitializedEvent.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,17 @@
2727
* @author Brian Clozel
2828
* @since 2.2.0
2929
*/
30-
@SuppressWarnings("serial")
3130
public class RSocketServerInitializedEvent extends ApplicationEvent {
3231

33-
public RSocketServerInitializedEvent(RSocketServer rSocketServer) {
34-
super(rSocketServer);
32+
public RSocketServerInitializedEvent(RSocketServer server) {
33+
super(server);
3534
}
3635

3736
/**
3837
* Access the {@link RSocketServer}.
3938
* @return the embedded RSocket server
4039
*/
41-
public RSocketServer getrSocketServer() {
40+
public RSocketServer getServer() {
4241
return getSource();
4342
}
4443

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/rsocket/netty/NettyRSocketServer.java

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,25 +61,13 @@ public InetSocketAddress address() {
6161

6262
@Override
6363
public void start() throws RSocketServerException {
64-
if (this.lifecycleTimeout != null) {
65-
this.channel = this.starter.block(this.lifecycleTimeout);
66-
}
67-
else {
68-
this.channel = this.starter.block();
69-
}
64+
this.channel = block(this.starter, this.lifecycleTimeout);
7065
logger.info("Netty RSocket started on port(s): " + address().getPort());
7166
startDaemonAwaitThread(this.channel);
7267
}
7368

7469
private void startDaemonAwaitThread(CloseableChannel channel) {
75-
Thread awaitThread = new Thread("rsocket") {
76-
77-
@Override
78-
public void run() {
79-
channel.onClose().block();
80-
}
81-
82-
};
70+
Thread awaitThread = new Thread(() -> channel.onClose().block(), "rsocket");
8371
awaitThread.setContextClassLoader(getClass().getClassLoader());
8472
awaitThread.setDaemon(false);
8573
awaitThread.start();
@@ -93,4 +81,8 @@ public void stop() throws RSocketServerException {
9381
}
9482
}
9583

84+
private <T> T block(Mono<T> mono, Duration timeout) {
85+
return (timeout != null) ? mono.block(timeout) : mono.block();
86+
}
87+
9688
}

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/rsocket/netty/NettyRSocketServerFactory.java

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public class NettyRSocketServerFactory implements RSocketServerFactory, Configur
5454

5555
private InetAddress address;
5656

57-
private RSocketServer.TRANSPORT transport = RSocketServer.TRANSPORT.TCP;
57+
private RSocketServer.Transport transport = RSocketServer.Transport.TCP;
5858

5959
private ReactorResourceFactory resourceFactory;
6060

@@ -73,7 +73,7 @@ public void setAddress(InetAddress address) {
7373
}
7474

7575
@Override
76-
public void setTransport(RSocketServer.TRANSPORT transport) {
76+
public void setTransport(RSocketServer.Transport transport) {
7777
this.transport = transport;
7878
}
7979

@@ -126,26 +126,28 @@ public NettyRSocketServer create(SocketAcceptor socketAcceptor) {
126126
}
127127

128128
private ServerTransport<CloseableChannel> createTransport() {
129-
if (this.transport == RSocketServer.TRANSPORT.WEBSOCKET) {
130-
if (this.resourceFactory != null) {
131-
HttpServer httpServer = HttpServer.create().tcpConfiguration((tcpServer) -> tcpServer
132-
.runOn(this.resourceFactory.getLoopResources()).addressSupplier(this::getListenAddress));
133-
return WebsocketServerTransport.create(httpServer);
134-
}
135-
else {
136-
return WebsocketServerTransport.create(getListenAddress());
137-
}
129+
if (this.transport == RSocketServer.Transport.WEBSOCKET) {
130+
return createWebSocketTransport();
138131
}
139-
else {
140-
if (this.resourceFactory != null) {
141-
TcpServer tcpServer = TcpServer.create().runOn(this.resourceFactory.getLoopResources())
142-
.addressSupplier(this::getListenAddress);
143-
return TcpServerTransport.create(tcpServer);
144-
}
145-
else {
146-
return TcpServerTransport.create(getListenAddress());
147-
}
132+
return createTcpTransport();
133+
}
134+
135+
private ServerTransport<CloseableChannel> createWebSocketTransport() {
136+
if (this.resourceFactory != null) {
137+
HttpServer httpServer = HttpServer.create().tcpConfiguration((tcpServer) -> tcpServer
138+
.runOn(this.resourceFactory.getLoopResources()).addressSupplier(this::getListenAddress));
139+
return WebsocketServerTransport.create(httpServer);
140+
}
141+
return WebsocketServerTransport.create(getListenAddress());
142+
}
143+
144+
private ServerTransport<CloseableChannel> createTcpTransport() {
145+
if (this.resourceFactory != null) {
146+
TcpServer tcpServer = TcpServer.create().runOn(this.resourceFactory.getLoopResources())
147+
.addressSupplier(this::getListenAddress);
148+
return TcpServerTransport.create(tcpServer);
148149
}
150+
return TcpServerTransport.create(getListenAddress());
149151
}
150152

151153
private InetSocketAddress getListenAddress() {

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/rsocket/server/ConfigurableRSocketServerFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,6 @@ public interface ConfigurableRSocketServerFactory {
4343
* Set the transport that the RSocket server should use.
4444
* @param transport the transport protocol to use
4545
*/
46-
void setTransport(RSocketServer.TRANSPORT transport);
46+
void setTransport(RSocketServer.Transport transport);
4747

4848
}

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/rsocket/server/RSocketServer.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,17 @@ public interface RSocketServer {
5050
/**
5151
* Choice of transport protocol for the RSocket server.
5252
*/
53-
enum TRANSPORT {
53+
enum Transport {
5454

55-
TCP, WEBSOCKET
55+
/**
56+
* TCP transport protocol.
57+
*/
58+
TCP,
59+
60+
/**
61+
* WebSocket transport protocol.
62+
*/
63+
WEBSOCKET
5664

5765
}
5866

0 commit comments

Comments
 (0)