public class SubscriptionMethodReturnValueHandler extends java.lang.Object implements HandlerMethodReturnValueHandler
HandlerMethodReturnValueHandler
for replying directly to a
subscription. It is supported on methods annotated with
SubscribeMapping
such that the return value is treated as a response to be
sent directly back on the session. This allows a client to implement
a request-response pattern and use it for example to obtain some data upon
initialization.
The value returned from the method is converted and turned into a
Message
that is then enriched with the sessionId, subscriptionId, and
destination of the input message.
Note: this default behavior for interpreting the return
value from an @SubscribeMapping
method can be overridden through use
of the SendTo
or SendToUser
annotations in which case a
message is prepared and sent to the broker instead.
Modifier and Type | Field and Description |
---|---|
private MessageHeaderInitializer |
headerInitializer |
private static Log |
logger |
private MessageSendingOperations<java.lang.String> |
messagingTemplate |
Constructor and Description |
---|
SubscriptionMethodReturnValueHandler(MessageSendingOperations<java.lang.String> template)
Construct a new SubscriptionMethodReturnValueHandler.
|
Modifier and Type | Method and Description |
---|---|
private MessageHeaders |
createHeaders(java.lang.String sessionId,
java.lang.String subscriptionId,
MethodParameter returnType) |
MessageHeaderInitializer |
getHeaderInitializer()
Return the configured header initializer.
|
void |
handleReturnValue(java.lang.Object returnValue,
MethodParameter returnType,
Message<?> message)
Handle the given return value.
|
void |
setHeaderInitializer(MessageHeaderInitializer headerInitializer)
Configure a
MessageHeaderInitializer to apply to the headers of all
messages sent to the client outbound channel. |
boolean |
supportsReturnType(MethodParameter returnType)
Whether the given method return type is
supported by this handler.
|
private static final Log logger
private final MessageSendingOperations<java.lang.String> messagingTemplate
private MessageHeaderInitializer headerInitializer
public SubscriptionMethodReturnValueHandler(MessageSendingOperations<java.lang.String> template)
template
- a messaging template to send messages to,
most likely the "clientOutboundChannel" (must not be null
)public void setHeaderInitializer(MessageHeaderInitializer headerInitializer)
MessageHeaderInitializer
to apply to the headers of all
messages sent to the client outbound channel.
By default this property is not set.
public MessageHeaderInitializer getHeaderInitializer()
public boolean supportsReturnType(MethodParameter returnType)
HandlerMethodReturnValueHandler
supportsReturnType
in interface HandlerMethodReturnValueHandler
returnType
- the method return type to checktrue
if this handler supports the supplied return type;
false
otherwisepublic void handleReturnValue(java.lang.Object returnValue, MethodParameter returnType, Message<?> message) throws java.lang.Exception
HandlerMethodReturnValueHandler
handleReturnValue
in interface HandlerMethodReturnValueHandler
returnValue
- the value returned from the handler methodreturnType
- the type of the return value. This type must have
previously been passed to
HandlerMethodReturnValueHandler.supportsReturnType(org.springframework.core.MethodParameter)
and it must have returned true
message
- the message that caused this method to be calledjava.lang.Exception
- if the return value handling results in an errorprivate MessageHeaders createHeaders(java.lang.String sessionId, java.lang.String subscriptionId, MethodParameter returnType)