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 {
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;

View File

@ -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 <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;
@ -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 <T> JmsResponse<T> 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<T>(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 <T> JmsResponse<T> 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<T>(result, new DestinationNameHolder(topicName, true));
}
/**
* 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");
return new JmsResponse(result, destination);
return new JmsResponse<T>(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 {

View File

@ -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<String> jmsResponse = JmsResponse.forQueue("foo", "myQueue");
Destination actual = jmsResponse.resolveDestination(destinationResolver, session);
assertSame(destination, actual);
}

View File

@ -285,21 +285,21 @@ public class MessagingMessageListenerAdapterTests {
.build();
}
public JmsResponse replyPayloadToQueue(Message<String> input) {
public JmsResponse<String> replyPayloadToQueue(Message<String> input) {
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");
}
public JmsResponse replyPayloadToDestination(Message<String> input) {
public JmsResponse<String> replyPayloadToDestination(Message<String> input) {
return JmsResponse.forDestination(input.getPayload(),
input.getHeaders().get("destination", Destination.class));
}
public JmsResponse replyPayloadNoDestination(Message<String> input) {
return new JmsResponse(input.getPayload(), null);
public JmsResponse<String> replyPayloadNoDestination(Message<String> input) {
return new JmsResponse<>(input.getPayload(), null);
}
public void fail(String input) {