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 */
017package org.apache.camel.spi;
018
019import java.io.Serializable;
020import java.util.concurrent.RejectedExecutionHandler;
021import java.util.concurrent.TimeUnit;
022
023import org.apache.camel.util.concurrent.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 */
030public class ThreadPoolProfile implements Serializable, Cloneable {
031
032    private static final long serialVersionUID = 1L;
033
034    private String id;
035    private Boolean defaultProfile;
036    private Integer poolSize;
037    private Integer maxPoolSize;
038    private Long keepAliveTime;
039    private TimeUnit timeUnit;
040    private Integer maxQueueSize;
041    private Boolean allowCoreThreadTimeOut;
042    private ThreadPoolRejectedPolicy rejectedPolicy;
043
044    /**
045     * Creates a new thread pool profile, with no id set.
046     */
047    public ThreadPoolProfile() {
048    }
049
050    /**
051     * Creates a new thread pool profile
052     *
053     * @param id id of the profile
054     */
055    public ThreadPoolProfile(String id) {
056        this.id = id;
057    }
058
059    /**
060     * Gets the id of this profile
061     *
062     * @return the id of this profile
063     */
064    public String getId() {
065        return id;
066    }
067
068    /**
069     * Sets the id of this profile
070     *
071     * @param id profile id
072     */
073    public void setId(String id) {
074        this.id = id;
075    }
076
077    /**
078     * Whether this profile is the default profile (there can only be one).
079     *
080     * @return <tt>true</tt> if its the default profile, <tt>false</tt> otherwise
081     */
082    public Boolean isDefaultProfile() {
083        return defaultProfile != null && defaultProfile;
084    }
085
086    /**
087     * Sets whether this profile is the default profile (there can only be one).
088     *
089     * @param defaultProfile the option
090     */
091    public void setDefaultProfile(Boolean defaultProfile) {
092        this.defaultProfile = defaultProfile;
093    }
094
095    /**
096     * Gets the core pool size (threads to keep minimum in pool)
097     *
098     * @return the pool size
099     */
100    public Integer getPoolSize() {
101        return poolSize;
102    }
103
104    /**
105     * Sets the core pool size (threads to keep minimum in pool)
106     *
107     * @param poolSize the pool size
108     */
109    public void setPoolSize(Integer poolSize) {
110        this.poolSize = poolSize;
111    }
112
113    /**
114     * Gets the maximum pool size
115     *
116     * @return the maximum pool size
117     */
118    public Integer getMaxPoolSize() {
119        return maxPoolSize;
120    }
121
122    /**
123     * Sets the maximum pool size
124     *
125     * @param maxPoolSize the max pool size
126     */
127    public void setMaxPoolSize(Integer maxPoolSize) {
128        this.maxPoolSize = maxPoolSize;
129    }
130
131    /**
132     * Gets the keep alive time for inactive threads
133     *
134     * @return the keep alive time
135     */
136    public Long getKeepAliveTime() {
137        return keepAliveTime;
138    }
139
140    /**
141     * Sets the keep alive time for inactive threads
142     *
143     * @param keepAliveTime the keep alive time
144     */
145    public void setKeepAliveTime(Long keepAliveTime) {
146        this.keepAliveTime = keepAliveTime;
147    }
148
149    /**
150     * Gets the time unit used for keep alive time
151     *
152     * @return the time unit
153     */
154    public TimeUnit getTimeUnit() {
155        return timeUnit;
156    }
157
158    /**
159     * Sets the time unit used for keep alive time
160     *
161     * @param timeUnit the time unit
162     */
163    public void setTimeUnit(TimeUnit timeUnit) {
164        this.timeUnit = timeUnit;
165    }
166
167    /**
168     * Gets the maximum number of tasks in the work queue.
169     * <p/>
170     * Use <tt>-1</tt> or <tt>Integer.MAX_VALUE</tt> for an unbounded queue
171     *
172     * @return the max queue size
173     */
174    public Integer getMaxQueueSize() {
175        return maxQueueSize;
176    }
177
178    /**
179     * Sets the maximum number of tasks in the work queue.
180     * <p/>
181     * Use <tt>-1</tt> or <tt>Integer.MAX_VALUE</tt> for an unbounded queue
182     *
183     * @param maxQueueSize the max queue size
184     */
185    public void setMaxQueueSize(Integer maxQueueSize) {
186        this.maxQueueSize = maxQueueSize;
187    }
188
189    /**
190     * Gets whether to allow core threads to timeout
191     *
192     * @return the allow core threads to timeout
193     */
194    public Boolean getAllowCoreThreadTimeOut() {
195        return allowCoreThreadTimeOut;
196    }
197
198    /**
199     * Sets whether to allow core threads to timeout
200     *
201     * @param allowCoreThreadTimeOut <tt>true</tt> to allow timeout
202     */
203    public void setAllowCoreThreadTimeOut(Boolean allowCoreThreadTimeOut) {
204        this.allowCoreThreadTimeOut = allowCoreThreadTimeOut;
205    }
206
207    /**
208     * Gets the policy for tasks which cannot be executed by the thread pool.
209     *
210     * @return the policy for the handler
211     */
212    public ThreadPoolRejectedPolicy getRejectedPolicy() {
213        return rejectedPolicy;
214    }
215
216    /**
217     * Gets the handler for tasks which cannot be executed by the thread pool.
218     *
219     * @return the handler, or <tt>null</tt> if none defined
220     */
221    public RejectedExecutionHandler getRejectedExecutionHandler() {
222        if (rejectedPolicy != null) {
223            return rejectedPolicy.asRejectedExecutionHandler();
224        }
225        return null;
226    }
227
228    /**
229     * Sets the handler for tasks which cannot be executed by the thread pool.
230     *
231     * @param rejectedPolicy  the policy for the handler
232     */
233    public void setRejectedPolicy(ThreadPoolRejectedPolicy rejectedPolicy) {
234        this.rejectedPolicy = rejectedPolicy;
235    }
236
237    /**
238     * Overwrites each attribute that is null with the attribute from defaultProfile 
239     * 
240     * @param defaultProfile profile with default values
241     */
242    public void addDefaults(ThreadPoolProfile defaultProfile) {
243        if (defaultProfile == null) {
244            return;
245        }
246        if (poolSize == null) {
247            poolSize = defaultProfile.getPoolSize();
248        }
249        if (maxPoolSize == null) {
250            maxPoolSize = defaultProfile.getMaxPoolSize();
251        }
252        if (keepAliveTime == null) {
253            keepAliveTime = defaultProfile.getKeepAliveTime();
254        }
255        if (timeUnit == null) {
256            timeUnit = defaultProfile.getTimeUnit();
257        }
258        if (maxQueueSize == null) {
259            maxQueueSize = defaultProfile.getMaxQueueSize();
260        }
261        if (allowCoreThreadTimeOut == null) {
262            allowCoreThreadTimeOut = defaultProfile.getAllowCoreThreadTimeOut();
263        }
264        if (rejectedPolicy == null) {
265            rejectedPolicy = defaultProfile.getRejectedPolicy();
266        }
267    }
268
269    @Override
270    public ThreadPoolProfile clone() {
271        ThreadPoolProfile cloned = new ThreadPoolProfile();
272        cloned.setDefaultProfile(defaultProfile);
273        cloned.setId(id);
274        cloned.setKeepAliveTime(keepAliveTime);
275        cloned.setMaxPoolSize(maxPoolSize);
276        cloned.setMaxQueueSize(maxQueueSize);
277        cloned.setPoolSize(maxPoolSize);
278        cloned.setAllowCoreThreadTimeOut(allowCoreThreadTimeOut);
279        cloned.setRejectedPolicy(rejectedPolicy);
280        cloned.setTimeUnit(timeUnit);
281        return cloned;
282    }
283
284    @Override
285    public String toString() {
286        return "ThreadPoolProfile[" + id + " (" + defaultProfile + ") size:" + poolSize + "-" + maxPoolSize
287                + ", keepAlive: " + keepAliveTime + " " + timeUnit + ", maxQueue: " + maxQueueSize
288                + ", allowCoreThreadTimeOut:" + allowCoreThreadTimeOut + ", rejectedPolicy:" + rejectedPolicy + "]";
289    }
290
291}