001/*
002 * Copyright 2002-2020 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.config;
018
019import javax.jms.ConnectionFactory;
020import javax.jms.ExceptionListener;
021
022import org.apache.commons.logging.Log;
023import org.apache.commons.logging.LogFactory;
024
025import org.springframework.jms.listener.AbstractMessageListenerContainer;
026import org.springframework.jms.support.QosSettings;
027import org.springframework.jms.support.converter.MessageConverter;
028import org.springframework.jms.support.destination.DestinationResolver;
029import org.springframework.lang.Nullable;
030import org.springframework.util.ErrorHandler;
031
032/**
033 * Base {@link JmsListenerContainerFactory} for Spring's base container implementation.
034 *
035 * @author Stephane Nicoll
036 * @since 4.1
037 * @param <C> the container type
038 * @see AbstractMessageListenerContainer
039 */
040public abstract class AbstractJmsListenerContainerFactory<C extends AbstractMessageListenerContainer>
041                implements JmsListenerContainerFactory<C> {
042
043        protected final Log logger = LogFactory.getLog(getClass());
044
045        @Nullable
046        private ConnectionFactory connectionFactory;
047
048        @Nullable
049        private DestinationResolver destinationResolver;
050
051        @Nullable
052        private MessageConverter messageConverter;
053
054        @Nullable
055        private ExceptionListener exceptionListener;
056
057        @Nullable
058        private ErrorHandler errorHandler;
059
060        @Nullable
061        private Boolean sessionTransacted;
062
063        @Nullable
064        private Integer sessionAcknowledgeMode;
065
066        @Nullable
067        private Boolean pubSubDomain;
068
069        @Nullable
070        private Boolean replyPubSubDomain;
071
072        @Nullable
073        private QosSettings replyQosSettings;
074
075        @Nullable
076        private Boolean subscriptionDurable;
077
078        @Nullable
079        private Boolean subscriptionShared;
080
081        @Nullable
082        private String clientId;
083
084        @Nullable
085        private Integer phase;
086
087        @Nullable
088        private Boolean autoStartup;
089
090
091        /**
092         * @see AbstractMessageListenerContainer#setConnectionFactory(ConnectionFactory)
093         */
094        public void setConnectionFactory(ConnectionFactory connectionFactory) {
095                this.connectionFactory = connectionFactory;
096        }
097
098        /**
099         * @see AbstractMessageListenerContainer#setDestinationResolver(DestinationResolver)
100         */
101        public void setDestinationResolver(DestinationResolver destinationResolver) {
102                this.destinationResolver = destinationResolver;
103        }
104
105        /**
106         * @see AbstractMessageListenerContainer#setMessageConverter(MessageConverter)
107         */
108        public void setMessageConverter(MessageConverter messageConverter) {
109                this.messageConverter = messageConverter;
110        }
111
112        /**
113         * @since 5.2.8
114         * @see AbstractMessageListenerContainer#setExceptionListener(ExceptionListener)
115         */
116        public void setExceptionListener(ExceptionListener exceptionListener) {
117                this.exceptionListener = exceptionListener;
118        }
119
120        /**
121         * @see AbstractMessageListenerContainer#setErrorHandler(ErrorHandler)
122         */
123        public void setErrorHandler(ErrorHandler errorHandler) {
124                this.errorHandler = errorHandler;
125        }
126
127        /**
128         * @see AbstractMessageListenerContainer#setSessionTransacted(boolean)
129         */
130        public void setSessionTransacted(Boolean sessionTransacted) {
131                this.sessionTransacted = sessionTransacted;
132        }
133
134        /**
135         * @see AbstractMessageListenerContainer#setSessionAcknowledgeMode(int)
136         */
137        public void setSessionAcknowledgeMode(Integer sessionAcknowledgeMode) {
138                this.sessionAcknowledgeMode = sessionAcknowledgeMode;
139        }
140
141        /**
142         * @see AbstractMessageListenerContainer#setPubSubDomain(boolean)
143         */
144        public void setPubSubDomain(Boolean pubSubDomain) {
145                this.pubSubDomain = pubSubDomain;
146        }
147
148        /**
149         * @see AbstractMessageListenerContainer#setReplyPubSubDomain(boolean)
150         */
151        public void setReplyPubSubDomain(Boolean replyPubSubDomain) {
152                this.replyPubSubDomain = replyPubSubDomain;
153        }
154
155        /**
156         * @see AbstractMessageListenerContainer#setReplyQosSettings(QosSettings)
157         */
158        public void setReplyQosSettings(QosSettings replyQosSettings) {
159                this.replyQosSettings = replyQosSettings;
160        }
161
162        /**
163         * @see AbstractMessageListenerContainer#setSubscriptionDurable(boolean)
164         */
165        public void setSubscriptionDurable(Boolean subscriptionDurable) {
166                this.subscriptionDurable = subscriptionDurable;
167        }
168
169        /**
170         * @see AbstractMessageListenerContainer#setSubscriptionShared(boolean)
171         */
172        public void setSubscriptionShared(Boolean subscriptionShared) {
173                this.subscriptionShared = subscriptionShared;
174        }
175
176        /**
177         * @see AbstractMessageListenerContainer#setClientId(String)
178         */
179        public void setClientId(String clientId) {
180                this.clientId = clientId;
181        }
182
183        /**
184         * @see AbstractMessageListenerContainer#setPhase(int)
185         */
186        public void setPhase(int phase) {
187                this.phase = phase;
188        }
189
190        /**
191         * @see AbstractMessageListenerContainer#setAutoStartup(boolean)
192         */
193        public void setAutoStartup(boolean autoStartup) {
194                this.autoStartup = autoStartup;
195        }
196
197
198        @Override
199        public C createListenerContainer(JmsListenerEndpoint endpoint) {
200                C instance = createContainerInstance();
201
202                if (this.connectionFactory != null) {
203                        instance.setConnectionFactory(this.connectionFactory);
204                }
205                if (this.destinationResolver != null) {
206                        instance.setDestinationResolver(this.destinationResolver);
207                }
208                if (this.messageConverter != null) {
209                        instance.setMessageConverter(this.messageConverter);
210                }
211                if (this.exceptionListener != null) {
212                        instance.setExceptionListener(this.exceptionListener);
213                }
214                if (this.errorHandler != null) {
215                        instance.setErrorHandler(this.errorHandler);
216                }
217                if (this.sessionTransacted != null) {
218                        instance.setSessionTransacted(this.sessionTransacted);
219                }
220                if (this.sessionAcknowledgeMode != null) {
221                        instance.setSessionAcknowledgeMode(this.sessionAcknowledgeMode);
222                }
223                if (this.pubSubDomain != null) {
224                        instance.setPubSubDomain(this.pubSubDomain);
225                }
226                if (this.replyPubSubDomain != null) {
227                        instance.setReplyPubSubDomain(this.replyPubSubDomain);
228                }
229                if (this.replyQosSettings != null) {
230                        instance.setReplyQosSettings(this.replyQosSettings);
231                }
232                if (this.subscriptionDurable != null) {
233                        instance.setSubscriptionDurable(this.subscriptionDurable);
234                }
235                if (this.subscriptionShared != null) {
236                        instance.setSubscriptionShared(this.subscriptionShared);
237                }
238                if (this.clientId != null) {
239                        instance.setClientId(this.clientId);
240                }
241                if (this.phase != null) {
242                        instance.setPhase(this.phase);
243                }
244                if (this.autoStartup != null) {
245                        instance.setAutoStartup(this.autoStartup);
246                }
247
248                initializeContainer(instance);
249                endpoint.setupListenerContainer(instance);
250
251                return instance;
252        }
253
254        /**
255         * Create an empty container instance.
256         */
257        protected abstract C createContainerInstance();
258
259        /**
260         * Further initialize the specified container.
261         * <p>Subclasses can inherit from this method to apply extra
262         * configuration if necessary.
263         */
264        protected void initializeContainer(C instance) {
265        }
266
267}