Skip to content

Commit 3e68228

Browse files
artembilangaryrussell
authored andcommitted
More docs for new RSocket features
1 parent dd1d65b commit 3e68228

File tree

4 files changed

+84
-15
lines changed

4 files changed

+84
-15
lines changed

spring-integration-rsocket/src/main/java/org/springframework/integration/rsocket/IntegrationRSocketMessageHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ class IntegrationRSocketMessageHandler extends RSocketMessageHandler {
5454
this(false);
5555
}
5656

57-
IntegrationRSocketMessageHandler(boolean requestMappingCompatible) {
58-
this.messageMappingCompatible = requestMappingCompatible;
57+
IntegrationRSocketMessageHandler(boolean messageMappingCompatible) {
58+
this.messageMappingCompatible = messageMappingCompatible;
5959
if (!this.messageMappingCompatible) {
6060
setHandlerPredicate((clazz) -> false);
6161
}

spring-integration-rsocket/src/main/java/org/springframework/integration/rsocket/ServerRSocketConnector.java

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ public ServerRSocketConnector(ServerTransport<CloseableChannel> serverTransport)
9999
this.serverTransport = serverTransport;
100100
}
101101

102+
private ServerRSocketMessageHandler serverRSocketMessageHandler() {
103+
return (ServerRSocketMessageHandler) this.rSocketMessageHandler;
104+
}
105+
102106
/**
103107
* Provide a {@link Consumer} to configure the {@link RSocketFactory.ServerRSocketFactory}.
104108
* @param factoryConfigurer the {@link Consumer} to configure the {@link RSocketFactory.ServerRSocketFactory}.
@@ -110,7 +114,7 @@ public void setFactoryConfigurer(Consumer<RSocketFactory.ServerRSocketFactory> f
110114

111115
/**
112116
* Configure a strategy to determine a key for the client {@link RSocketRequester} connected.
113-
* Defaults to the {@code destination} the client is connected.
117+
* Defaults to the {@code destination} to which a client is connected.
114118
* @param clientRSocketKeyStrategy the {@link BiFunction} to determine a key for client {@link RSocketRequester}s.
115119
*/
116120
public void setClientRSocketKeyStrategy(BiFunction<Map<String, Object>,
@@ -172,15 +176,31 @@ public void afterPropertiesSet() {
172176
}
173177
}
174178

179+
/**
180+
* Return connected {@link RSocketRequester}s mapped by keys.
181+
* @return connected {@link RSocketRequester}s mapped by keys.
182+
* @see ServerRSocketMessageHandler#getClientRSocketRequesters()
183+
*/
175184
public Map<Object, RSocketRequester> getClientRSocketRequesters() {
176185
return serverRSocketMessageHandler().getClientRSocketRequesters();
177186
}
178187

188+
/**
189+
* Return connected {@link RSocketRequester} mapped by key or null.
190+
* @param key the mapping key.
191+
* @return the {@link RSocketRequester} or null.
192+
* @see ServerRSocketMessageHandler#getClientRSocketRequester(Object)
193+
*/
179194
@Nullable
180195
public RSocketRequester getClientRSocketRequester(Object key) {
181-
return getClientRSocketRequesters().get(key);
196+
return serverRSocketMessageHandler().getClientRSocketRequester(key);
182197
}
183198

199+
/**
200+
* Return the port this internal server is bound or empty {@link Mono}.
201+
* @return the port this internal server is bound or empty {@link Mono}
202+
* if an external server is used.
203+
*/
184204
public Mono<Integer> getBoundPort() {
185205
if (this.serverTransport != null) {
186206
return this.serverMono
@@ -191,10 +211,6 @@ public Mono<Integer> getBoundPort() {
191211
}
192212
}
193213

194-
private ServerRSocketMessageHandler serverRSocketMessageHandler() {
195-
return (ServerRSocketMessageHandler) this.rSocketMessageHandler;
196-
}
197-
198214
@Override
199215
protected void doStart() {
200216
if (this.serverTransport != null) {

spring-integration-rsocket/src/main/java/org/springframework/integration/rsocket/ServerRSocketMessageHandler.java

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.springframework.context.ApplicationEventPublisher;
2727
import org.springframework.context.ApplicationEventPublisherAware;
2828
import org.springframework.core.io.buffer.DataBuffer;
29+
import org.springframework.lang.Nullable;
2930
import org.springframework.messaging.Message;
3031
import org.springframework.messaging.MessageHeaders;
3132
import org.springframework.messaging.handler.CompositeMessageCondition;
@@ -38,6 +39,18 @@
3839

3940
/**
4041
* An {@link IntegrationRSocketMessageHandler} extension for RSocket service side.
42+
* <p>
43+
* In a plain Spring Integration application instances of this class are created by the
44+
* {@link ServerRSocketConnector} internally and a new RSocket server is started over there.
45+
* When an existing RSocket server is in use, an instance of this class has to be
46+
* provided as a {@link #responder()} into that server and a {@link ServerRSocketConnector}
47+
* should accept the same instance as a delegate.
48+
*<p>
49+
* With a {@link #messageMappingCompatible} option this class also handles
50+
* {@link org.springframework.messaging.handler.annotation.MessageMapping} methods,
51+
* covering both Spring Integration and standard
52+
* {@link org.springframework.messaging.rsocket.annotation.support.RSocketMessageHandler}
53+
* functionality.
4154
*
4255
* @author Artem Bilan
4356
*
@@ -57,26 +70,61 @@ public class ServerRSocketMessageHandler extends IntegrationRSocketMessageHandle
5770

5871
private ApplicationEventPublisher applicationEventPublisher;
5972

73+
/**
74+
* Create an service side RSocket message handler instance for delegating
75+
* to {@link IntegrationRSocketEndpoint} beans and collect {@link RSocketRequester}s
76+
* from client connections.
77+
*/
6078
public ServerRSocketMessageHandler() {
6179
this(false);
6280
}
6381

64-
public ServerRSocketMessageHandler(boolean requestMappingCompatible) {
65-
super(requestMappingCompatible);
66-
}
67-
68-
69-
public Map<Object, RSocketRequester> getClientRSocketRequesters() {
70-
return Collections.unmodifiableMap(this.clientRSocketRequesters);
82+
/**
83+
* Create an service side RSocket message handler instance for delegating
84+
* to {@link IntegrationRSocketEndpoint} beans and collect {@link RSocketRequester}s
85+
* from client connections.
86+
* When {@code messageMappingCompatible == true}, this class also handles
87+
* {@link org.springframework.messaging.handler.annotation.MessageMapping} methods
88+
* as it is done by the standard
89+
* {@link org.springframework.messaging.rsocket.annotation.support.RSocketMessageHandler}.
90+
* @param messageMappingCompatible whether handle also
91+
* {@link org.springframework.messaging.handler.annotation.MessageMapping}.
92+
*/
93+
public ServerRSocketMessageHandler(boolean messageMappingCompatible) {
94+
super(messageMappingCompatible);
7195
}
7296

97+
/**
98+
* Configure a {@link BiFunction} to extract a key for mapping connected {@link RSocketRequester}s.
99+
* Defaults to the {@code destination} a client is connected.
100+
* @param clientRSocketKeyStrategy the {@link BiFunction} to use.
101+
*/
73102
public void setClientRSocketKeyStrategy(
74103
BiFunction<Map<String, Object>, DataBuffer, Object> clientRSocketKeyStrategy) {
75104

76105
Assert.notNull(clientRSocketKeyStrategy, "'clientRSocketKeyStrategy' must not be null");
77106
this.clientRSocketKeyStrategy = clientRSocketKeyStrategy;
78107
}
79108

109+
/**
110+
* Get connected {@link RSocketRequester}s mapped by the keys from the connect messages.
111+
* @return the map of connected {@link RSocketRequester}s.
112+
* @see #setClientRSocketKeyStrategy
113+
*/
114+
public Map<Object, RSocketRequester> getClientRSocketRequesters() {
115+
return Collections.unmodifiableMap(this.clientRSocketRequesters);
116+
}
117+
118+
/**
119+
* Obtain a connected {@link RSocketRequester} mapped by provided key or null.
120+
* @param key the key for mapped {@link RSocketRequester} if any.
121+
* @return the mapped {@link RSocketRequester} or null.
122+
*/
123+
@Nullable
124+
public RSocketRequester getClientRSocketRequester(Object key) {
125+
return this.clientRSocketRequesters.get(key);
126+
}
127+
80128
@Override
81129
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
82130
this.applicationEventPublisher = applicationEventPublisher;

src/reference/asciidoc/rsocket.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ public void onApplicationEvent(RSocketConnectedEvent event) {
7474
All the options, including `RSocketStrategies` bean and `@EventListener` for `RSocketConnectedEvent`, are optional.
7575
See `ServerRSocketConnector` JavaDocs for more information.
7676

77+
Starting with version 5.2.1, the `ServerRSocketMessageHandler` is extracted to a public, top-level class for possible connection with an existing RSocket server.
78+
When a `ServerRSocketConnector` is supplied with an external instance of `ServerRSocketMessageHandler`, it doesn't create an RSocket server internally and just delegates all the handling logic to the provided instance.
79+
In addition the `ServerRSocketMessageHandler` can be configured with a `messageMappingCompatible` flag to handle also `@MessageMapping` for an RSocket controller, fully replacing the functionality provided by the standard `RSocketMessageHandler`.
80+
This can be useful in mixed configurations, when classic `@MessageMapping` methods are present in the same application along with RSocket channel adapters and an externally configured RSocket server is present in the application.
81+
7782
The `ClientRSocketConnector` serves as a holder for `RSocketRequester` based on the `RSocket` connected via the provided `ClientTransport`.
7883
The `RSocketFactory.ClientRSocketFactory` can be customized with the provided `ClientRSocketFactoryConfigurer`.
7984
The `setupRoute` (with optional templates variables) and `setupData` with metadata can be also configured on this component.

0 commit comments

Comments
 (0)