Split AbstractMessagingTemplate across send/receive
This commit is contained in:
@@ -3,23 +3,16 @@ package org.springframework.web.messaging.support;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.MessageChannel;
|
||||
import org.springframework.messaging.MessageDeliveryException;
|
||||
import org.springframework.messaging.converter.DefaultMessageConverter;
|
||||
import org.springframework.messaging.converter.MessageConverter;
|
||||
import org.springframework.messaging.core.MessagePostProcessor;
|
||||
import org.springframework.messaging.core.MessageSendingOperations;
|
||||
import org.springframework.messaging.core.AbstractMessagingTemplate;
|
||||
import org.springframework.messaging.support.MessageBuilder;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.web.messaging.MessageType;
|
||||
|
||||
|
||||
public class WebMessagingTemplate implements MessageSendingOperations<String> {
|
||||
public class WebMessagingTemplate extends AbstractMessagingTemplate<String> {
|
||||
|
||||
private final MessageChannel outputChannel;
|
||||
|
||||
protected volatile MessageConverter converter = new DefaultMessageConverter();
|
||||
|
||||
private volatile String defaultDestination;
|
||||
|
||||
private volatile long sendTimeout = -1;
|
||||
|
||||
|
||||
@@ -29,16 +22,6 @@ public class WebMessagingTemplate implements MessageSendingOperations<String> {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Set the {@link MessageConverter} that is to be used to convert
|
||||
* between Messages and objects for this template.
|
||||
* <p>The default is {@link SimpleMessageConverter}.
|
||||
*/
|
||||
public void setMessageConverter(MessageConverter messageConverter) {
|
||||
Assert.notNull(messageConverter, "'messageConverter' must not be null");
|
||||
this.converter = messageConverter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify the timeout value to use for send operations.
|
||||
*
|
||||
@@ -48,72 +31,34 @@ public class WebMessagingTemplate implements MessageSendingOperations<String> {
|
||||
this.sendTimeout = sendTimeout;
|
||||
}
|
||||
|
||||
public void setDefaultDestination(String defaultDestination) {
|
||||
this.defaultDestination = defaultDestination;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public <P> void send(Message<P> message) {
|
||||
// TODO: maybe look up destination of current message (via ThreadLocal)
|
||||
this.send(getRequiredDefaultDestination(), message);
|
||||
}
|
||||
|
||||
private String getRequiredDefaultDestination() {
|
||||
|
||||
// TODO: maybe look up destination of current message (via ThreadLocal)
|
||||
|
||||
Assert.state(this.defaultDestination != null,
|
||||
"No 'defaultDestination' specified for WebMessagingTemplate. " +
|
||||
"Unable to invoke method without an explicit destination argument.");
|
||||
|
||||
return this.defaultDestination;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <P> void send(String destinationName, Message<P> message) {
|
||||
Assert.notNull(destinationName, "destinationName is required");
|
||||
message = addDestinationToMessage(message, destinationName);
|
||||
protected void doSend(String destination, Message<?> message) {
|
||||
Assert.notNull(destination, "destination is required");
|
||||
message = addDestinationToMessage(message, destination);
|
||||
long timeout = this.sendTimeout;
|
||||
boolean sent = (timeout >= 0)
|
||||
? this.outputChannel.send(message, timeout)
|
||||
: this.outputChannel.send(message);
|
||||
if (!sent) {
|
||||
throw new MessageDeliveryException(message,
|
||||
"failed to send message to destination '" + destinationName + "' within timeout: " + timeout);
|
||||
"failed to send message to destination '" + destination + "' within timeout: " + timeout);
|
||||
}
|
||||
}
|
||||
|
||||
protected <P> Message<P> addDestinationToMessage(Message<P> message, String destinationName) {
|
||||
Assert.notNull(destinationName, "destinationName is required");
|
||||
protected <P> Message<P> addDestinationToMessage(Message<P> message, String destination) {
|
||||
Assert.notNull(destination, "destination is required");
|
||||
WebMessageHeaderAccesssor headers = WebMessageHeaderAccesssor.create(MessageType.MESSAGE);
|
||||
headers.copyHeaders(message.getHeaders());
|
||||
headers.setDestination(destinationName);
|
||||
headers.setDestination(destination);
|
||||
message = MessageBuilder.withPayload(message.getPayload()).copyHeaders(headers.toMap()).build();
|
||||
return message;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> void convertAndSend(T object) {
|
||||
this.convertAndSend(getRequiredDefaultDestination(), object);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> void convertAndSend(String destinationName, T object) {
|
||||
this.convertAndSend(destinationName, object, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> void convertAndSend(T object, MessagePostProcessor postProcessor) {
|
||||
this.convertAndSend(getRequiredDefaultDestination(), object, postProcessor);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> void convertAndSend(String destinationName, T object, MessagePostProcessor postProcessor) {
|
||||
Message<?> message = this.converter.toMessage(object);
|
||||
if (postProcessor != null) {
|
||||
message = postProcessor.postProcessMessage(message);
|
||||
}
|
||||
this.send(destinationName, message);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user