001/*
002 * Copyright 2002-2015 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.jms.listener.endpoint;
018
019import javax.jms.Session;
020
021import org.springframework.core.Constants;
022import org.springframework.jms.support.converter.MessageConverter;
023
024/**
025 * Common configuration object for activating a JMS message endpoint.
026 * Gets converted into a provider-specific JCA 1.5 ActivationSpec
027 * object for activating the endpoint.
028 *
029 * <p>Typically used in combination with {@link JmsMessageEndpointManager},
030 * but not tied to it.
031 *
032 * @author Juergen Hoeller
033 * @author Stephane Nicoll
034 * @since 2.5
035 * @see JmsActivationSpecFactory
036 * @see JmsMessageEndpointManager#setActivationSpecConfig
037 * @see javax.resource.spi.ResourceAdapter#endpointActivation
038 */
039public class JmsActivationSpecConfig {
040
041        /** Constants instance for javax.jms.Session */
042        private static final Constants sessionConstants = new Constants(Session.class);
043
044
045        private String destinationName;
046
047        private boolean pubSubDomain = false;
048
049        private Boolean replyPubSubDomain;
050
051        private boolean subscriptionDurable = false;
052
053        private boolean subscriptionShared = false;
054
055        private String subscriptionName;
056
057        private String clientId;
058
059        private String messageSelector;
060
061        private int acknowledgeMode = Session.AUTO_ACKNOWLEDGE;
062
063        private int maxConcurrency = -1;
064
065        private int prefetchSize = -1;
066
067        private MessageConverter messageConverter;
068
069
070        public void setDestinationName(String destinationName) {
071                this.destinationName = destinationName;
072        }
073
074        public String getDestinationName() {
075                return this.destinationName;
076        }
077
078        public void setPubSubDomain(boolean pubSubDomain) {
079                this.pubSubDomain = pubSubDomain;
080        }
081
082        public boolean isPubSubDomain() {
083                return this.pubSubDomain;
084        }
085
086        public void setReplyPubSubDomain(boolean replyPubSubDomain) {
087                this.replyPubSubDomain = replyPubSubDomain;
088        }
089
090        public boolean isReplyPubSubDomain() {
091                if (this.replyPubSubDomain != null) {
092                        return this.replyPubSubDomain;
093                }
094                else {
095                        return isPubSubDomain();
096                }
097        }
098
099        public void setSubscriptionDurable(boolean subscriptionDurable) {
100                this.subscriptionDurable = subscriptionDurable;
101                if (subscriptionDurable) {
102                        this.pubSubDomain = true;
103                }
104        }
105
106        public boolean isSubscriptionDurable() {
107                return this.subscriptionDurable;
108        }
109
110        public void setSubscriptionShared(boolean subscriptionShared) {
111                this.subscriptionShared = subscriptionShared;
112                if (subscriptionShared) {
113                        this.pubSubDomain = true;
114                }
115        }
116
117        public boolean isSubscriptionShared() {
118                return this.subscriptionShared;
119        }
120
121        public void setSubscriptionName(String subscriptionName) {
122                this.subscriptionName = subscriptionName;
123        }
124
125        public String getSubscriptionName() {
126                return this.subscriptionName;
127        }
128
129        public void setDurableSubscriptionName(String durableSubscriptionName) {
130                this.subscriptionName = durableSubscriptionName;
131                this.subscriptionDurable = true;
132        }
133
134        public String getDurableSubscriptionName() {
135                return (this.subscriptionDurable ? this.subscriptionName : null);
136        }
137
138        public void setClientId(String clientId) {
139                this.clientId = clientId;
140        }
141
142        public String getClientId() {
143                return this.clientId;
144        }
145
146        public void setMessageSelector(String messageSelector) {
147                this.messageSelector = messageSelector;
148        }
149
150        public String getMessageSelector() {
151                return this.messageSelector;
152        }
153
154        /**
155         * Set the JMS acknowledgement mode by the name of the corresponding constant
156         * in the JMS {@link Session} interface, e.g. "CLIENT_ACKNOWLEDGE".
157         * <p>Note that JCA resource adapters generally only support auto and dups-ok
158         * (see Spring's {@link StandardJmsActivationSpecFactory}). ActiveMQ also
159         * supports "SESSION_TRANSACTED" in the form of RA-managed transactions
160         * (automatically translated by Spring's {@link DefaultJmsActivationSpecFactory}.
161         * @param constantName the name of the {@link Session} acknowledge mode constant
162         * @see javax.jms.Session#AUTO_ACKNOWLEDGE
163         * @see javax.jms.Session#CLIENT_ACKNOWLEDGE
164         * @see javax.jms.Session#DUPS_OK_ACKNOWLEDGE
165         * @see javax.jms.Session#SESSION_TRANSACTED
166         * @see StandardJmsActivationSpecFactory
167         * @see DefaultJmsActivationSpecFactory
168         */
169        public void setAcknowledgeModeName(String constantName) {
170                setAcknowledgeMode(sessionConstants.asNumber(constantName).intValue());
171        }
172
173        /**
174         * Set the JMS acknowledgement mode to use.
175         * @see javax.jms.Session#AUTO_ACKNOWLEDGE
176         * @see javax.jms.Session#CLIENT_ACKNOWLEDGE
177         * @see javax.jms.Session#DUPS_OK_ACKNOWLEDGE
178         * @see javax.jms.Session#SESSION_TRANSACTED
179         */
180        public void setAcknowledgeMode(int acknowledgeMode) {
181                this.acknowledgeMode = acknowledgeMode;
182        }
183
184        /**
185         * Return the JMS acknowledgement mode to use.
186         */
187        public int getAcknowledgeMode() {
188                return this.acknowledgeMode;
189        }
190
191        /**
192         * Specify concurrency limits via a "lower-upper" String, e.g. "5-10", or a simple
193         * upper limit String, e.g. "10".
194         * <p>JCA listener containers will always scale from zero to the given upper limit.
195         * A specified lower limit will effectively be ignored.
196         * <p>This property is primarily supported for configuration compatibility with
197         * {@link org.springframework.jms.listener.DefaultMessageListenerContainer}.
198         * For this activation config, generally use {@link #setMaxConcurrency} instead.
199         */
200        public void setConcurrency(String concurrency) {
201                try {
202                        int separatorIndex = concurrency.indexOf('-');
203                        if (separatorIndex != -1) {
204                                setMaxConcurrency(Integer.parseInt(concurrency.substring(separatorIndex + 1)));
205                        }
206                        else {
207                                setMaxConcurrency(Integer.parseInt(concurrency));
208                        }
209                }
210                catch (NumberFormatException ex) {
211                        throw new IllegalArgumentException("Invalid concurrency value [" + concurrency + "]: only " +
212                                        "single maximum integer (e.g. \"5\") and minimum-maximum combo (e.g. \"3-5\") supported. " +
213                                        "Note that JmsActivationSpecConfig will effectively ignore the minimum value and " +
214                                        "scale from zero up to the number of consumers according to the maximum value.");
215                }
216        }
217
218        /**
219         * Specify the maximum number of consumers/sessions to use, effectively
220         * controlling the number of concurrent invocations on the target listener.
221         */
222        public void setMaxConcurrency(int maxConcurrency) {
223                this.maxConcurrency = maxConcurrency;
224        }
225
226        /**
227         * Return the maximum number of consumers/sessions to use.
228         */
229        public int getMaxConcurrency() {
230                return this.maxConcurrency;
231        }
232
233        /**
234         * Specify the maximum number of messages to load into a session
235         * (a kind of batch size).
236         */
237        public void setPrefetchSize(int prefetchSize) {
238                this.prefetchSize = prefetchSize;
239        }
240
241        /**
242         * Return the maximum number of messages to load into a session.
243         */
244        public int getPrefetchSize() {
245                return this.prefetchSize;
246        }
247
248        /**
249         * Set the {@link MessageConverter} strategy for converting JMS Messages.
250         * @param messageConverter the message converter to use
251         */
252        public void setMessageConverter(MessageConverter messageConverter) {
253                this.messageConverter = messageConverter;
254        }
255
256        /**
257         * Return the {@link MessageConverter} to use, if any.
258         */
259        public MessageConverter getMessageConverter() {
260                return this.messageConverter;
261        }
262
263}