From c8fcdadbae37ca9daa7e44d51116d084272a0781 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Tue, 16 Jun 2015 15:33:50 +0200 Subject: [PATCH] Polish Review bd093eb to provide a generic type on `JmsResponse` Issue: SPR-13133 --- .../AbstractAdaptableMessageListener.java | 4 +-- .../jms/listener/adapter/JmsResponse.java | 35 ++++++++++++------- .../listener/adapter/JmsResponseTests.java | 2 +- .../MessagingMessageListenerAdapterTests.java | 10 +++--- 4 files changed, 31 insertions(+), 20 deletions(-) diff --git a/spring-jms/src/main/java/org/springframework/jms/listener/adapter/AbstractAdaptableMessageListener.java b/spring-jms/src/main/java/org/springframework/jms/listener/adapter/AbstractAdaptableMessageListener.java index 883101848b..9b53d5647b 100644 --- a/spring-jms/src/main/java/org/springframework/jms/listener/adapter/AbstractAdaptableMessageListener.java +++ b/spring-jms/src/main/java/org/springframework/jms/listener/adapter/AbstractAdaptableMessageListener.java @@ -267,7 +267,7 @@ public abstract class AbstractAdaptableMessageListener */ protected Message buildMessage(Session session, Object result) throws JMSException { Object content = (result instanceof JmsResponse - ? ((JmsResponse) result).getResponse() : result); + ? ((JmsResponse) result).getResponse() : result); MessageConverter converter = getMessageConverter(); if (converter != null) { @@ -308,7 +308,7 @@ public abstract class AbstractAdaptableMessageListener private Destination getResponseDestination(Message request, Message response, Session session, Object result) throws JMSException { if (result instanceof JmsResponse) { - JmsResponse jmsResponse = (JmsResponse) result; + JmsResponse jmsResponse = (JmsResponse) result; Destination destination = jmsResponse.resolveDestination(getDestinationResolver(), session); if (destination != null) { return destination; diff --git a/spring-jms/src/main/java/org/springframework/jms/listener/adapter/JmsResponse.java b/spring-jms/src/main/java/org/springframework/jms/listener/adapter/JmsResponse.java index e6a91858f2..15a48de37c 100644 --- a/spring-jms/src/main/java/org/springframework/jms/listener/adapter/JmsResponse.java +++ b/spring-jms/src/main/java/org/springframework/jms/listener/adapter/JmsResponse.java @@ -50,10 +50,11 @@ import org.springframework.util.Assert; * @since 4.2 * @see org.springframework.jms.annotation.JmsListener * @see org.springframework.messaging.handler.annotation.SendTo + * @param the type of the response */ -public class JmsResponse { +public class JmsResponse { - private final Object response; + private final T response; private final Object destination; @@ -62,7 +63,7 @@ public class JmsResponse { * @param response the content of the result * @param destination the destination */ - protected JmsResponse(Object response, Object destination) { + protected JmsResponse(T response, Object destination) { Assert.notNull(response, "Result must not be null"); this.response = response; this.destination = destination; @@ -71,32 +72,42 @@ public class JmsResponse { /** * Create a {@link JmsResponse} targeting the queue with the specified name. */ - public static JmsResponse forQueue(Object result, String queueName) { + public static JmsResponse forQueue(T result, String queueName) { Assert.notNull(queueName, "Queue name must not be null"); - return new JmsResponse(result, new DestinationNameHolder(queueName, false)); + return new JmsResponse(result, new DestinationNameHolder(queueName, false)); } /** * Create a {@link JmsResponse} targeting the topic with the specified name. */ - public static JmsResponse forTopic(Object result, String topicName) { + public static JmsResponse forTopic(T result, String topicName) { Assert.notNull(topicName, "Topic name must not be null"); - return new JmsResponse(result, new DestinationNameHolder(topicName, true)); + return new JmsResponse(result, new DestinationNameHolder(topicName, true)); } /** * Create a {@link JmsResponse} targeting the specified {@link Destination}. */ - public static JmsResponse forDestination(Object result, Destination destination) { + public static JmsResponse forDestination(T result, Destination destination) { Assert.notNull(destination, "Destination must not be null"); - return new JmsResponse(result, destination); + return new JmsResponse(result, destination); } - - public Object getResponse() { - return response; + /** + * Return the content of the 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) throws JMSException { diff --git a/spring-jms/src/test/java/org/springframework/jms/listener/adapter/JmsResponseTests.java b/spring-jms/src/test/java/org/springframework/jms/listener/adapter/JmsResponseTests.java index bc7d15f653..216b85d7eb 100644 --- a/spring-jms/src/test/java/org/springframework/jms/listener/adapter/JmsResponseTests.java +++ b/spring-jms/src/test/java/org/springframework/jms/listener/adapter/JmsResponseTests.java @@ -52,7 +52,7 @@ public class JmsResponseTests { Destination destination = mock(Destination.class); given(destinationResolver.resolveDestinationName(session, "myQueue", false)).willReturn(destination); - JmsResponse jmsResponse = JmsResponse.forQueue("foo", "myQueue"); + JmsResponse jmsResponse = JmsResponse.forQueue("foo", "myQueue"); Destination actual = jmsResponse.resolveDestination(destinationResolver, session); assertSame(destination, actual); } diff --git a/spring-jms/src/test/java/org/springframework/jms/listener/adapter/MessagingMessageListenerAdapterTests.java b/spring-jms/src/test/java/org/springframework/jms/listener/adapter/MessagingMessageListenerAdapterTests.java index 80377824a7..d5620748c3 100644 --- a/spring-jms/src/test/java/org/springframework/jms/listener/adapter/MessagingMessageListenerAdapterTests.java +++ b/spring-jms/src/test/java/org/springframework/jms/listener/adapter/MessagingMessageListenerAdapterTests.java @@ -285,21 +285,21 @@ public class MessagingMessageListenerAdapterTests { .build(); } - public JmsResponse replyPayloadToQueue(Message input) { + public JmsResponse replyPayloadToQueue(Message input) { return JmsResponse.forQueue(input.getPayload(), "queueOut"); } - public JmsResponse replyPayloadToTopic(Message input) { + public JmsResponse replyPayloadToTopic(Message input) { return JmsResponse.forTopic(input.getPayload(), "topicOut"); } - public JmsResponse replyPayloadToDestination(Message input) { + public JmsResponse replyPayloadToDestination(Message input) { return JmsResponse.forDestination(input.getPayload(), input.getHeaders().get("destination", Destination.class)); } - public JmsResponse replyPayloadNoDestination(Message input) { - return new JmsResponse(input.getPayload(), null); + public JmsResponse replyPayloadNoDestination(Message input) { + return new JmsResponse<>(input.getPayload(), null); } public void fail(String input) {