001/*
002 * Copyright 2002-2014 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 java.util.concurrent.Executor;
020
021import org.springframework.jms.listener.DefaultMessageListenerContainer;
022import org.springframework.transaction.PlatformTransactionManager;
023import org.springframework.util.backoff.BackOff;
024
025/**
026 * A {@link JmsListenerContainerFactory} implementation to build a regular
027 * {@link DefaultMessageListenerContainer}.
028 *
029 * <p>This should be the default for most users and a good transition paths
030 * for those that are used to build such container definition manually.
031 *
032 * @author Stephane Nicoll
033 * @since 4.1
034 */
035public class DefaultJmsListenerContainerFactory
036                extends AbstractJmsListenerContainerFactory<DefaultMessageListenerContainer> {
037
038        private Executor taskExecutor;
039
040        private PlatformTransactionManager transactionManager;
041
042        private Integer cacheLevel;
043
044        private String cacheLevelName;
045
046        private String concurrency;
047
048        private Integer maxMessagesPerTask;
049
050        private Long receiveTimeout;
051
052        private Long recoveryInterval;
053
054        private BackOff backOff;
055
056
057        /**
058         * @see DefaultMessageListenerContainer#setTaskExecutor
059         */
060        public void setTaskExecutor(Executor taskExecutor) {
061                this.taskExecutor = taskExecutor;
062        }
063
064        /**
065         * @see DefaultMessageListenerContainer#setTransactionManager
066         */
067        public void setTransactionManager(PlatformTransactionManager transactionManager) {
068                this.transactionManager = transactionManager;
069        }
070
071        /**
072         * @see DefaultMessageListenerContainer#setCacheLevel
073         */
074        public void setCacheLevel(Integer cacheLevel) {
075                this.cacheLevel = cacheLevel;
076        }
077
078        /**
079         * @see DefaultMessageListenerContainer#setCacheLevelName
080         */
081        public void setCacheLevelName(String cacheLevelName) {
082                this.cacheLevelName = cacheLevelName;
083        }
084
085        /**
086         * @see DefaultMessageListenerContainer#setConcurrency
087         */
088        public void setConcurrency(String concurrency) {
089                this.concurrency = concurrency;
090        }
091
092        /**
093         * @see DefaultMessageListenerContainer#setMaxMessagesPerTask
094         */
095        public void setMaxMessagesPerTask(Integer maxMessagesPerTask) {
096                this.maxMessagesPerTask = maxMessagesPerTask;
097        }
098
099        /**
100         * @see DefaultMessageListenerContainer#setReceiveTimeout
101         */
102        public void setReceiveTimeout(Long receiveTimeout) {
103                this.receiveTimeout = receiveTimeout;
104        }
105
106        /**
107         * @see DefaultMessageListenerContainer#setRecoveryInterval
108         */
109        public void setRecoveryInterval(Long recoveryInterval) {
110                this.recoveryInterval = recoveryInterval;
111        }
112
113        /**
114         * @see DefaultMessageListenerContainer#setBackOff
115         */
116        public void setBackOff(BackOff backOff) {
117                this.backOff = backOff;
118        }
119
120
121        @Override
122        protected DefaultMessageListenerContainer createContainerInstance() {
123                return new DefaultMessageListenerContainer();
124        }
125
126        @Override
127        protected void initializeContainer(DefaultMessageListenerContainer container) {
128                if (this.taskExecutor != null) {
129                        container.setTaskExecutor(this.taskExecutor);
130                }
131                if (this.transactionManager != null) {
132                        container.setTransactionManager(this.transactionManager);
133                }
134
135                if (this.cacheLevel != null) {
136                        container.setCacheLevel(this.cacheLevel);
137                }
138                else if (this.cacheLevelName != null) {
139                        container.setCacheLevelName(this.cacheLevelName);
140                }
141
142                if (this.concurrency != null) {
143                        container.setConcurrency(this.concurrency);
144                }
145                if (this.maxMessagesPerTask != null) {
146                        container.setMaxMessagesPerTask(this.maxMessagesPerTask);
147                }
148                if (this.receiveTimeout != null) {
149                        container.setReceiveTimeout(this.receiveTimeout);
150                }
151
152                if (this.backOff != null) {
153                        container.setBackOff(this.backOff);
154                        if (this.recoveryInterval != null) {
155                                logger.warn("Ignoring recovery interval in DefaultJmsListenerContainerFactory in favor of BackOff");
156                        }
157                }
158                else if (this.recoveryInterval != null) {
159                        container.setRecoveryInterval(this.recoveryInterval);
160                }
161        }
162
163}