Review bd093eb to provide a generic type on `JmsResponse`

Issue: SPR-13133
This commit is contained in:
Stephane Nicoll 2015-06-16 15:33:50 +02:00
parent 75c88ffbeb
commit c8fcdadbae
4 changed files with 31 additions and 20 deletions

View File

@ -267,7 +267,7 @@ public abstract class AbstractAdaptableMessageListener
*/ */
protected Message buildMessage(Session session, Object result) throws JMSException { protected Message buildMessage(Session session, Object result) throws JMSException {
Object content = (result instanceof JmsResponse Object content = (result instanceof JmsResponse
? ((JmsResponse) result).getResponse() : result); ? ((JmsResponse<?>) result).getResponse() : result);
MessageConverter converter = getMessageConverter(); MessageConverter converter = getMessageConverter();
if (converter != null) { if (converter != null) {
@ -308,7 +308,7 @@ public abstract class AbstractAdaptableMessageListener
private Destination getResponseDestination(Message request, Message response, Session session, Object result) private Destination getResponseDestination(Message request, Message response, Session session, Object result)
throws JMSException { throws JMSException {
if (result instanceof JmsResponse) { if (result instanceof JmsResponse) {
JmsResponse jmsResponse = (JmsResponse) result; JmsResponse<?> jmsResponse = (JmsResponse) result;
Destination destination = jmsResponse.resolveDestination(getDestinationResolver(), session); Destination destination = jmsResponse.resolveDestination(getDestinationResolver(), session);
if (destination != null) { if (destination != null) {
return destination; return destination;

View File

@ -50,10 +50,11 @@ import org.springframework.util.Assert;
* @since 4.2 * @since 4.2
* @see org.springframework.jms.annotation.JmsListener * @see org.springframework.jms.annotation.JmsListener
* @see org.springframework.messaging.handler.annotation.SendTo * @see org.springframework.messaging.handler.annotation.SendTo
* @param <T> the type of the response
*/ */
public class JmsResponse { public class JmsResponse<T> {
private final Object response; private final T response;
private final Object destination; private final Object destination;
@ -62,7 +63,7 @@ public class JmsResponse {
* @param response the content of the result * @param response the content of the result
* @param destination the destination * @param destination the destination
*/ */
protected JmsResponse(Object response, Object destination) { protected JmsResponse(T response, Object destination) {
Assert.notNull(response, "Result must not be null"); Assert.notNull(response, "Result must not be null");
this.response = response; this.response = response;
this.destination = destination; this.destination = destination;
@ -71,32 +72,42 @@ public class JmsResponse {
/** /**
* Create a {@link JmsResponse} targeting the queue with the specified name. * Create a {@link JmsResponse} targeting the queue with the specified name.
*/ */
public static JmsResponse forQueue(Object result, String queueName) { public static <T> JmsResponse<T> forQueue(T result, String queueName) {
Assert.notNull(queueName, "Queue name must not be null"); Assert.notNull(queueName, "Queue name must not be null");
return new JmsResponse(result, new DestinationNameHolder(queueName, false)); return new JmsResponse<T>(result, new DestinationNameHolder(queueName, false));
} }
/** /**
* Create a {@link JmsResponse} targeting the topic with the specified name. * Create a {@link JmsResponse} targeting the topic with the specified name.
*/ */
public static JmsResponse forTopic(Object result, String topicName) { public static <T> JmsResponse<T> forTopic(T result, String topicName) {
Assert.notNull(topicName, "Topic name must not be null"); Assert.notNull(topicName, "Topic name must not be null");
return new JmsResponse(result, new DestinationNameHolder(topicName, true)); return new JmsResponse<T>(result, new DestinationNameHolder(topicName, true));
} }
/** /**
* Create a {@link JmsResponse} targeting the specified {@link Destination}. * Create a {@link JmsResponse} targeting the specified {@link Destination}.
*/ */
public static JmsResponse forDestination(Object result, Destination destination) { public static <T> JmsResponse<T> forDestination(T result, Destination destination) {
Assert.notNull(destination, "Destination must not be null"); Assert.notNull(destination, "Destination must not be null");
return new JmsResponse(result, destination); return new JmsResponse<T>(result, destination);
} }
/**
public Object getResponse() { * Return the content of the response.
return response; */
public T getResponse() {
return this.response;
} }
/**
* Resolve the {@link Destination} to use for this instance. The {@link DestinationResolver}
* and {@link Session} can be used to resolve a destination at runtime.
* @param destinationResolver the destination resolver to use if necessary
* @param session the session to use, if necessary
* @return the {@link Destination} to use
* @throws JMSException if the DestinationResolver failed to resolve the destination
*/
public Destination resolveDestination(DestinationResolver destinationResolver, Session session) public Destination resolveDestination(DestinationResolver destinationResolver, Session session)
throws JMSException { throws JMSException {

View File

@ -52,7 +52,7 @@ public class JmsResponseTests {
Destination destination = mock(Destination.class); Destination destination = mock(Destination.class);
given(destinationResolver.resolveDestinationName(session, "myQueue", false)).willReturn(destination); given(destinationResolver.resolveDestinationName(session, "myQueue", false)).willReturn(destination);
JmsResponse jmsResponse = JmsResponse.forQueue("foo", "myQueue"); JmsResponse<String> jmsResponse = JmsResponse.forQueue("foo", "myQueue");
Destination actual = jmsResponse.resolveDestination(destinationResolver, session); Destination actual = jmsResponse.resolveDestination(destinationResolver, session);
assertSame(destination, actual); assertSame(destination, actual);
} }

View File

@ -285,21 +285,21 @@ public class MessagingMessageListenerAdapterTests {
.build(); .build();
} }
public JmsResponse replyPayloadToQueue(Message<String> input) { public JmsResponse<String> replyPayloadToQueue(Message<String> input) {
return JmsResponse.forQueue(input.getPayload(), "queueOut"); return JmsResponse.forQueue(input.getPayload(), "queueOut");
} }
public JmsResponse replyPayloadToTopic(Message<String> input) { public JmsResponse<String> replyPayloadToTopic(Message<String> input) {
return JmsResponse.forTopic(input.getPayload(), "topicOut"); return JmsResponse.forTopic(input.getPayload(), "topicOut");
} }
public JmsResponse replyPayloadToDestination(Message<String> input) { public JmsResponse<String> replyPayloadToDestination(Message<String> input) {
return JmsResponse.forDestination(input.getPayload(), return JmsResponse.forDestination(input.getPayload(),
input.getHeaders().get("destination", Destination.class)); input.getHeaders().get("destination", Destination.class));
} }
public JmsResponse replyPayloadNoDestination(Message<String> input) { public JmsResponse<String> replyPayloadNoDestination(Message<String> input) {
return new JmsResponse(input.getPayload(), null); return new JmsResponse<>(input.getPayload(), null);
} }
public void fail(String input) { public void fail(String input) {