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.ThreadPoolRejectedPolicy; 025import org.apache.camel.spi.ThreadPoolProfile; 026 027/** 028 * A builder to create thread pools. 029 * 030 * @version 031 */ 032public final class ThreadPoolBuilder { 033 034 // reuse a profile to store the settings 035 private final ThreadPoolProfile profile; 036 private final CamelContext context; 037 038 public ThreadPoolBuilder(CamelContext context) { 039 this.context = context; 040 this.profile = new ThreadPoolProfile(); 041 } 042 043 public ThreadPoolBuilder poolSize(int poolSize) { 044 profile.setPoolSize(poolSize); 045 return this; 046 } 047 048 public ThreadPoolBuilder maxPoolSize(int maxPoolSize) { 049 profile.setMaxPoolSize(maxPoolSize); 050 return this; 051 } 052 053 public ThreadPoolBuilder keepAliveTime(long keepAliveTime, TimeUnit timeUnit) { 054 profile.setKeepAliveTime(keepAliveTime); 055 profile.setTimeUnit(timeUnit); 056 return this; 057 } 058 059 public ThreadPoolBuilder keepAliveTime(long keepAliveTime) { 060 profile.setKeepAliveTime(keepAliveTime); 061 return this; 062 } 063 064 public ThreadPoolBuilder maxQueueSize(int maxQueueSize) { 065 profile.setMaxQueueSize(maxQueueSize); 066 return this; 067 } 068 069 public ThreadPoolBuilder rejectedPolicy(ThreadPoolRejectedPolicy rejectedPolicy) { 070 profile.setRejectedPolicy(rejectedPolicy); 071 return this; 072 } 073 074 /** 075 * Builds the new thread pool 076 * 077 * @return the created thread pool 078 * @throws Exception is thrown if error building the thread pool 079 */ 080 public ExecutorService build() throws Exception { 081 return build(null, null); 082 } 083 084 /** 085 * Builds the new thread pool 086 * 087 * @param name name which is appended to the thread name 088 * @return the created thread pool 089 * @throws Exception is thrown if error building the thread pool 090 */ 091 public ExecutorService build(String name) throws Exception { 092 return build(null, name); 093 } 094 095 /** 096 * Builds the new thread pool 097 * 098 * @param source the source object, usually it should be <tt>this</tt> 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, null); 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> 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}