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