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.model;
018
019import java.util.concurrent.RejectedExecutionHandler;
020import java.util.concurrent.TimeUnit;
021
022import javax.xml.bind.annotation.XmlAccessType;
023import javax.xml.bind.annotation.XmlAccessorType;
024import javax.xml.bind.annotation.XmlAttribute;
025import javax.xml.bind.annotation.XmlRootElement;
026import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
027
028import org.apache.camel.ThreadPoolRejectedPolicy;
029import org.apache.camel.builder.xml.TimeUnitAdapter;
030import org.apache.camel.spi.Metadata;
031
032/**
033 * To configure thread pools
034 *
035 * @version 
036 */
037@Metadata(label = "configuration")
038@XmlRootElement(name = "threadPoolProfile")
039@XmlAccessorType(XmlAccessType.FIELD)
040public class ThreadPoolProfileDefinition extends OptionalIdentifiedDefinition<ThreadPoolProfileDefinition> {
041    @XmlAttribute
042    private Boolean defaultProfile;
043    @XmlAttribute
044    private String poolSize;
045    @XmlAttribute
046    private String maxPoolSize;
047    @XmlAttribute
048    private String keepAliveTime;
049    @XmlAttribute
050    @XmlJavaTypeAdapter(TimeUnitAdapter.class)
051    private TimeUnit timeUnit;
052    @XmlAttribute
053    private String maxQueueSize;
054    @XmlAttribute
055    private String allowCoreThreadTimeOut;
056    @XmlAttribute
057    private ThreadPoolRejectedPolicy rejectedPolicy;
058
059    public ThreadPoolProfileDefinition() {
060    }
061
062    @Override
063    public String getShortName() {
064        return "threadPoolProfile";
065    }
066
067    @Override
068    public String getLabel() {
069        return "ThreadPoolProfile " + getId();
070    }
071
072    public ThreadPoolProfileDefinition poolSize(int poolSize) {
073        return poolSize("" + poolSize);
074    }
075
076    public ThreadPoolProfileDefinition poolSize(String poolSize) {
077        setPoolSize(poolSize);
078        return this;
079    }
080
081    public ThreadPoolProfileDefinition maxPoolSize(int maxPoolSize) {
082        return maxPoolSize("" + maxQueueSize);
083    }
084
085    public ThreadPoolProfileDefinition maxPoolSize(String maxPoolSize) {
086        setMaxPoolSize("" + maxPoolSize);
087        return this;
088    }
089
090    public ThreadPoolProfileDefinition keepAliveTime(long keepAliveTime) {
091        return keepAliveTime("" + keepAliveTime);
092    }
093
094    public ThreadPoolProfileDefinition keepAliveTime(String keepAliveTime) {
095        setKeepAliveTime("" + keepAliveTime);
096        return this;
097    }
098
099    public ThreadPoolProfileDefinition timeUnit(TimeUnit timeUnit) {
100        setTimeUnit(timeUnit);
101        return this;
102    }
103
104    public ThreadPoolProfileDefinition maxQueueSize(int maxQueueSize) {
105        return maxQueueSize("" + maxQueueSize);
106    }
107
108    public ThreadPoolProfileDefinition maxQueueSize(String maxQueueSize) {
109        setMaxQueueSize("" + maxQueueSize);
110        return this;
111    }
112
113    public ThreadPoolProfileDefinition rejectedPolicy(ThreadPoolRejectedPolicy rejectedPolicy) {
114        setRejectedPolicy(rejectedPolicy);
115        return this;
116    }
117
118    public ThreadPoolProfileDefinition allowCoreThreadTimeOut(boolean allowCoreThreadTimeOut) {
119        setAllowCoreThreadTimeOut("" + allowCoreThreadTimeOut);
120        return this;
121    }
122
123    public Boolean getDefaultProfile() {
124        return defaultProfile;
125    }
126
127    /**
128     * Whether this profile is the default thread pool profile
129     */
130    public void setDefaultProfile(Boolean defaultProfile) {
131        this.defaultProfile = defaultProfile;
132    }
133
134    public Boolean isDefaultProfile() {
135        return defaultProfile != null && defaultProfile;
136    }
137
138    public String getPoolSize() {
139        return poolSize;
140    }
141
142    /**
143     * Sets the core pool size
144     */
145    public void setPoolSize(String poolSize) {
146        this.poolSize = poolSize;
147    }
148
149    public String getMaxPoolSize() {
150        return maxPoolSize;
151    }
152
153    /**
154     * Sets the maximum pool size
155     */
156    public void setMaxPoolSize(String maxPoolSize) {
157        this.maxPoolSize = maxPoolSize;
158    }
159
160    public String getKeepAliveTime() {
161        return keepAliveTime;
162    }
163
164    /**
165     * Sets the keep alive time for idle threads in the pool
166     */
167    public void setKeepAliveTime(String keepAliveTime) {
168        this.keepAliveTime = keepAliveTime;
169    }
170
171    public String getMaxQueueSize() {
172        return maxQueueSize;
173    }
174
175    /**
176     * Sets the maximum number of tasks in the work queue.
177     * <p/>
178     * Use <tt>-1</tt> or <tt>Integer.MAX_VALUE</tt> for an unbounded queue
179     */
180    public void setMaxQueueSize(String maxQueueSize) {
181        this.maxQueueSize = maxQueueSize;
182    }
183
184    public String getAllowCoreThreadTimeOut() {
185        return allowCoreThreadTimeOut;
186    }
187
188    /**
189     * Whether idle core threads is allowed to timeout and therefore can shrink the pool size below the core pool size
190     * <p/>
191     * Is by default <tt>false</tt>
192     */
193    public void setAllowCoreThreadTimeOut(String allowCoreThreadTimeOut) {
194        this.allowCoreThreadTimeOut = allowCoreThreadTimeOut;
195    }
196
197    public TimeUnit getTimeUnit() {
198        return timeUnit;
199    }
200
201    /**
202     * Sets the time unit to use for keep alive time
203     * By default SECONDS is used.
204     */
205    public void setTimeUnit(TimeUnit timeUnit) {
206        this.timeUnit = timeUnit;
207    }
208
209    public ThreadPoolRejectedPolicy getRejectedPolicy() {
210        return rejectedPolicy;
211    }
212
213    public RejectedExecutionHandler getRejectedExecutionHandler() {
214        if (rejectedPolicy != null) {
215            return rejectedPolicy.asRejectedExecutionHandler();
216        }
217        return null;
218    }
219
220    /**
221     * Sets the handler for tasks which cannot be executed by the thread pool.
222     */
223    public void setRejectedPolicy(ThreadPoolRejectedPolicy rejectedPolicy) {
224        this.rejectedPolicy = rejectedPolicy;
225    }
226
227}