Skip to content

Commit c8fcdad

Browse files
committed
Polish
Review bd093eb to provide a generic type on `JmsResponse` Issue: SPR-13133
1 parent 75c88ff commit c8fcdad

File tree

4 files changed

+31
-20
lines changed

4 files changed

+31
-20
lines changed

spring-jms/src/main/java/org/springframework/jms/listener/adapter/AbstractAdaptableMessageListener.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ protected void handleResult(Object result, Message request, Session session) {
267267
*/
268268
protected Message buildMessage(Session session, Object result) throws JMSException {
269269
Object content = (result instanceof JmsResponse
270-
? ((JmsResponse) result).getResponse() : result);
270+
? ((JmsResponse<?>) result).getResponse() : result);
271271

272272
MessageConverter converter = getMessageConverter();
273273
if (converter != null) {
@@ -308,7 +308,7 @@ protected void postProcessResponse(Message request, Message response) throws JMS
308308
private Destination getResponseDestination(Message request, Message response, Session session, Object result)
309309
throws JMSException {
310310
if (result instanceof JmsResponse) {
311-
JmsResponse jmsResponse = (JmsResponse) result;
311+
JmsResponse<?> jmsResponse = (JmsResponse) result;
312312
Destination destination = jmsResponse.resolveDestination(getDestinationResolver(), session);
313313
if (destination != null) {
314314
return destination;

spring-jms/src/main/java/org/springframework/jms/listener/adapter/JmsResponse.java

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,11 @@
5050
* @since 4.2
5151
* @see org.springframework.jms.annotation.JmsListener
5252
* @see org.springframework.messaging.handler.annotation.SendTo
53+
* @param <T> the type of the response
5354
*/
54-
public class JmsResponse {
55+
public class JmsResponse<T> {
5556

56-
private final Object response;
57+
private final T response;
5758

5859
private final Object destination;
5960

@@ -62,7 +63,7 @@ public class JmsResponse {
6263
* @param response the content of the result
6364
* @param destination the destination
6465
*/
65-
protected JmsResponse(Object response, Object destination) {
66+
protected JmsResponse(T response, Object destination) {
6667
Assert.notNull(response, "Result must not be null");
6768
this.response = response;
6869
this.destination = destination;
@@ -71,32 +72,42 @@ protected JmsResponse(Object response, Object destination) {
7172
/**
7273
* Create a {@link JmsResponse} targeting the queue with the specified name.
7374
*/
74-
public static JmsResponse forQueue(Object result, String queueName) {
75+
public static <T> JmsResponse<T> forQueue(T result, String queueName) {
7576
Assert.notNull(queueName, "Queue name must not be null");
76-
return new JmsResponse(result, new DestinationNameHolder(queueName, false));
77+
return new JmsResponse<T>(result, new DestinationNameHolder(queueName, false));
7778
}
7879

7980
/**
8081
* Create a {@link JmsResponse} targeting the topic with the specified name.
8182
*/
82-
public static JmsResponse forTopic(Object result, String topicName) {
83+
public static <T> JmsResponse<T> forTopic(T result, String topicName) {
8384
Assert.notNull(topicName, "Topic name must not be null");
84-
return new JmsResponse(result, new DestinationNameHolder(topicName, true));
85+
return new JmsResponse<T>(result, new DestinationNameHolder(topicName, true));
8586
}
8687

8788
/**
8889
* Create a {@link JmsResponse} targeting the specified {@link Destination}.
8990
*/
90-
public static JmsResponse forDestination(Object result, Destination destination) {
91+
public static <T> JmsResponse<T> forDestination(T result, Destination destination) {
9192
Assert.notNull(destination, "Destination must not be null");
92-
return new JmsResponse(result, destination);
93+
return new JmsResponse<T>(result, destination);
9394
}
9495

95-
96-
public Object getResponse() {
97-
return response;
96+
/**
97+
* Return the content of the response.
98+
*/
99+
public T getResponse() {
100+
return this.response;
98101
}
99102

103+
/**
104+
* Resolve the {@link Destination} to use for this instance. The {@link DestinationResolver}
105+
* and {@link Session} can be used to resolve a destination at runtime.
106+
* @param destinationResolver the destination resolver to use if necessary
107+
* @param session the session to use, if necessary
108+
* @return the {@link Destination} to use
109+
* @throws JMSException if the DestinationResolver failed to resolve the destination
110+
*/
100111
public Destination resolveDestination(DestinationResolver destinationResolver, Session session)
101112
throws JMSException {
102113

spring-jms/src/test/java/org/springframework/jms/listener/adapter/JmsResponseTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public void resolveDestinationForQueue() throws JMSException {
5252
Destination destination = mock(Destination.class);
5353

5454
given(destinationResolver.resolveDestinationName(session, "myQueue", false)).willReturn(destination);
55-
JmsResponse jmsResponse = JmsResponse.forQueue("foo", "myQueue");
55+
JmsResponse<String> jmsResponse = JmsResponse.forQueue("foo", "myQueue");
5656
Destination actual = jmsResponse.resolveDestination(destinationResolver, session);
5757
assertSame(destination, actual);
5858
}

spring-jms/src/test/java/org/springframework/jms/listener/adapter/MessagingMessageListenerAdapterTests.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -285,21 +285,21 @@ public Message<String> echo(Message<String> input) {
285285
.build();
286286
}
287287

288-
public JmsResponse replyPayloadToQueue(Message<String> input) {
288+
public JmsResponse<String> replyPayloadToQueue(Message<String> input) {
289289
return JmsResponse.forQueue(input.getPayload(), "queueOut");
290290
}
291291

292-
public JmsResponse replyPayloadToTopic(Message<String> input) {
292+
public JmsResponse<String> replyPayloadToTopic(Message<String> input) {
293293
return JmsResponse.forTopic(input.getPayload(), "topicOut");
294294
}
295295

296-
public JmsResponse replyPayloadToDestination(Message<String> input) {
296+
public JmsResponse<String> replyPayloadToDestination(Message<String> input) {
297297
return JmsResponse.forDestination(input.getPayload(),
298298
input.getHeaders().get("destination", Destination.class));
299299
}
300300

301-
public JmsResponse replyPayloadNoDestination(Message<String> input) {
302-
return new JmsResponse(input.getPayload(), null);
301+
public JmsResponse<String> replyPayloadNoDestination(Message<String> input) {
302+
return new JmsResponse<>(input.getPayload(), null);
303303
}
304304

305305
public void fail(String input) {

0 commit comments

Comments
 (0)