001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.camel.spi;
018
019 import java.io.Serializable;
020 import java.util.concurrent.RejectedExecutionHandler;
021 import java.util.concurrent.TimeUnit;
022
023 import org.apache.camel.ThreadPoolRejectedPolicy;
024
025 /**
026 * A profile which defines thread pool settings.
027 * <p/>
028 * See more details at <a href="http://camel.apache.org/threading-model.html">threading model</a>
029 *
030 * @version
031 */
032 public class ThreadPoolProfile implements Serializable, Cloneable {
033
034 // TODO: Camel 2.9/3.0 consider moving to org.apache.camel
035
036 private static final long serialVersionUID = 1L;
037
038 private String id;
039 private Boolean defaultProfile;
040 private Integer poolSize;
041 private Integer maxPoolSize;
042 private Long keepAliveTime;
043 private TimeUnit timeUnit;
044 private Integer maxQueueSize;
045 private ThreadPoolRejectedPolicy rejectedPolicy;
046
047 /**
048 * Creates a new thread pool profile, with no id set.
049 */
050 public ThreadPoolProfile() {
051 }
052
053 /**
054 * Creates a new thread pool profile
055 *
056 * @param id id of the profile
057 */
058 public ThreadPoolProfile(String id) {
059 this.id = id;
060 }
061
062 /**
063 * Gets the id of this profile
064 *
065 * @return the id of this profile
066 */
067 public String getId() {
068 return id;
069 }
070
071 /**
072 * Sets the id of this profile
073 *
074 * @param id profile id
075 */
076 public void setId(String id) {
077 this.id = id;
078 }
079
080 /**
081 * Whether this profile is the default profile (there can only be one).
082 *
083 * @return <tt>true</tt> if its the default profile, <tt>false</tt> otherwise
084 */
085 public Boolean isDefaultProfile() {
086 return defaultProfile != null && defaultProfile;
087 }
088
089 /**
090 * Sets whether this profile is the default profile (there can only be one).
091 *
092 * @param defaultProfile the option
093 */
094 public void setDefaultProfile(Boolean defaultProfile) {
095 this.defaultProfile = defaultProfile;
096 }
097
098 /**
099 * Gets the core pool size (threads to keep minimum in pool)
100 *
101 * @return the pool size
102 */
103 public Integer getPoolSize() {
104 return poolSize;
105 }
106
107 /**
108 * Sets the core pool size (threads to keep minimum in pool)
109 *
110 * @param poolSize the pool size
111 */
112 public void setPoolSize(Integer poolSize) {
113 this.poolSize = poolSize;
114 }
115
116 /**
117 * Gets the maximum pool size
118 *
119 * @return the maximum pool size
120 */
121 public Integer getMaxPoolSize() {
122 return maxPoolSize;
123 }
124
125 /**
126 * Sets the maximum pool size
127 *
128 * @param maxPoolSize the max pool size
129 */
130 public void setMaxPoolSize(Integer maxPoolSize) {
131 this.maxPoolSize = maxPoolSize;
132 }
133
134 /**
135 * Gets the keep alive time for inactive threads
136 *
137 * @return the keep alive time
138 */
139 public Long getKeepAliveTime() {
140 return keepAliveTime;
141 }
142
143 /**
144 * Sets the keep alive time for inactive threads
145 *
146 * @param keepAliveTime the keep alive time
147 */
148 public void setKeepAliveTime(Long keepAliveTime) {
149 this.keepAliveTime = keepAliveTime;
150 }
151
152 /**
153 * Gets the time unit used for keep alive time
154 *
155 * @return the time unit
156 */
157 public TimeUnit getTimeUnit() {
158 return timeUnit;
159 }
160
161 /**
162 * Sets the time unit used for keep alive time
163 *
164 * @param timeUnit the time unit
165 */
166 public void setTimeUnit(TimeUnit timeUnit) {
167 this.timeUnit = timeUnit;
168 }
169
170 /**
171 * Gets the maximum number of tasks in the work queue.
172 * <p/>
173 * Use <tt>-1</tt> or <tt>Integer.MAX_VALUE</tt> for an unbounded queue
174 *
175 * @return the max queue size
176 */
177 public Integer getMaxQueueSize() {
178 return maxQueueSize;
179 }
180
181 /**
182 * Sets the maximum number of tasks in the work queue.
183 * <p/>
184 * Use <tt>-1</tt> or <tt>Integer.MAX_VALUE</tt> for an unbounded queue
185 *
186 * @param maxQueueSize the max queue size
187 */
188 public void setMaxQueueSize(Integer maxQueueSize) {
189 this.maxQueueSize = maxQueueSize;
190 }
191
192 /**
193 * Gets the policy for tasks which cannot be executed by the thread pool.
194 *
195 * @return the policy for the handler
196 */
197 public ThreadPoolRejectedPolicy getRejectedPolicy() {
198 return rejectedPolicy;
199 }
200
201 /**
202 * Gets the handler for tasks which cannot be executed by the thread pool.
203 *
204 * @return the handler, or <tt>null</tt> if none defined
205 */
206 public RejectedExecutionHandler getRejectedExecutionHandler() {
207 if (rejectedPolicy != null) {
208 return rejectedPolicy.asRejectedExecutionHandler();
209 }
210 return null;
211 }
212
213 /**
214 * Sets the handler for tasks which cannot be executed by the thread pool.
215 *
216 * @param rejectedPolicy the policy for the handler
217 */
218 public void setRejectedPolicy(ThreadPoolRejectedPolicy rejectedPolicy) {
219 this.rejectedPolicy = rejectedPolicy;
220 }
221
222 /**
223 * Overwrites each attribute that is null with the attribute from defaultProfile
224 *
225 * @param defaultProfile profile with default values
226 */
227 public void addDefaults(ThreadPoolProfile defaultProfile) {
228 if (defaultProfile == null) {
229 return;
230 }
231 if (poolSize == null) {
232 poolSize = defaultProfile.getPoolSize();
233 }
234 if (maxPoolSize == null) {
235 maxPoolSize = defaultProfile.getMaxPoolSize();
236 }
237 if (keepAliveTime == null) {
238 keepAliveTime = defaultProfile.getKeepAliveTime();
239 }
240 if (timeUnit == null) {
241 timeUnit = defaultProfile.getTimeUnit();
242 }
243 if (maxQueueSize == null) {
244 maxQueueSize = defaultProfile.getMaxQueueSize();
245 }
246 if (rejectedPolicy == null) {
247 rejectedPolicy = defaultProfile.getRejectedPolicy();
248 }
249 }
250
251 @Override
252 public ThreadPoolProfile clone() {
253 ThreadPoolProfile cloned = new ThreadPoolProfile();
254 cloned.setDefaultProfile(defaultProfile);
255 cloned.setId(id);
256 cloned.setKeepAliveTime(keepAliveTime);
257 cloned.setMaxPoolSize(maxPoolSize);
258 cloned.setMaxQueueSize(maxQueueSize);
259 cloned.setPoolSize(maxPoolSize);
260 cloned.setRejectedPolicy(rejectedPolicy);
261 cloned.setTimeUnit(timeUnit);
262 return cloned;
263 }
264
265 @Override
266 public String toString() {
267 return "ThreadPoolProfile[" + id + " (" + defaultProfile + ") size:" + poolSize + "-" + maxPoolSize
268 + ", keepAlive: " + keepAliveTime + " " + timeUnit + ", maxQueue: " + maxQueueSize
269 + ", rejectedPolicy:" + rejectedPolicy + "]";
270 }
271
272 }