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.builder;
018
019import java.util.concurrent.ExecutorService;
020import java.util.concurrent.ScheduledExecutorService;
021import java.util.concurrent.TimeUnit;
022
023import org.apache.camel.CamelContext;
024import org.apache.camel.spi.ThreadPoolProfile;
025import org.apache.camel.util.concurrent.ThreadPoolRejectedPolicy;
026
027/**
028 * A builder to create thread pools.
029 */
030public final class ThreadPoolBuilder {
031
032    // reuse a profile to store the settings
033    private final ThreadPoolProfile profile;
034    private final CamelContext context;
035
036    public ThreadPoolBuilder(CamelContext context) {
037        this.context = context;
038        this.profile = new ThreadPoolProfile();
039    }
040
041    public ThreadPoolBuilder poolSize(int poolSize) {
042        profile.setPoolSize(poolSize);
043        return this;
044    }
045
046    public ThreadPoolBuilder maxPoolSize(int maxPoolSize) {
047        profile.setMaxPoolSize(maxPoolSize);
048        return this;
049    }
050
051    public ThreadPoolBuilder keepAliveTime(long keepAliveTime, TimeUnit timeUnit) {
052        profile.setKeepAliveTime(keepAliveTime);
053        profile.setTimeUnit(timeUnit);
054        return this;
055    }
056
057    public ThreadPoolBuilder keepAliveTime(long keepAliveTime) {
058        profile.setKeepAliveTime(keepAliveTime);
059        return this;
060    }
061
062    public ThreadPoolBuilder maxQueueSize(int maxQueueSize) {
063        profile.setMaxQueueSize(maxQueueSize);
064        return this;
065    }
066
067    public ThreadPoolBuilder rejectedPolicy(ThreadPoolRejectedPolicy rejectedPolicy) {
068        profile.setRejectedPolicy(rejectedPolicy);
069        return this;
070    }
071
072    /**
073     * Builds the new thread pool
074     *
075     * @return the created thread pool
076     * @throws Exception is thrown if error building the thread pool
077     */
078    public ExecutorService build() throws Exception {
079        return build(null, null);
080    }
081
082    /**
083     * Builds the new thread pool
084     *
085     * @param name name which is appended to the thread name
086     * @return the created thread pool
087     * @throws Exception is thrown if error building the thread pool
088     */
089    public ExecutorService build(String name) throws Exception {
090        return build(null, name);
091    }
092
093    /**
094     * Builds the new thread pool
095     *
096     * @param source the source object, usually it should be <tt>this</tt>
097     *            passed in as parameter
098     * @param name name which is appended to the thread name
099     * @return the created thread pool
100     * @throws Exception is thrown if error building the thread pool
101     */
102    public ExecutorService build(Object source, String name) throws Exception {
103        return context.getExecutorServiceManager().newThreadPool(source, name, profile);
104    }
105
106    /**
107     * Builds the new scheduled thread pool
108     *
109     * @return the created scheduled thread pool
110     * @throws Exception is thrown if error building the scheduled thread pool
111     */
112    public ScheduledExecutorService buildScheduled() throws Exception {
113        return buildScheduled(null, null);
114    }
115
116    /**
117     * Builds the new scheduled thread pool
118     *
119     * @param name name which is appended to the thread name
120     * @return the created scheduled thread pool
121     * @throws Exception is thrown if error building the scheduled thread pool
122     */
123    public ScheduledExecutorService buildScheduled(String name) throws Exception {
124        return buildScheduled(null, name);
125    }
126
127    /**
128     * Builds the new scheduled thread pool
129     *
130     * @param source the source object, usually it should be <tt>this</tt>
131     *            passed in as parameter
132     * @param name name which is appended to the thread name
133     * @return the created scheduled thread pool
134     * @throws Exception is thrown if error building the scheduled thread pool
135     */
136    public ScheduledExecutorService buildScheduled(Object source, String name) throws Exception {
137        return context.getExecutorServiceManager().newScheduledThreadPool(source, name, profile);
138    }
139
140}