Skip to content

Commit e19e36a

Browse files
committed
Simplify RSocket client responder config
Now that responder RSocketStrategies also exposes responder strategies, AnnotationClientResponderConfigurer is reduced and no longer needs to be public. This commit folds it into RSocketMessageHandler as a nested class and exposes it as a ClientRSocketFactoryConfigurer through a static method that accepts the handlers to use. Effectively a shortcut for creating RSocketMessageHandler, giving it RSocketStrategies, calling afterPropertiesSet, and then the instance createResponder. See gh-23314
1 parent c456950 commit e19e36a

File tree

4 files changed

+50
-108
lines changed

4 files changed

+50
-108
lines changed

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
import org.springframework.core.ReactiveAdapterRegistry;
3131
import org.springframework.core.codec.Decoder;
3232
import org.springframework.lang.Nullable;
33-
import org.springframework.messaging.rsocket.annotation.support.AnnotationClientResponderConfigurer;
33+
import org.springframework.messaging.rsocket.annotation.support.RSocketMessageHandler;
3434
import org.springframework.util.MimeType;
3535

3636
/**
@@ -165,14 +165,15 @@ interface Builder {
165165

166166
/**
167167
* Callback to configure the {@code ClientRSocketFactory} directly.
168-
* <p>See {@link AnnotationClientResponderConfigurer} for configuring a
169-
* client side responder.
168+
* <p>See static factory method
169+
* {@link RSocketMessageHandler#clientResponder(Object...)} for
170+
* configuring a client side responder with annotated methods.
170171
* <p><strong>Note:</strong> Do not set {@link #dataMimeType(MimeType)}
171172
* and {@link #metadataMimeType(MimeType)} directly on the
172173
* {@code ClientRSocketFactory}. Use the shortcuts on this builder
173174
* instead since the created {@code RSocketRequester} needs to be aware
174175
* of those settings.
175-
* @see AnnotationClientResponderConfigurer
176+
* @see RSocketMessageHandler#clientResponder(Object...)
176177
*/
177178
RSocketRequester.Builder rsocketFactory(ClientRSocketFactoryConfigurer configurer);
178179

spring-messaging/src/main/java/org/springframework/messaging/rsocket/annotation/support/AnnotationClientResponderConfigurer.java

Lines changed: 0 additions & 101 deletions
This file was deleted.

spring-messaging/src/main/java/org/springframework/messaging/rsocket/annotation/support/RSocketMessageHandler.java

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,12 @@
2424

2525
import io.rsocket.ConnectionSetupPayload;
2626
import io.rsocket.RSocket;
27+
import io.rsocket.RSocketFactory;
2728
import io.rsocket.SocketAcceptor;
2829
import io.rsocket.frame.FrameType;
2930
import reactor.core.publisher.Mono;
3031

32+
import org.springframework.beans.BeanUtils;
3133
import org.springframework.core.ReactiveAdapterRegistry;
3234
import org.springframework.core.annotation.AnnotatedElementUtils;
3335
import org.springframework.core.codec.Decoder;
@@ -40,6 +42,7 @@
4042
import org.springframework.messaging.handler.annotation.MessageMapping;
4143
import org.springframework.messaging.handler.annotation.reactive.MessageMappingMessageHandler;
4244
import org.springframework.messaging.handler.invocation.reactive.HandlerMethodReturnValueHandler;
45+
import org.springframework.messaging.rsocket.ClientRSocketFactoryConfigurer;
4346
import org.springframework.messaging.rsocket.DefaultMetadataExtractor;
4447
import org.springframework.messaging.rsocket.MetadataExtractor;
4548
import org.springframework.messaging.rsocket.RSocketRequester;
@@ -324,10 +327,50 @@ private MessagingRSocket createResponder(ConnectionSetupPayload setupPayload, RS
324327
Assert.notNull(strategies, "No RSocketStrategies. Was afterPropertiesSet not called?");
325328
RSocketRequester requester = RSocketRequester.wrap(rsocket, dataMimeType, metadataMimeType, strategies);
326329

327-
Assert.notNull(this.metadataExtractor, () -> "No MetadataExtractor. Was afterPropertiesSet not called?");
330+
Assert.state(this.metadataExtractor != null,
331+
() -> "No MetadataExtractor. Was afterPropertiesSet not called?");
332+
333+
Assert.state(getRouteMatcher() != null,
334+
() -> "No RouteMatcher. Was afterPropertiesSet not called?");
328335

329336
return new MessagingRSocket(dataMimeType, metadataMimeType, this.metadataExtractor, requester,
330337
this, getRouteMatcher(), strategies);
331338
}
332339

340+
341+
public static ClientRSocketFactoryConfigurer clientResponder(Object... handlers) {
342+
return new ResponderConfigurer(handlers);
343+
}
344+
345+
346+
private static final class ResponderConfigurer implements ClientRSocketFactoryConfigurer {
347+
348+
private final List<Object> handlers = new ArrayList<>();
349+
350+
@Nullable
351+
private RSocketStrategies strategies;
352+
353+
354+
private ResponderConfigurer(Object... handlers) {
355+
Assert.notEmpty(handlers, "No handlers");
356+
for (Object obj : handlers) {
357+
this.handlers.add(obj instanceof Class ? BeanUtils.instantiateClass((Class<?>) obj) : obj);
358+
}
359+
}
360+
361+
@Override
362+
public void configureWithStrategies(RSocketStrategies strategies) {
363+
this.strategies = strategies;
364+
}
365+
366+
@Override
367+
public void configure(RSocketFactory.ClientRSocketFactory factory) {
368+
RSocketMessageHandler handler = new RSocketMessageHandler();
369+
handler.setHandlers(this.handlers);
370+
handler.setRSocketStrategies(this.strategies);
371+
handler.afterPropertiesSet();
372+
factory.acceptor(handler.clientResponder());
373+
}
374+
}
375+
333376
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
import org.springframework.context.annotation.Configuration;
4040
import org.springframework.messaging.handler.annotation.MessageMapping;
4141
import org.springframework.messaging.rsocket.annotation.ConnectMapping;
42-
import org.springframework.messaging.rsocket.annotation.support.AnnotationClientResponderConfigurer;
4342
import org.springframework.messaging.rsocket.annotation.support.RSocketMessageHandler;
4443
import org.springframework.stereotype.Controller;
4544

@@ -111,7 +110,7 @@ private static void connectAndRunTest(String connectionRoute) {
111110
factory.metadataMimeType("text/plain");
112111
factory.setupPayload(ByteBufPayload.create("", connectionRoute));
113112
})
114-
.rsocketFactory(AnnotationClientResponderConfigurer.withHandlers(new ClientHandler()))
113+
.rsocketFactory(RSocketMessageHandler.clientResponder(new ClientHandler()))
115114
.rsocketStrategies(context.getBean(RSocketStrategies.class))
116115
.connectTcp("localhost", server.address().getPort())
117116
.block();

0 commit comments

Comments
 (0)