Browse Source

MessageConversionException offers constructor without cause argument now, plus related polishing

Issue: SPR-11653
pull/516/head
Juergen Hoeller 12 years ago
parent
commit
28887750b0
  1. 5
      spring-messaging/src/main/java/org/springframework/messaging/converter/MessageConversionException.java
  2. 25
      spring-messaging/src/main/java/org/springframework/messaging/core/AbstractDestinationResolvingMessagingTemplate.java
  3. 51
      spring-messaging/src/main/java/org/springframework/messaging/core/AbstractMessageSendingTemplate.java
  4. 47
      spring-messaging/src/main/java/org/springframework/messaging/core/AbstractMessagingTemplate.java
  5. 38
      spring-messaging/src/main/java/org/springframework/messaging/core/GenericMessagingTemplate.java
  6. 28
      spring-messaging/src/main/java/org/springframework/messaging/simp/SimpMessagingTemplate.java

5
spring-messaging/src/main/java/org/springframework/messaging/converter/MessageConversionException.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2013 the original author or authors. * Copyright 2002-2014 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -28,6 +28,9 @@ import org.springframework.messaging.MessagingException;
@SuppressWarnings("serial") @SuppressWarnings("serial")
public class MessageConversionException extends MessagingException { public class MessageConversionException extends MessagingException {
public MessageConversionException(String description) {
super(description);
}
public MessageConversionException(String description, Throwable cause) { public MessageConversionException(String description, Throwable cause) {
super(description, cause); super(description, cause);

25
spring-messaging/src/main/java/org/springframework/messaging/core/AbstractDestinationResolvingMessagingTemplate.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2013 the original author or authors. * Copyright 2002-2014 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -25,9 +25,9 @@ import org.springframework.util.Assert;
* An extension of {@link AbstractMessagingTemplate} that adds operations for sending * An extension of {@link AbstractMessagingTemplate} that adds operations for sending
* messages to a resolvable destination name as defined by the following interfaces: * messages to a resolvable destination name as defined by the following interfaces:
* <ul> * <ul>
* <li>{@link DestinationResolvingMessageSendingOperations}</li> * <li>{@link DestinationResolvingMessageSendingOperations}</li>
* <li>{@link DestinationResolvingMessageReceivingOperations}</li> * <li>{@link DestinationResolvingMessageReceivingOperations}</li>
* <li>{@link DestinationResolvingMessageRequestReplyOperations}</li> * <li>{@link DestinationResolvingMessageRequestReplyOperations}</li>
* </ul> * </ul>
* *
* @author Mark Fisher * @author Mark Fisher
@ -65,36 +65,31 @@ public abstract class AbstractDestinationResolvingMessagingTemplate<D> extends A
@Override @Override
public void send(String destinationName, Message<?> message) { public void send(String destinationName, Message<?> message) {
D destination = resolveDestination(destinationName); D destination = resolveDestination(destinationName);
this.doSend(destination, message); doSend(destination, message);
} }
protected final D resolveDestination(String destinationName) { protected final D resolveDestination(String destinationName) {
Assert.state(this.destinationResolver != null, "destinationResolver is required to resolve destination names"); Assert.state(this.destinationResolver != null, "DestinationResolver is required to resolve destination names");
return this.destinationResolver.resolveDestination(destinationName); return this.destinationResolver.resolveDestination(destinationName);
} }
@Override @Override
public <T> void convertAndSend(String destinationName, T payload) { public <T> void convertAndSend(String destinationName, T payload) {
Map<String, Object> headers = null; convertAndSend(destinationName, payload, null, null);
this.convertAndSend(destinationName, payload, headers);
} }
@Override @Override
public <T> void convertAndSend(String destinationName, T payload, Map<String, Object> headers) { public <T> void convertAndSend(String destinationName, T payload, Map<String, Object> headers) {
MessagePostProcessor postProcessor = null; convertAndSend(destinationName, payload, headers, null);
this.convertAndSend(destinationName, payload, headers, postProcessor);
} }
@Override @Override
public <T> void convertAndSend(String destinationName, T payload, MessagePostProcessor postProcessor) { public <T> void convertAndSend(String destinationName, T payload, MessagePostProcessor postProcessor) {
Map<String, Object> headers = null; convertAndSend(destinationName, payload, null, postProcessor);
this.convertAndSend(destinationName, payload, headers, postProcessor);
} }
@Override @Override
public <T> void convertAndSend(String destinationName, T payload, Map<String, Object> headers, public <T> void convertAndSend(String destinationName, T payload, Map<String, Object> headers, MessagePostProcessor postProcessor) {
MessagePostProcessor postProcessor) {
D destination = resolveDestination(destinationName); D destination = resolveDestination(destinationName);
super.convertAndSend(destination, payload, headers, postProcessor); super.convertAndSend(destination, payload, headers, postProcessor);
} }

51
spring-messaging/src/main/java/org/springframework/messaging/core/AbstractMessageSendingTemplate.java

@ -20,6 +20,7 @@ import java.util.Map;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.springframework.messaging.Message; import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders; import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.MessagingException; import org.springframework.messaging.MessagingException;
@ -37,7 +38,7 @@ import org.springframework.util.Assert;
*/ */
public abstract class AbstractMessageSendingTemplate<D> implements MessageSendingOperations<D> { public abstract class AbstractMessageSendingTemplate<D> implements MessageSendingOperations<D> {
protected final Log logger = LogFactory.getLog(this.getClass()); protected final Log logger = LogFactory.getLog(getClass());
private volatile D defaultDestination; private volatile D defaultDestination;
@ -80,17 +81,17 @@ public abstract class AbstractMessageSendingTemplate<D> implements MessageSendin
@Override @Override
public void send(Message<?> message) { public void send(Message<?> message) {
this.send(getRequiredDefaultDestination(), message); send(getRequiredDefaultDestination(), message);
} }
protected final D getRequiredDefaultDestination() { protected final D getRequiredDefaultDestination() {
Assert.state(this.defaultDestination != null, "No 'defaultDestination' configured."); Assert.state(this.defaultDestination != null, "No 'defaultDestination' configured");
return this.defaultDestination; return this.defaultDestination;
} }
@Override @Override
public void send(D destination, Message<?> message) { public void send(D destination, Message<?> message) {
this.doSend(destination, message); doSend(destination, message);
} }
protected abstract void doSend(D destination, Message<?> message); protected abstract void doSend(D destination, Message<?> message);
@ -98,29 +99,29 @@ public abstract class AbstractMessageSendingTemplate<D> implements MessageSendin
@Override @Override
public void convertAndSend(Object payload) throws MessagingException { public void convertAndSend(Object payload) throws MessagingException {
this.convertAndSend(getRequiredDefaultDestination(), payload); convertAndSend(getRequiredDefaultDestination(), payload);
} }
@Override @Override
public void convertAndSend(D destination, Object payload) throws MessagingException { public void convertAndSend(D destination, Object payload) throws MessagingException {
this.convertAndSend(destination, payload, (Map<String, Object>) null); convertAndSend(destination, payload, (Map<String, Object>) null);
} }
@Override @Override
public void convertAndSend(D destination, Object payload, Map<String, Object> headers) throws MessagingException { public void convertAndSend(D destination, Object payload, Map<String, Object> headers) throws MessagingException {
this.convertAndSend(destination, payload, headers, null); convertAndSend(destination, payload, headers, null);
} }
@Override @Override
public void convertAndSend(Object payload, MessagePostProcessor postProcessor) throws MessagingException { public void convertAndSend(Object payload, MessagePostProcessor postProcessor) throws MessagingException {
this.convertAndSend(getRequiredDefaultDestination(), payload, postProcessor); convertAndSend(getRequiredDefaultDestination(), payload, postProcessor);
} }
@Override @Override
public void convertAndSend(D destination, Object payload, MessagePostProcessor postProcessor) public void convertAndSend(D destination, Object payload, MessagePostProcessor postProcessor)
throws MessagingException { throws MessagingException {
this.convertAndSend(destination, payload, null, postProcessor); convertAndSend(destination, payload, null, postProcessor);
} }
@Override @Override
@ -128,41 +129,35 @@ public abstract class AbstractMessageSendingTemplate<D> implements MessageSendin
MessagePostProcessor postProcessor) throws MessagingException { MessagePostProcessor postProcessor) throws MessagingException {
MessageHeaders messageHeaders = null; MessageHeaders messageHeaders = null;
Map<String, Object> headersToUse = processHeadersToSend(headers);
headers = processHeadersToSend(headers); if (headersToUse != null) {
if (headers != null) { if (headersToUse instanceof MessageHeaders) {
if (headers instanceof MessageHeaders) { messageHeaders = (MessageHeaders) headersToUse;
messageHeaders = (MessageHeaders) headers;
} }
else { else {
messageHeaders = new MessageHeaders(headers); messageHeaders = new MessageHeaders(headersToUse);
} }
} }
Message<?> message = this.converter.toMessage(payload, messageHeaders); Message<?> message = getMessageConverter().toMessage(payload, messageHeaders);
if (message == null) { if (message == null) {
String payloadType = (payload != null) ? payload.getClass().getName() : null; String payloadType = (payload != null ? payload.getClass().getName() : null);
Object contentType = (messageHeaders != null) ? messageHeaders.get(MessageHeaders.CONTENT_TYPE) : null; Object contentType = (messageHeaders != null ? messageHeaders.get(MessageHeaders.CONTENT_TYPE) : null);
throw new MessageConversionException("Unable to convert payload type '" throw new MessageConversionException("Unable to convert payload with type='" + payloadType +
+ payloadType + "', Content-Type=" + contentType + ", converter=" + this.converter, null); "', contentType='" + contentType + "', converter=[" + getMessageConverter() + "]");
} }
if (postProcessor != null) { if (postProcessor != null) {
message = postProcessor.postProcessMessage(message); message = postProcessor.postProcessMessage(message);
} }
send(destination, message);
this.send(destination, message);
} }
/** /**
* Provides access to the map of input headers before a send operation. Sub-classes * Provides access to the map of input headers before a send operation. Sub-classes
* can modify the headers and then return the same or a different map. * can modify the headers and then return the same or a different map.
*
* <p>This default implementation in this class returns the input map. * <p>This default implementation in this class returns the input map.
* * @param headers the headers to send or {@code null}
* @param headers the headers to send or {@code null}. * @return the actual headers to send or {@code null}
* @return the actual headers to send or {@code null}.
*/ */
protected Map<String, Object> processHeadersToSend(Map<String, Object> headers) { protected Map<String, Object> processHeadersToSend(Map<String, Object> headers) {
return headers; return headers;

47
spring-messaging/src/main/java/org/springframework/messaging/core/AbstractMessagingTemplate.java

@ -37,12 +37,12 @@ public abstract class AbstractMessagingTemplate<D> extends AbstractMessageSendin
@Override @Override
public Message<?> receive() { public Message<?> receive() {
return this.receive(getRequiredDefaultDestination()); return receive(getRequiredDefaultDestination());
} }
@Override @Override
public Message<?> receive(D destination) { public Message<?> receive(D destination) {
return this.doReceive(destination); return doReceive(destination);
} }
protected abstract Message<?> doReceive(D destination); protected abstract Message<?> doReceive(D destination);
@ -50,13 +50,13 @@ public abstract class AbstractMessagingTemplate<D> extends AbstractMessageSendin
@Override @Override
public <T> T receiveAndConvert(Class<T> targetClass) { public <T> T receiveAndConvert(Class<T> targetClass) {
return this.receiveAndConvert(getRequiredDefaultDestination(), targetClass); return receiveAndConvert(getRequiredDefaultDestination(), targetClass);
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Override @Override
public <T> T receiveAndConvert(D destination, Class<T> targetClass) { public <T> T receiveAndConvert(D destination, Class<T> targetClass) {
Message<?> message = this.doReceive(destination); Message<?> message = doReceive(destination);
if (message != null) { if (message != null) {
return (T) getMessageConverter().fromMessage(message, targetClass); return (T) getMessageConverter().fromMessage(message, targetClass);
} }
@ -67,12 +67,12 @@ public abstract class AbstractMessagingTemplate<D> extends AbstractMessageSendin
@Override @Override
public Message<?> sendAndReceive(Message<?> requestMessage) { public Message<?> sendAndReceive(Message<?> requestMessage) {
return this.sendAndReceive(getRequiredDefaultDestination(), requestMessage); return sendAndReceive(getRequiredDefaultDestination(), requestMessage);
} }
@Override @Override
public Message<?> sendAndReceive(D destination, Message<?> requestMessage) { public Message<?> sendAndReceive(D destination, Message<?> requestMessage) {
return this.doSendAndReceive(destination, requestMessage); return doSendAndReceive(destination, requestMessage);
} }
protected abstract Message<?> doSendAndReceive(D destination, Message<?> requestMessage); protected abstract Message<?> doSendAndReceive(D destination, Message<?> requestMessage);
@ -80,34 +80,27 @@ public abstract class AbstractMessagingTemplate<D> extends AbstractMessageSendin
@Override @Override
public <T> T convertSendAndReceive(Object request, Class<T> targetClass) { public <T> T convertSendAndReceive(Object request, Class<T> targetClass) {
return this.convertSendAndReceive(getRequiredDefaultDestination(), request, targetClass); return convertSendAndReceive(getRequiredDefaultDestination(), request, targetClass);
} }
@Override @Override
public <T> T convertSendAndReceive(D destination, Object request, Class<T> targetClass) { public <T> T convertSendAndReceive(D destination, Object request, Class<T> targetClass) {
Map<String, Object> headers = null; return convertSendAndReceive(destination, request, null, targetClass);
return this.convertSendAndReceive(destination, request, headers, targetClass);
} }
@Override @Override
public <T> T convertSendAndReceive(D destination, Object request, Map<String, Object> headers, public <T> T convertSendAndReceive(D destination, Object request, Map<String, Object> headers, Class<T> targetClass) {
Class<T> targetClass) { return convertSendAndReceive(destination, request, headers, targetClass, null);
MessagePostProcessor postProcessor = null;
return this.convertSendAndReceive(destination, request, headers, targetClass, postProcessor);
} }
@Override @Override
public <T> T convertSendAndReceive(Object request, Class<T> targetClass, MessagePostProcessor postProcessor) { public <T> T convertSendAndReceive(Object request, Class<T> targetClass, MessagePostProcessor postProcessor) {
return this.convertSendAndReceive(getRequiredDefaultDestination(), request, targetClass, postProcessor); return convertSendAndReceive(getRequiredDefaultDestination(), request, targetClass, postProcessor);
} }
@Override @Override
public <T> T convertSendAndReceive(D destination, Object request, Class<T> targetClass, public <T> T convertSendAndReceive(D destination, Object request, Class<T> targetClass, MessagePostProcessor postProcessor) {
MessagePostProcessor postProcessor) { return convertSendAndReceive(destination, request, null, targetClass, postProcessor);
Map<String, Object> headers = null;
return this.convertSendAndReceive(destination, request, headers, targetClass, postProcessor);
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@ -115,22 +108,22 @@ public abstract class AbstractMessagingTemplate<D> extends AbstractMessageSendin
public <T> T convertSendAndReceive(D destination, Object request, Map<String, Object> headers, public <T> T convertSendAndReceive(D destination, Object request, Map<String, Object> headers,
Class<T> targetClass, MessagePostProcessor postProcessor) { Class<T> targetClass, MessagePostProcessor postProcessor) {
MessageHeaders messageHeaders = (headers != null) ? new MessageHeaders(headers) : null; MessageHeaders messageHeaders = (headers != null ? new MessageHeaders(headers) : null);
Message<?> requestMessage = getMessageConverter().toMessage(request, messageHeaders); Message<?> requestMessage = getMessageConverter().toMessage(request, messageHeaders);
if (requestMessage == null) { if (requestMessage == null) {
String payloadType = (request != null) ? request.getClass().getName() : null; String payloadType = (request != null ? request.getClass().getName() : null);
throw new MessageConversionException("Unable to convert payload type '" Object contentType = (messageHeaders != null ? messageHeaders.get(MessageHeaders.CONTENT_TYPE) : null);
+ payloadType + "', Content-Type=" + messageHeaders.get(MessageHeaders.CONTENT_TYPE) throw new MessageConversionException("Unable to convert payload with type '" + payloadType +
+ ", converter=" + getMessageConverter(), null); "', contentType='" + contentType + "', converter=[" + getMessageConverter() + "]");
} }
if (postProcessor != null) { if (postProcessor != null) {
requestMessage = postProcessor.postProcessMessage(requestMessage); requestMessage = postProcessor.postProcessMessage(requestMessage);
} }
Message<?> replyMessage = this.sendAndReceive(destination, requestMessage); Message<?> replyMessage = sendAndReceive(destination, requestMessage);
return (replyMessage != null) ? (T) getMessageConverter().fromMessage(replyMessage, targetClass) : null; return (replyMessage != null ? (T) getMessageConverter().fromMessage(replyMessage, targetClass) : null);
} }
} }

38
spring-messaging/src/main/java/org/springframework/messaging/core/GenericMessagingTemplate.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2013 the original author or authors. * Copyright 2002-2014 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -54,7 +54,6 @@ public class GenericMessagingTemplate extends AbstractDestinationResolvingMessag
/** /**
* Configure the timeout value to use for send operations. * Configure the timeout value to use for send operations.
*
* @param sendTimeout the send timeout in milliseconds * @param sendTimeout the send timeout in milliseconds
*/ */
public void setSendTimeout(long sendTimeout) { public void setSendTimeout(long sendTimeout) {
@ -70,7 +69,6 @@ public class GenericMessagingTemplate extends AbstractDestinationResolvingMessag
/** /**
* Configure the timeout value to use for receive operations. * Configure the timeout value to use for receive operations.
*
* @param receiveTimeout the receive timeout in milliseconds * @param receiveTimeout the receive timeout in milliseconds
*/ */
public void setReceiveTimeout(long receiveTimeout) { public void setReceiveTimeout(long receiveTimeout) {
@ -89,11 +87,9 @@ public class GenericMessagingTemplate extends AbstractDestinationResolvingMessag
* receiving thread isn't going to receive the reply either because it timed out, * receiving thread isn't going to receive the reply either because it timed out,
* or because it already received a reply, or because it got an exception while * or because it already received a reply, or because it got an exception while
* sending the request message. * sending the request message.
* <p> * <p>The default value is {@code false} in which case only a WARN message is logged.
* The default value is {@code false} in which case only a WARN message is logged.
* If set to {@code true} a {@link MessageDeliveryException} is raised in addition * If set to {@code true} a {@link MessageDeliveryException} is raised in addition
* to the log message. * to the log message.
*
* @param throwExceptionOnLateReply whether to throw an exception or not * @param throwExceptionOnLateReply whether to throw an exception or not
*/ */
public void setThrowExceptionOnLateReply(boolean throwExceptionOnLateReply) { public void setThrowExceptionOnLateReply(boolean throwExceptionOnLateReply) {
@ -108,8 +104,7 @@ public class GenericMessagingTemplate extends AbstractDestinationResolvingMessag
@Override @Override
protected final void doSend(MessageChannel channel, Message<?> message) { protected final void doSend(MessageChannel channel, Message<?> message) {
Assert.notNull(channel, "'channel' is required");
Assert.notNull(channel, "channel must not be null");
MessageHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, MessageHeaderAccessor.class); MessageHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, MessageHeaderAccessor.class);
if (accessor != null && accessor.isMutable()) { if (accessor != null && accessor.isMutable()) {
@ -117,7 +112,7 @@ public class GenericMessagingTemplate extends AbstractDestinationResolvingMessag
} }
long timeout = this.sendTimeout; long timeout = this.sendTimeout;
boolean sent = (timeout >= 0) ? channel.send(message, timeout) : channel.send(message); boolean sent = (timeout >= 0 ? channel.send(message, timeout) : channel.send(message));
if (!sent) { if (!sent) {
throw new MessageDeliveryException(message, throw new MessageDeliveryException(message,
@ -127,15 +122,14 @@ public class GenericMessagingTemplate extends AbstractDestinationResolvingMessag
@Override @Override
protected final Message<?> doReceive(MessageChannel channel) { protected final Message<?> doReceive(MessageChannel channel) {
Assert.notNull(channel, "'channel' is required"); Assert.notNull(channel, "'channel' is required");
Assert.state(channel instanceof PollableChannel, "A PollableChannel is required to receive messages."); Assert.state(channel instanceof PollableChannel, "A PollableChannel is required to receive messages");
long timeout = this.receiveTimeout; long timeout = this.receiveTimeout;
Message<?> message = (timeout >= 0) ? Message<?> message = (timeout >= 0 ?
((PollableChannel) channel).receive(timeout) : ((PollableChannel) channel).receive(); ((PollableChannel) channel).receive(timeout) : ((PollableChannel) channel).receive());
if ((message == null) && this.logger.isTraceEnabled()) { if (message == null && this.logger.isTraceEnabled()) {
this.logger.trace("Failed to receive message from channel '" + channel + "' within timeout: " + timeout); this.logger.trace("Failed to receive message from channel '" + channel + "' within timeout: " + timeout);
} }
@ -144,20 +138,16 @@ public class GenericMessagingTemplate extends AbstractDestinationResolvingMessag
@Override @Override
protected final Message<?> doSendAndReceive(MessageChannel channel, Message<?> requestMessage) { protected final Message<?> doSendAndReceive(MessageChannel channel, Message<?> requestMessage) {
Assert.notNull(channel, "'channel' is required"); Assert.notNull(channel, "'channel' is required");
Object originalReplyChannelHeader = requestMessage.getHeaders().getReplyChannel(); Object originalReplyChannelHeader = requestMessage.getHeaders().getReplyChannel();
Object originalErrorChannelHeader = requestMessage.getHeaders().getErrorChannel(); Object originalErrorChannelHeader = requestMessage.getHeaders().getErrorChannel();
TemporaryReplyChannel tempReplyChannel = new TemporaryReplyChannel(); TemporaryReplyChannel tempReplyChannel = new TemporaryReplyChannel();
requestMessage = MessageBuilder.fromMessage(requestMessage).setReplyChannel(tempReplyChannel).
requestMessage = MessageBuilder.fromMessage(requestMessage) setErrorChannel(tempReplyChannel).build();
.setReplyChannel(tempReplyChannel)
.setErrorChannel(tempReplyChannel).build();
try { try {
this.doSend(channel, requestMessage); doSend(channel, requestMessage);
} }
catch (RuntimeException e) { catch (RuntimeException e) {
tempReplyChannel.setSendFailed(true); tempReplyChannel.setSendFailed(true);
@ -183,10 +173,10 @@ public class GenericMessagingTemplate extends AbstractDestinationResolvingMessag
private final Log logger = LogFactory.getLog(TemporaryReplyChannel.class); private final Log logger = LogFactory.getLog(TemporaryReplyChannel.class);
private volatile Message<?> replyMessage;
private final CountDownLatch replyLatch = new CountDownLatch(1); private final CountDownLatch replyLatch = new CountDownLatch(1);
private volatile Message<?> replyMessage;
private volatile boolean hasReceived; private volatile boolean hasReceived;
private volatile boolean hasTimedOut; private volatile boolean hasTimedOut;
@ -198,7 +188,6 @@ public class GenericMessagingTemplate extends AbstractDestinationResolvingMessag
this.hasSendFailed = hasSendError; this.hasSendFailed = hasSendError;
} }
@Override @Override
public Message<?> receive() { public Message<?> receive() {
return this.receive(-1); return this.receive(-1);
@ -233,7 +222,6 @@ public class GenericMessagingTemplate extends AbstractDestinationResolvingMessag
@Override @Override
public boolean send(Message<?> message, long timeout) { public boolean send(Message<?> message, long timeout) {
this.replyMessage = message; this.replyMessage = message;
boolean alreadyReceivedReply = this.hasReceived; boolean alreadyReceivedReply = this.hasReceived;
this.replyLatch.countDown(); this.replyLatch.countDown();

28
spring-messaging/src/main/java/org/springframework/messaging/simp/SimpMessagingTemplate.java

@ -84,22 +84,21 @@ public class SimpMessagingTemplate extends AbstractMessageSendingTemplate<String
/** /**
* Configure a {@link MessageHeaderInitializer} to apply to the headers of all * Configure a {@link MessageHeaderInitializer} to apply to the headers of all
* messages created through the {@code SimpMessagingTemplate}. * messages created through the {@code SimpMessagingTemplate}.
* * <p>By default, this property is not set.
* <p>By default this property is not set.
*/ */
public void setHeaderInitializer(MessageHeaderInitializer headerInitializer) { public void setHeaderInitializer(MessageHeaderInitializer headerInitializer) {
this.headerInitializer = headerInitializer; this.headerInitializer = headerInitializer;
} }
/** /**
* @return the configured header initializer. * Return the configured header initializer.
*/ */
public MessageHeaderInitializer getHeaderInitializer() { public MessageHeaderInitializer getHeaderInitializer() {
return this.headerInitializer; return this.headerInitializer;
} }
/** /**
* @return the messageChannel * Return the configured message channel.
*/ */
public MessageChannel getMessageChannel() { public MessageChannel getMessageChannel() {
return this.messageChannel; return this.messageChannel;
@ -107,7 +106,6 @@ public class SimpMessagingTemplate extends AbstractMessageSendingTemplate<String
/** /**
* Specify the timeout value to use for send operations. * Specify the timeout value to use for send operations.
*
* @param sendTimeout the send timeout in milliseconds * @param sendTimeout the send timeout in milliseconds
*/ */
public void setSendTimeout(long sendTimeout) { public void setSendTimeout(long sendTimeout) {
@ -115,7 +113,7 @@ public class SimpMessagingTemplate extends AbstractMessageSendingTemplate<String
} }
/** /**
* @return the sendTimeout * Return the configured send timeout.
*/ */
public long getSendTimeout() { public long getSendTimeout() {
return this.sendTimeout; return this.sendTimeout;
@ -127,13 +125,11 @@ public class SimpMessagingTemplate extends AbstractMessageSendingTemplate<String
* {@link org.springframework.messaging.simp.SimpMessageHeaderAccessor#DESTINATION_HEADER * {@link org.springframework.messaging.simp.SimpMessageHeaderAccessor#DESTINATION_HEADER
* SimpMessageHeaderAccessor#DESTINATION_HEADER} then the message is sent without * SimpMessageHeaderAccessor#DESTINATION_HEADER} then the message is sent without
* further changes. * further changes.
*
* <p>If a destination header is not already present ,the message is sent * <p>If a destination header is not already present ,the message is sent
* to the configured {@link #setDefaultDestination(Object) defaultDestination} * to the configured {@link #setDefaultDestination(Object) defaultDestination}
* or an exception an {@code IllegalStateException} is raised if that isn't * or an exception an {@code IllegalStateException} is raised if that isn't
* configured. * configured.
* * @param message the message to send (never {@code null})
* @param message the message to send, never {@code null}
*/ */
@Override @Override
public void send(Message<?> message) { public void send(Message<?> message) {
@ -149,7 +145,6 @@ public class SimpMessagingTemplate extends AbstractMessageSendingTemplate<String
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Override @Override
protected void doSend(String destination, Message<?> message) { protected void doSend(String destination, Message<?> message) {
Assert.notNull(destination, "Destination must not be null"); Assert.notNull(destination, "Destination must not be null");
SimpMessageHeaderAccessor simpAccessor = SimpMessageHeaderAccessor simpAccessor =
@ -181,14 +176,11 @@ public class SimpMessagingTemplate extends AbstractMessageSendingTemplate<String
} }
private void sendInternal(Message<?> message) { private void sendInternal(Message<?> message) {
String destination = SimpMessageHeaderAccessor.getDestination(message.getHeaders()); String destination = SimpMessageHeaderAccessor.getDestination(message.getHeaders());
Assert.notNull(destination); Assert.notNull(destination);
long timeout = this.sendTimeout; long timeout = this.sendTimeout;
boolean sent = (timeout >= 0) boolean sent = (timeout >= 0 ? this.messageChannel.send(message, timeout) : this.messageChannel.send(message));
? this.messageChannel.send(message, timeout)
: this.messageChannel.send(message);
if (!sent) { if (!sent) {
throw new MessageDeliveryException(message, throw new MessageDeliveryException(message,
@ -204,21 +196,21 @@ public class SimpMessagingTemplate extends AbstractMessageSendingTemplate<String
@Override @Override
public void convertAndSendToUser(String user, String destination, Object payload) throws MessagingException { public void convertAndSendToUser(String user, String destination, Object payload) throws MessagingException {
this.convertAndSendToUser(user, destination, payload, (MessagePostProcessor) null); convertAndSendToUser(user, destination, payload, (MessagePostProcessor) null);
} }
@Override @Override
public void convertAndSendToUser(String user, String destination, Object payload, public void convertAndSendToUser(String user, String destination, Object payload,
Map<String, Object> headers) throws MessagingException { Map<String, Object> headers) throws MessagingException {
this.convertAndSendToUser(user, destination, payload, headers, null); convertAndSendToUser(user, destination, payload, headers, null);
} }
@Override @Override
public void convertAndSendToUser(String user, String destination, Object payload, public void convertAndSendToUser(String user, String destination, Object payload,
MessagePostProcessor postProcessor) throws MessagingException { MessagePostProcessor postProcessor) throws MessagingException {
this.convertAndSendToUser(user, destination, payload, null, postProcessor); convertAndSendToUser(user, destination, payload, null, postProcessor);
} }
@Override @Override
@ -235,11 +227,9 @@ public class SimpMessagingTemplate extends AbstractMessageSendingTemplate<String
* {@link org.springframework.messaging.support.NativeMessageHeaderAccessor#NATIVE_HEADERS NATIVE_HEADERS NATIVE_HEADERS NATIVE_HEADERS}. * {@link org.springframework.messaging.support.NativeMessageHeaderAccessor#NATIVE_HEADERS NATIVE_HEADERS NATIVE_HEADERS NATIVE_HEADERS}.
* effectively treats the input header map as headers to be sent out to the * effectively treats the input header map as headers to be sent out to the
* destination. * destination.
*
* <p>However if the given headers already contain the key * <p>However if the given headers already contain the key
* {@code NATIVE_HEADERS NATIVE_HEADERS} then the same headers instance is * {@code NATIVE_HEADERS NATIVE_HEADERS} then the same headers instance is
* returned without changes. * returned without changes.
*
* <p>Also if the given headers were prepared and obtained with * <p>Also if the given headers were prepared and obtained with
* {@link SimpMessageHeaderAccessor#getMessageHeaders()} then the same headers * {@link SimpMessageHeaderAccessor#getMessageHeaders()} then the same headers
* instance is also returned without changes. * instance is also returned without changes.

Loading…
Cancel
Save