|
| 1 | +/* |
| 2 | + * Copyright 2002-2015 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.jms.listener.adapter; |
| 18 | + |
| 19 | +import javax.jms.Destination; |
| 20 | +import javax.jms.JMSException; |
| 21 | +import javax.jms.Session; |
| 22 | + |
| 23 | +import org.springframework.jms.support.destination.DestinationResolver; |
| 24 | +import org.springframework.util.Assert; |
| 25 | + |
| 26 | +/** |
| 27 | + * Return type of any JMS listener method used to indicate the actual response destination |
| 28 | + * alongside the response itself. Typically used when said destination needs to be |
| 29 | + * computed at runtime. |
| 30 | + * <p> |
| 31 | + * The example below sends a response with the content of the {@code result} argument to |
| 32 | + * the {@code queueOut Queue}: |
| 33 | + * |
| 34 | + * <pre class="code"> |
| 35 | + * package com.acme.foo; |
| 36 | + * |
| 37 | + * public class MyService { |
| 38 | + * @JmsListener |
| 39 | + * public JmsResponse process(String msg) { |
| 40 | + * // process incoming message |
| 41 | + * return JmsResponse.forQueue(result, "queueOut"); |
| 42 | + * } |
| 43 | + * }</pre> |
| 44 | + * |
| 45 | + * If the destination does not need to be computed at runtime, |
| 46 | + * {@link org.springframework.messaging.handler.annotation.SendTo @SendTo} is the |
| 47 | + * recommended declarative approach. |
| 48 | + * |
| 49 | + * @author Stephane Nicoll |
| 50 | + * @since 4.2 |
| 51 | + * @see org.springframework.jms.annotation.JmsListener |
| 52 | + * @see org.springframework.messaging.handler.annotation.SendTo |
| 53 | + */ |
| 54 | +public class JmsResponse { |
| 55 | + |
| 56 | + private final Object response; |
| 57 | + |
| 58 | + private final Object destination; |
| 59 | + |
| 60 | + /** |
| 61 | + * Create a new instance |
| 62 | + * @param response the content of the result |
| 63 | + * @param destination the destination |
| 64 | + */ |
| 65 | + protected JmsResponse(Object response, Object destination) { |
| 66 | + Assert.notNull(response, "Result must not be null"); |
| 67 | + this.response = response; |
| 68 | + this.destination = destination; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Create a {@link JmsResponse} targeting the queue with the specified name. |
| 73 | + */ |
| 74 | + public static JmsResponse forQueue(Object result, String queueName) { |
| 75 | + Assert.notNull(queueName, "Queue name must not be null"); |
| 76 | + return new JmsResponse(result, new DestinationNameHolder(queueName, false)); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Create a {@link JmsResponse} targeting the topic with the specified name. |
| 81 | + */ |
| 82 | + public static JmsResponse forTopic(Object result, String topicName) { |
| 83 | + Assert.notNull(topicName, "Topic name must not be null"); |
| 84 | + return new JmsResponse(result, new DestinationNameHolder(topicName, true)); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Create a {@link JmsResponse} targeting the specified {@link Destination}. |
| 89 | + */ |
| 90 | + public static JmsResponse forDestination(Object result, Destination destination) { |
| 91 | + Assert.notNull(destination, "Destination must not be null"); |
| 92 | + return new JmsResponse(result, destination); |
| 93 | + } |
| 94 | + |
| 95 | + |
| 96 | + public Object getResponse() { |
| 97 | + return response; |
| 98 | + } |
| 99 | + |
| 100 | + public Destination resolveDestination(DestinationResolver destinationResolver, Session session) |
| 101 | + throws JMSException { |
| 102 | + |
| 103 | + if (this.destination instanceof Destination) { |
| 104 | + return (Destination) this.destination; |
| 105 | + } |
| 106 | + if (this.destination instanceof DestinationNameHolder) { |
| 107 | + DestinationNameHolder nameHolder = (DestinationNameHolder) this.destination; |
| 108 | + return destinationResolver.resolveDestinationName(session, |
| 109 | + nameHolder.destinationName, nameHolder.pubSubDomain); |
| 110 | + } |
| 111 | + return null; |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public String toString() { |
| 116 | + return "JmsResponse{" + "response=" + this.response + ", destination=" + this.destination + '}'; |
| 117 | + } |
| 118 | + |
| 119 | + |
| 120 | + /** |
| 121 | + * Internal class combining a destination name |
| 122 | + * and its target destination type (queue or topic). |
| 123 | + */ |
| 124 | + protected static class DestinationNameHolder { |
| 125 | + private final String destinationName; |
| 126 | + |
| 127 | + private final boolean pubSubDomain; |
| 128 | + |
| 129 | + public DestinationNameHolder(String destinationName, boolean pubSubDomain) { |
| 130 | + this.destinationName = destinationName; |
| 131 | + this.pubSubDomain = pubSubDomain; |
| 132 | + } |
| 133 | + |
| 134 | + @Override |
| 135 | + public String toString() { |
| 136 | + return this.destinationName + "{" + "pubSubDomain=" + this.pubSubDomain + '}'; |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | +} |
0 commit comments