Top Description Fields Constructors Methods
org.springframework.messaging.rsocket.annotation.support

public Class RSocketMessageHandler

extends MessageMappingMessageHandler
Class Inheritance
Imports
java.lang.reflect.AnnotatedElement, java.util.ArrayList, .List, .Set, java.util.function.Predicate, java.util.stream.Collectors, io.rsocket.ConnectionSetupPayload, .RSocket, .SocketAcceptor, io.rsocket.frame.FrameType, io.rsocket.metadata.WellKnownMimeType, reactor.core.publisher.Mono, org.springframework.beans.BeanUtils, org.springframework.core.MethodParameter, .ReactiveAdapter, .ReactiveAdapterRegistry, org.springframework.core.annotation.AnnotatedElementUtils, org.springframework.core.codec.Decoder, .Encoder, org.springframework.lang.Nullable, org.springframework.messaging.Message, .MessageDeliveryException, org.springframework.messaging.handler.CompositeMessageCondition, .DestinationPatternsMessageCondition, .HandlerMethod, .MessageCondition, org.springframework.messaging.handler.annotation.MessageMapping, org.springframework.messaging.handler.annotation.reactive.MessageMappingMessageHandler, .PayloadMethodArgumentResolver, org.springframework.messaging.handler.invocation.reactive.HandlerMethodReturnValueHandler, org.springframework.messaging.rsocket.MetadataExtractor, .RSocketRequester, .RSocketStrategies, org.springframework.messaging.rsocket.annotation.ConnectMapping, org.springframework.messaging.rsocket.service.RSocketExchange, org.springframework.util.Assert, .MimeType, .MimeTypeUtils, .RouteMatcher, .StringUtils

Extension of MessageMappingMessageHandler to handle RSocket requests with @MessageMapping and @ConnectMapping methods, also supporting use of @RSocketExchange.

For server scenarios this class can be declared as a bean in Spring configuration and that would detect @MessageMapping methods in @Controller beans. What beans are checked can be changed through a handlerPredicate. Given an instance of this class, you can then use responder() to obtain a SocketAcceptor adapter to register with the io.rsocket.core.RSocketServer.

For a client, possibly in the same process as a server, consider using the static factory method responder(RSocketStrategies, Object...) to obtain a client responder to be registered via RSocketRequester.Builder.

For @MessageMapping and @RSocketExchange methods, this class automatically determines the RSocket interaction type based on the input and output cardinality of the method. See the "Annotated Responders" section of the Spring Framework reference for more details.

Authors
Rossen Stoyanchev, Olga Maciaszek-Sharma
Since
5.2

Field Summary

Modifier and TypeField and Description
private MimeType
private MimeType
private final List<Encoder<?>>
private RSocketStrategies

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
afterPropertiesSet()

Overrides org.springframework.messaging.handler.annotation.reactive.MessageMappingMessageHandler.afterPropertiesSet.

Implements org.springframework.beans.factory.InitializingBean.afterPropertiesSet.

Invoked by the containing BeanFactory after it has set all bean properties and satisfied BeanFactoryAware, ApplicationContextAware etc.

This method allows the bean instance to perform validation of its overall configuration and final initialization when all bean properties have been set.

private MessagingRSocket
protected CompositeMessageCondition
extendMapping(CompositeMessageCondition
the mapping to be added
composite
,
HandlerMethod
the target handler for the mapping
handler
)

Overrides org.springframework.messaging.handler.invocation.reactive.AbstractMethodMessageHandler.extendMapping.

This method is invoked just before mappings are added.
private int
protected CompositeMessageCondition
getCondition(AnnotatedElement
the element to check
element
)

Overrides org.springframework.messaging.handler.annotation.reactive.MessageMappingMessageHandler.getCondition.

Determine the mapping condition for the given annotated element.
public MimeType
getDefaultDataMimeType()

Return the configured defaultDataMimeType, or null.

public MimeType
public List<? extends Encoder<?>>
getEncoders()

Return the configured encoders.

public MetadataExtractor
getMetadataExtractor()

Return the configured MetadataExtractor.

public RSocketStrategies
getRSocketStrategies()

Return the configured RSocketStrategies.

protected void
handleNoMatch(RouteMatcher.Route
the destination
destination
,
Message<?>
the message
message
)

Overrides org.springframework.messaging.handler.invocation.reactive.AbstractMethodMessageHandler.handleNoMatch.

Invoked when no matching handler is found.
protected List<? extends HandlerMethodReturnValueHandler>
private boolean
public SocketAcceptor
responder()

Return an RSocket SocketAcceptor backed by this RSocketMessageHandler instance that can be plugged in as a client or server RSocket responder.

public static SocketAcceptor
responder(RSocketStrategies
the strategies to set on the created RSocketMessageHandler
strategies
,
Object...
a list of Objects and/or Classes with annotated handler methods; used to call setHandlers(List) with on the created RSocketMessageHandler
candidateHandlers
)

Static factory method to create an RSocket SocketAcceptor backed by handlers with annotated methods.

public void
setDecoders(List<? extends Decoder<?>> decoders)

Overrides org.springframework.messaging.handler.annotation.reactive.MessageMappingMessageHandler.setDecoders.

Configure the decoders to use for incoming payloads.
public void
setDefaultDataMimeType(MimeType
the MimeType to use
mimeType
)

Configure the default content type to use for data payloads if the SETUP frame did not specify one.

public void
setDefaultMetadataMimeType(MimeType
the MimeType to use
mimeType
)

Configure the default MimeType for payload data if the SETUP frame did not specify one.

public void
setEncoders(List<? extends Encoder<?>> encoders)

Configure the encoders to use for encoding handler method return values.

public void
setMetadataExtractor(MetadataExtractor
the extractor to use
extractor
)

Configure a MetadataExtractor to extract the route along with other metadata.

public void
public void
setRouteMatcher(RouteMatcher routeMatcher)

Overrides org.springframework.messaging.handler.annotation.reactive.MessageMappingMessageHandler.setRouteMatcher.

Set the RouteMatcher to use for mapping messages to handlers based on the route patterns they're configured with.
public void
setRSocketStrategies(RSocketStrategies rsocketStrategies)

Configure this handler through an RSocketStrategies instance which can be re-used to initialize a client-side RSocketRequester.

Inherited from org.springframework.messaging.handler.annotation.reactive.MessageMappingMessageHandler:
createExceptionMethodResolverForgetConversionServicegetDecodersgetDestinationgetDirectLookupMappingsgetMappingComparatorgetMappingForMethodgetMatchingMappinggetRouteMatchergetValidatorhandleMatchinitArgumentResolversobtainRouteMatcherprocessDestinationsregisterMessagingAdvicesetConversionServicesetEmbeddedValueResolversetValidator

Field Detail

defaultDataMimeTypeback to summary
private MimeType defaultDataMimeType
Annotations
@Nullable
defaultMetadataMimeTypeback to summary
private MimeType defaultMetadataMimeType
encodersback to summary
private final List<Encoder<?>> encoders
strategiesback to summary
private RSocketStrategies strategies

Constructor Detail

RSocketMessageHandlerback to summary
public RSocketMessageHandler()

Method Detail

afterPropertiesSetback to summary
public void afterPropertiesSet()

Overrides org.springframework.messaging.handler.annotation.reactive.MessageMappingMessageHandler.afterPropertiesSet.

Implements org.springframework.beans.factory.InitializingBean.afterPropertiesSet.

Doc from org.springframework.beans.factory.InitializingBean.afterPropertiesSet.

Invoked by the containing BeanFactory after it has set all bean properties and satisfied BeanFactoryAware, ApplicationContextAware etc.

This method allows the bean instance to perform validation of its overall configuration and final initialization when all bean properties have been set.

Annotations
@Override
createResponderback to summary
private MessagingRSocket createResponder(ConnectionSetupPayload setupPayload, RSocket rsocket)
extendMappingback to summary
protected CompositeMessageCondition extendMapping(CompositeMessageCondition composite, HandlerMethod handler)

Overrides org.springframework.messaging.handler.invocation.reactive.AbstractMethodMessageHandler.extendMapping.

Doc from org.springframework.messaging.handler.invocation.reactive.AbstractMethodMessageHandler.extendMapping.

This method is invoked just before mappings are added. It allows subclasses to update the mapping with the HandlerMethod in mind. This can be useful when the method signature is used to refine the mapping, e.g. based on the cardinality of input and output.

By default this method returns the mapping that is passed in.

Parameters
composite:CompositeMessageCondition

the mapping to be added

handler:HandlerMethod

the target handler for the mapping

Returns:CompositeMessageCondition

a new mapping or the same

Annotations
@Override
getCardinalityback to summary
private int getCardinality(MethodParameter parameter)
getConditionback to summary
protected CompositeMessageCondition getCondition(AnnotatedElement element)

Overrides org.springframework.messaging.handler.annotation.reactive.MessageMappingMessageHandler.getCondition.

Doc from org.springframework.messaging.handler.annotation.reactive.MessageMappingMessageHandler.getCondition.

Determine the mapping condition for the given annotated element.

Parameters
element:AnnotatedElement

the element to check

Returns:CompositeMessageCondition

the condition, or null

Annotations
@Override
@Nullable
getDefaultDataMimeTypeback to summary
public MimeType getDefaultDataMimeType()

Return the configured defaultDataMimeType, or null.

Annotations
@Nullable
getDefaultMetadataMimeTypeback to summary
public MimeType getDefaultMetadataMimeType()

Return the configured defaultMetadataMimeType.

getEncodersback to summary
public List<? extends Encoder<?>> getEncoders()

Return the configured encoders.

getMetadataExtractorback to summary
public MetadataExtractor getMetadataExtractor()

Return the configured MetadataExtractor.

getRSocketStrategiesback to summary
public RSocketStrategies getRSocketStrategies()

Return the configured RSocketStrategies.

handleNoMatchback to summary
protected void handleNoMatch(RouteMatcher.Route destination, Message<?> message)

Overrides org.springframework.messaging.handler.invocation.reactive.AbstractMethodMessageHandler.handleNoMatch.

Doc from org.springframework.messaging.handler.invocation.reactive.AbstractMethodMessageHandler.handleNoMatch.

Invoked when no matching handler is found.

Parameters
destination:RouteMatcher.Route

the destination

message:Message<?>

the message

Annotations
@Override
initReturnValueHandlersback to summary
protected List<? extends HandlerMethodReturnValueHandler> initReturnValueHandlers()

Overrides org.springframework.messaging.handler.annotation.reactive.MessageMappingMessageHandler.initReturnValueHandlers.

Doc from org.springframework.messaging.handler.invocation.reactive.AbstractMethodMessageHandler.initReturnValueHandlers.

Return the list of return value handlers to use.

Subclasses should also take into account custom return value types configured via setReturnValueHandlerConfigurer.

Annotations
@Override
isDataMimeTypeSupportedback to summary
private boolean isDataMimeTypeSupported(MimeType dataMimeType)
responderback to summary
public SocketAcceptor responder()

Return an RSocket SocketAcceptor backed by this RSocketMessageHandler instance that can be plugged in as a client or server RSocket responder.

The initial ConnectionSetupPayload is handled through @ConnectionMapping methods that can be asynchronous and return Mono<Void> with an error signal preventing the connection. Such a method can also start requests to the client but that must be done decoupled from handling and from the current thread.

Subsequent requests on the connection can be handled with MessageMapping and RSocketExchange methods.

responderback to summary
public static SocketAcceptor responder(RSocketStrategies strategies, Object... candidateHandlers)

Static factory method to create an RSocket SocketAcceptor backed by handlers with annotated methods. Effectively a shortcut for:

RSocketMessageHandler handler = new RSocketMessageHandler();
handler.setHandlers(handlers);
handler.setRSocketStrategies(strategies);
handler.afterPropertiesSet();

SocketAcceptor acceptor = handler.responder();

This is intended for programmatic creation and registration of a client-side responder. For example:

SocketAcceptor responder =
        RSocketMessageHandler.responder(strategies, new ClientHandler());

RSocketRequester.builder()
        .rsocketConnector(connector -> connector.acceptor(responder))
        .connectTcp("localhost", server.address().getPort());

Note that the given handlers do not need to have any stereotype annotations such as @Controller which helps to avoid overlap with server side handlers that may be used in the same application. However, for more advanced scenarios, e.g. discovering handlers through a custom stereotype annotation, consider declaring RSocketMessageHandler as a bean, and then obtain the responder from it.

Parameters
strategies:RSocketStrategies

the strategies to set on the created RSocketMessageHandler

candidateHandlers:Object[]

a list of Objects and/or Classes with annotated handler methods; used to call setHandlers(List) with on the created RSocketMessageHandler

Returns:SocketAcceptor

a configurer that may be passed into org.springframework.messaging.rsocket.RSocketRequester.Builder#rsocketConnector

Since
5.2.6
setDecodersback to summary
public void setDecoders(List<? extends Decoder<?>> decoders)

Overrides org.springframework.messaging.handler.annotation.reactive.MessageMappingMessageHandler.setDecoders.

Doc from org.springframework.messaging.handler.annotation.reactive.MessageMappingMessageHandler.setDecoders.

Configure the decoders to use for incoming payloads.

When rsocketStrategies is set, this property is re-initialized with the decoders in it, and likewise when this property is set the RSocketStrategies are mutated to change the decoders in them.

By default this is set to the defaults from RSocketStrategies.

Annotations
@Override
setDefaultDataMimeTypeback to summary
public void setDefaultDataMimeType(MimeType mimeType)

Configure the default content type to use for data payloads if the SETUP frame did not specify one.

By default this is not set.

Parameters
mimeType:MimeType

the MimeType to use

setDefaultMetadataMimeTypeback to summary
public void setDefaultMetadataMimeType(MimeType mimeType)

Configure the default MimeType for payload data if the SETUP frame did not specify one.

By default this is set to "message/x.rsocket.composite-metadata.v0"

Parameters
mimeType:MimeType

the MimeType to use

setEncodersback to summary
public void setEncoders(List<? extends Encoder<?>> encoders)

Configure the encoders to use for encoding handler method return values.

When rsocketStrategies is set, this property is re-initialized with the encoders in it, and likewise when this property is set the RSocketStrategies are mutated to change the encoders in it.

By default this is set to the defaults from RSocketStrategies.

setMetadataExtractorback to summary
public void setMetadataExtractor(MetadataExtractor extractor)

Configure a MetadataExtractor to extract the route along with other metadata.

When rsocketStrategies is set, this property is re-initialized with the extractor in it, and likewise when this property is set the RSocketStrategies are mutated to change the extractor in it.

By default this is set to the defaults from RSocketStrategies.

Parameters
extractor:MetadataExtractor

the extractor to use

setReactiveAdapterRegistryback to summary
public void setReactiveAdapterRegistry(ReactiveAdapterRegistry registry)

Overrides org.springframework.messaging.handler.invocation.reactive.AbstractMethodMessageHandler.setReactiveAdapterRegistry.

Configure the registry for adapting various reactive types.

When rsocketStrategies is set, this property is re-initialized with the registry in it, and likewise when this property is set the RSocketStrategies are mutated to change the registry in it.

By default this is set to the defaults from RSocketStrategies.

Annotations
@Override
setRouteMatcherback to summary
public void setRouteMatcher(RouteMatcher routeMatcher)

Overrides org.springframework.messaging.handler.annotation.reactive.MessageMappingMessageHandler.setRouteMatcher.

Doc from org.springframework.messaging.handler.annotation.reactive.MessageMappingMessageHandler.setRouteMatcher.

Set the RouteMatcher to use for mapping messages to handlers based on the route patterns they're configured with.

By default, SimpleRouteMatcher is used, backed by AntPathMatcher with "." as separator. For greater efficiency consider using the PathPatternRouteMatcher from spring-web instead.

When rsocketStrategies is set, this property is re-initialized with the route matcher in it, and likewise when this property is set the RSocketStrategies are mutated to change the matcher in it.

By default this is set to the defaults from RSocketStrategies.

Annotations
@Override
setRSocketStrategiesback to summary
public void setRSocketStrategies(RSocketStrategies rsocketStrategies)

Configure this handler through an RSocketStrategies instance which can be re-used to initialize a client-side RSocketRequester.

When this property is set, in turn it sets the following:

By default this is set to RSocketStrategies#create() which in turn sets default settings for all related properties.