1   /*
2    * Copyright 2002-2014 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.config;
18  
19  import java.util.concurrent.Executor;
20  
21  import org.springframework.jms.listener.DefaultMessageListenerContainer;
22  import org.springframework.transaction.PlatformTransactionManager;
23  import org.springframework.util.backoff.BackOff;
24  
25  /**
26   * A {@link JmsListenerContainerFactory} implementation to build a regular
27   * {@link DefaultMessageListenerContainer}.
28   *
29   * <p>This should be the default for most users and a good transition paths
30   * for those that are used to build such container definition manually.
31   *
32   * @author Stephane Nicoll
33   * @since 4.1
34   */
35  public class DefaultJmsListenerContainerFactory
36  		extends AbstractJmsListenerContainerFactory<DefaultMessageListenerContainer> {
37  
38  	private Executor taskExecutor;
39  
40  	private PlatformTransactionManager transactionManager;
41  
42  	private Integer cacheLevel;
43  
44  	private String cacheLevelName;
45  
46  	private String concurrency;
47  
48  	private Integer maxMessagesPerTask;
49  
50  	private Long receiveTimeout;
51  
52  	private Long recoveryInterval;
53  
54  	private BackOff backOff;
55  
56  
57  	/**
58  	 * @see DefaultMessageListenerContainer#setTaskExecutor
59  	 */
60  	public void setTaskExecutor(Executor taskExecutor) {
61  		this.taskExecutor = taskExecutor;
62  	}
63  
64  	/**
65  	 * @see DefaultMessageListenerContainer#setTransactionManager
66  	 */
67  	public void setTransactionManager(PlatformTransactionManager transactionManager) {
68  		this.transactionManager = transactionManager;
69  	}
70  
71  	/**
72  	 * @see DefaultMessageListenerContainer#setCacheLevel
73  	 */
74  	public void setCacheLevel(Integer cacheLevel) {
75  		this.cacheLevel = cacheLevel;
76  	}
77  
78  	/**
79  	 * @see DefaultMessageListenerContainer#setCacheLevelName
80  	 */
81  	public void setCacheLevelName(String cacheLevelName) {
82  		this.cacheLevelName = cacheLevelName;
83  	}
84  
85  	/**
86  	 * @see DefaultMessageListenerContainer#setConcurrency
87  	 */
88  	public void setConcurrency(String concurrency) {
89  		this.concurrency = concurrency;
90  	}
91  
92  	/**
93  	 * @see DefaultMessageListenerContainer#setMaxMessagesPerTask
94  	 */
95  	public void setMaxMessagesPerTask(Integer maxMessagesPerTask) {
96  		this.maxMessagesPerTask = maxMessagesPerTask;
97  	}
98  
99  	/**
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 }