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