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.builder;
018    
019    import java.util.concurrent.ExecutorService;
020    import java.util.concurrent.TimeUnit;
021    
022    import org.apache.camel.CamelContext;
023    import org.apache.camel.ThreadPoolRejectedPolicy;
024    import org.apache.camel.model.ThreadPoolProfileDefinition;
025    
026    /**
027     * A builder to create thread pools.
028     *
029     * @version $Revision: 18413 $
030     */
031    public final class ThreadPoolBuilder {
032    
033        private final CamelContext camelContext;
034        private ThreadPoolProfileDefinition threadPoolDefinition;
035    
036        public ThreadPoolBuilder(CamelContext camelContext) {
037            this.camelContext = camelContext;
038            // use the default thread profile as the base
039            this.threadPoolDefinition = new ThreadPoolProfileDefinition(camelContext.getExecutorServiceStrategy().getDefaultThreadPoolProfile());
040        }
041    
042        public ThreadPoolBuilder poolSize(int poolSize) {
043            threadPoolDefinition.poolSize(poolSize);
044            return this;
045        }
046    
047        public ThreadPoolBuilder maxPoolSize(int maxPoolSize) {
048            threadPoolDefinition.maxPoolSize(maxPoolSize);
049            return this;
050        }
051    
052        public ThreadPoolBuilder keepAliveTime(long keepAliveTime) {
053            threadPoolDefinition.keepAliveTime(keepAliveTime);
054            return this;
055        }
056    
057        public ThreadPoolBuilder timeUnit(TimeUnit timeUnit) {
058            threadPoolDefinition.timeUnit(timeUnit);
059            return this;
060        }
061    
062        public ThreadPoolBuilder maxQueueSize(int maxQueueSize) {
063            threadPoolDefinition.maxQueueSize(maxQueueSize);
064            return this;
065        }
066    
067        public ThreadPoolBuilder rejectedPolicy(ThreadPoolRejectedPolicy rejectedPolicy) {
068            threadPoolDefinition.rejectedPolicy(rejectedPolicy);
069            return this;
070        }
071    
072        /**
073         * Builds the new thread pool
074         *
075         * @param name name which is appended to the thread name
076         * @return the created thread pool
077         */
078        public ExecutorService build(String name) {
079            return build(null, name);
080        }
081    
082        /**
083         * Builds the new thread pool
084         *
085         * @param source the source object, usually it should be <tt>this</tt> passed in as parameter
086         * @param name   name which is appended to the thread name
087         * @return the created thread pool
088         */
089        public ExecutorService build(Object source, String name) {
090            ExecutorService answer = camelContext.getExecutorServiceStrategy().newThreadPool(source, name,
091                    threadPoolDefinition.getPoolSize(), threadPoolDefinition.getMaxPoolSize(),
092                    threadPoolDefinition.getKeepAliveTime(), threadPoolDefinition.getTimeUnit(),
093                    threadPoolDefinition.getMaxQueueSize(), threadPoolDefinition.getRejectedExecutionHandler(), false);
094    
095            return answer;
096        }
097    
098    }