Browse Source

Polish in rsocket package

pull/23057/head
Rossen Stoyanchev 6 years ago
parent
commit
64f3dbbee8
  1. 44
      spring-messaging/src/main/java/org/springframework/messaging/rsocket/DefaultRSocketRequesterBuilder.java
  2. 9
      spring-messaging/src/main/java/org/springframework/messaging/rsocket/MessageHandlerAcceptor.java
  3. 34
      spring-messaging/src/main/java/org/springframework/messaging/rsocket/MessagingRSocket.java
  4. 4
      spring-messaging/src/main/java/org/springframework/messaging/rsocket/RSocketRequester.java
  5. 2
      spring-messaging/src/main/java/org/springframework/messaging/rsocket/RSocketStrategies.java

44
spring-messaging/src/main/java/org/springframework/messaging/rsocket/DefaultRSocketRequesterBuilder.java

@ -34,6 +34,7 @@ import org.springframework.util.MimeType;
* Default implementation of {@link RSocketRequester.Builder}. * Default implementation of {@link RSocketRequester.Builder}.
* *
* @author Brian Clozel * @author Brian Clozel
* @author Rossen Stoyanchev
* @since 5.2 * @since 5.2
*/ */
final class DefaultRSocketRequesterBuilder implements RSocketRequester.Builder { final class DefaultRSocketRequesterBuilder implements RSocketRequester.Builder {
@ -76,29 +77,36 @@ final class DefaultRSocketRequesterBuilder implements RSocketRequester.Builder {
@Override @Override
public Mono<RSocketRequester> connect(ClientTransport transport) { public Mono<RSocketRequester> connect(ClientTransport transport) {
return Mono.defer(() -> { return Mono.defer(() -> doConnect(transport));
RSocketStrategies strategies = getRSocketStrategies(); }
MimeType dataMimeType = getDefaultDataMimeType(strategies);
private Mono<RSocketRequester> doConnect(ClientTransport transport) {
RSocketFactory.ClientRSocketFactory factory = RSocketFactory.connect(); RSocketStrategies rsocketStrategies = getRSocketStrategies();
if (dataMimeType != null) { RSocketFactory.ClientRSocketFactory rsocketFactory = RSocketFactory.connect();
factory.dataMimeType(dataMimeType.toString());
} // 1. Apply default settings
this.factoryConfigurers.forEach(configurer -> configurer.accept(factory)); MimeType dataMimeType = getDefaultDataMimeType(rsocketStrategies);
if (dataMimeType != null) {
return factory.transport(transport).start() rsocketFactory.dataMimeType(dataMimeType.toString());
.map(rsocket -> new DefaultRSocketRequester(rsocket, dataMimeType, strategies)); }
});
// 2. Application customizations
this.factoryConfigurers.forEach(c -> c.accept(rsocketFactory));
return rsocketFactory.transport(transport).start()
.map(rsocket -> new DefaultRSocketRequester(rsocket, dataMimeType, rsocketStrategies));
} }
private RSocketStrategies getRSocketStrategies() { private RSocketStrategies getRSocketStrategies() {
if (this.strategiesConfigurers.isEmpty()) { if (!this.strategiesConfigurers.isEmpty()) {
RSocketStrategies.Builder builder =
this.strategies != null ? this.strategies.mutate() : RSocketStrategies.builder();
this.strategiesConfigurers.forEach(c -> c.accept(builder));
return builder.build();
}
else {
return this.strategies != null ? this.strategies : RSocketStrategies.builder().build(); return this.strategies != null ? this.strategies : RSocketStrategies.builder().build();
} }
RSocketStrategies.Builder strategiesBuilder = this.strategies != null ?
this.strategies.mutate() : RSocketStrategies.builder();
this.strategiesConfigurers.forEach(configurer -> configurer.accept(strategiesBuilder));
return strategiesBuilder.build();
} }
@Nullable @Nullable

9
spring-messaging/src/main/java/org/springframework/messaging/rsocket/MessageHandlerAcceptor.java

@ -24,6 +24,7 @@ import io.rsocket.RSocket;
import io.rsocket.SocketAcceptor; import io.rsocket.SocketAcceptor;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
import org.springframework.messaging.Message; import org.springframework.messaging.Message;
import org.springframework.util.MimeType; import org.springframework.util.MimeType;
@ -78,11 +79,9 @@ public final class MessageHandlerAcceptor extends RSocketMessageHandler
MimeType dataMimeType = StringUtils.hasText(setupPayload.dataMimeType()) ? MimeType dataMimeType = StringUtils.hasText(setupPayload.dataMimeType()) ?
MimeTypeUtils.parseMimeType(setupPayload.dataMimeType()) : MimeTypeUtils.parseMimeType(setupPayload.dataMimeType()) :
this.defaultDataMimeType; this.defaultDataMimeType;
return new MessagingRSocket(this::handleMessage, RSocketRequester requester = RSocketRequester.wrap(rsocket, dataMimeType, getRSocketStrategies());
route -> getRouteMatcher().parseRoute(route), DataBufferFactory bufferFactory = getRSocketStrategies().dataBufferFactory();
RSocketRequester.wrap(rsocket, dataMimeType, getRSocketStrategies()), return new MessagingRSocket(this, getRouteMatcher(), requester, dataMimeType, bufferFactory);
dataMimeType,
getRSocketStrategies().dataBufferFactory());
} }
} }

34
spring-messaging/src/main/java/org/springframework/messaging/rsocket/MessagingRSocket.java

@ -44,18 +44,20 @@ import org.springframework.util.MimeType;
import org.springframework.util.RouteMatcher; import org.springframework.util.RouteMatcher;
/** /**
* Implementation of {@link RSocket} that wraps incoming requests with a * Responder {@link RSocket} that wraps the payload and metadata of incoming
* {@link Message}, delegates to a {@link Function} for handling, and then * requests as a {@link Message} and then delegates to the configured
* obtains the response from a "reply" header. * {@link RSocketMessageHandler} to handle it. The response, if applicable, is
* obtained from the {@link RSocketPayloadReturnValueHandler#RESPONSE_HEADER
* "rsocketResponse"} header.
* *
* @author Rossen Stoyanchev * @author Rossen Stoyanchev
* @since 5.2 * @since 5.2
*/ */
class MessagingRSocket extends AbstractRSocket { class MessagingRSocket extends AbstractRSocket {
private final Function<Message<?>, Mono<Void>> handler; private final RSocketMessageHandler messageHandler;
private final Function<String, RouteMatcher.Route> routeParser; private final RouteMatcher routeMatcher;
private final RSocketRequester requester; private final RSocketRequester requester;
@ -65,16 +67,16 @@ class MessagingRSocket extends AbstractRSocket {
private final DataBufferFactory bufferFactory; private final DataBufferFactory bufferFactory;
MessagingRSocket(Function<Message<?>, Mono<Void>> handler, MessagingRSocket(RSocketMessageHandler messageHandler, RouteMatcher routeMatcher,
Function<String, RouteMatcher.Route> routeParser, RSocketRequester requester, RSocketRequester requester, @Nullable MimeType defaultDataMimeType,
@Nullable MimeType defaultDataMimeType, DataBufferFactory bufferFactory) { DataBufferFactory bufferFactory) {
this.routeParser = routeParser; Assert.notNull(messageHandler, "'messageHandler' is required");
Assert.notNull(routeMatcher, "'routeMatcher' is required");
Assert.notNull(handler, "'handler' is required");
Assert.notNull(routeParser, "'routeParser' is required");
Assert.notNull(requester, "'requester' is required"); Assert.notNull(requester, "'requester' is required");
this.handler = handler;
this.messageHandler = messageHandler;
this.routeMatcher = routeMatcher;
this.requester = requester; this.requester = requester;
this.dataMimeType = defaultDataMimeType; this.dataMimeType = defaultDataMimeType;
this.bufferFactory = bufferFactory; this.bufferFactory = bufferFactory;
@ -132,7 +134,7 @@ class MessagingRSocket extends AbstractRSocket {
DataBuffer dataBuffer = retainDataAndReleasePayload(payload); DataBuffer dataBuffer = retainDataAndReleasePayload(payload);
int refCount = refCount(dataBuffer); int refCount = refCount(dataBuffer);
Message<?> message = MessageBuilder.createMessage(dataBuffer, headers); Message<?> message = MessageBuilder.createMessage(dataBuffer, headers);
return Mono.defer(() -> this.handler.apply(message)) return Mono.defer(() -> this.messageHandler.handleMessage(message))
.doFinally(s -> { .doFinally(s -> {
if (refCount(dataBuffer) == refCount) { if (refCount(dataBuffer) == refCount) {
DataBufferUtils.release(dataBuffer); DataBufferUtils.release(dataBuffer);
@ -154,7 +156,7 @@ class MessagingRSocket extends AbstractRSocket {
Flux<DataBuffer> buffers = payloads.map(this::retainDataAndReleasePayload).doOnSubscribe(s -> read.set(true)); Flux<DataBuffer> buffers = payloads.map(this::retainDataAndReleasePayload).doOnSubscribe(s -> read.set(true));
Message<Flux<DataBuffer>> message = MessageBuilder.createMessage(buffers, headers); Message<Flux<DataBuffer>> message = MessageBuilder.createMessage(buffers, headers);
return Mono.defer(() -> this.handler.apply(message)) return Mono.defer(() -> this.messageHandler.handleMessage(message))
.doFinally(s -> { .doFinally(s -> {
// Subscription should have happened by now due to ChannelSendOperator // Subscription should have happened by now due to ChannelSendOperator
if (!read.get()) { if (!read.get()) {
@ -183,7 +185,7 @@ class MessagingRSocket extends AbstractRSocket {
private MessageHeaders createHeaders(String destination, @Nullable MonoProcessor<?> replyMono) { private MessageHeaders createHeaders(String destination, @Nullable MonoProcessor<?> replyMono) {
MessageHeaderAccessor headers = new MessageHeaderAccessor(); MessageHeaderAccessor headers = new MessageHeaderAccessor();
headers.setLeaveMutable(true); headers.setLeaveMutable(true);
RouteMatcher.Route route = this.routeParser.apply(destination); RouteMatcher.Route route = this.routeMatcher.parseRoute(destination);
headers.setHeader(DestinationPatternsMessageCondition.LOOKUP_DESTINATION_HEADER, route); headers.setHeader(DestinationPatternsMessageCondition.LOOKUP_DESTINATION_HEADER, route);
if (this.dataMimeType != null) { if (this.dataMimeType != null) {
headers.setContentType(this.dataMimeType); headers.setContentType(this.dataMimeType);

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

@ -116,8 +116,8 @@ public interface RSocketRequester {
RSocketRequester.Builder rsocketFactory(Consumer<RSocketFactory.ClientRSocketFactory> configurer); RSocketRequester.Builder rsocketFactory(Consumer<RSocketFactory.ClientRSocketFactory> configurer);
/** /**
* Set the {@link RSocketStrategies} instance. * Set the {@link RSocketStrategies} to use for access to encoders,
* @param strategies the strategies to use * decoders, and a factory for {@code DataBuffer's}.
*/ */
RSocketRequester.Builder rsocketStrategies(@Nullable RSocketStrategies strategies); RSocketRequester.Builder rsocketStrategies(@Nullable RSocketStrategies strategies);

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

@ -172,7 +172,7 @@ public interface RSocketStrategies {
Builder dataBufferFactory(DataBufferFactory bufferFactory); Builder dataBufferFactory(DataBufferFactory bufferFactory);
/** /**
* Builder the {@code RSocketStrategies} instance. * Build the {@code RSocketStrategies} instance.
*/ */
RSocketStrategies build(); RSocketStrategies build();
} }

Loading…
Cancel
Save