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        // cannot use null name
080        return build(null, "ThreadPool");
081    }
082
083    /**
084     * Builds the new thread pool
085     *
086     * @param name name which is appended to the thread name
087     * @return the created thread pool
088     * @throws Exception is thrown if error building the thread pool
089     */
090    public ExecutorService build(String name) throws Exception {
091        return build(null, name);
092    }
093
094    /**
095     * Builds the new thread pool
096     *
097     * @param source the source object, usually it should be <tt>this</tt>
098     *            passed in as parameter
099     * @param name name which is appended to the thread name
100     * @return the created thread pool
101     * @throws Exception is thrown if error building the thread pool
102     */
103    public ExecutorService build(Object source, String name) throws Exception {
104        return context.getExecutorServiceManager().newThreadPool(source, name, profile);
105    }
106
107    /**
108     * Builds the new scheduled thread pool
109     *
110     * @return the created scheduled thread pool
111     * @throws Exception is thrown if error building the scheduled thread pool
112     */
113    public ScheduledExecutorService buildScheduled() throws Exception {
114        return buildScheduled(null, "ScheduledThreadPool");
115    }
116
117    /**
118     * Builds the new scheduled thread pool
119     *
120     * @param name name which is appended to the thread name
121     * @return the created scheduled thread pool
122     * @throws Exception is thrown if error building the scheduled thread pool
123     */
124    public ScheduledExecutorService buildScheduled(String name) throws Exception {
125        return buildScheduled(null, name);
126    }
127
128    /**
129     * Builds the new scheduled thread pool
130     *
131     * @param source the source object, usually it should be <tt>this</tt>
132     *            passed in as parameter
133     * @param name name which is appended to the thread name
134     * @return the created scheduled thread pool
135     * @throws Exception is thrown if error building the scheduled thread pool
136     */
137    public ScheduledExecutorService buildScheduled(Object source, String name) throws Exception {
138        return context.getExecutorServiceManager().newScheduledThreadPool(source, name, profile);
139    }
140
141}