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.ExecutorService; 020import java.util.concurrent.TimeUnit; 021 022import javax.xml.bind.annotation.XmlAccessType; 023import javax.xml.bind.annotation.XmlAccessorType; 024import javax.xml.bind.annotation.XmlAttribute; 025import javax.xml.bind.annotation.XmlRootElement; 026import javax.xml.bind.annotation.XmlTransient; 027import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; 028 029import org.apache.camel.spi.Metadata; 030import org.apache.camel.util.concurrent.ThreadPoolRejectedPolicy; 031 032/** 033 * Specifies that all steps after this node are processed asynchronously 034 */ 035@Metadata(label = "eip,routing") 036@XmlRootElement(name = "threads") 037@XmlAccessorType(XmlAccessType.FIELD) 038public class ThreadsDefinition extends NoOutputDefinition<ThreadsDefinition> implements ExecutorServiceAwareDefinition<ThreadsDefinition> { 039 040 @XmlTransient 041 private ExecutorService executorService; 042 @XmlAttribute 043 private String executorServiceRef; 044 @XmlAttribute 045 private Integer poolSize; 046 @XmlAttribute 047 private Integer maxPoolSize; 048 @XmlAttribute 049 private Long keepAliveTime; 050 @XmlAttribute 051 @XmlJavaTypeAdapter(TimeUnitAdapter.class) 052 private TimeUnit timeUnit; 053 @XmlAttribute 054 private Integer maxQueueSize; 055 @XmlAttribute 056 private Boolean allowCoreThreadTimeOut; 057 @XmlAttribute 058 @Metadata(defaultValue = "Threads") 059 private String threadName; 060 @XmlAttribute 061 private ThreadPoolRejectedPolicy rejectedPolicy; 062 @XmlAttribute 063 @Metadata(defaultValue = "true") 064 private Boolean callerRunsWhenRejected; 065 066 public ThreadsDefinition() { 067 this.threadName = "Threads"; 068 } 069 070 @Override 071 public String getShortName() { 072 return "threads"; 073 } 074 075 @Override 076 public String getLabel() { 077 return "threads"; 078 } 079 080 @Override 081 public String toString() { 082 return "Threads[" + getOutputs() + "]"; 083 } 084 085 /** 086 * To use a custom thread pool 087 */ 088 @Override 089 public ThreadsDefinition executorService(ExecutorService executorService) { 090 setExecutorService(executorService); 091 return this; 092 } 093 094 /** 095 * To refer to a custom thread pool or use a thread pool profile (as 096 * overlay) 097 */ 098 @Override 099 public ThreadsDefinition executorServiceRef(String executorServiceRef) { 100 setExecutorServiceRef(executorServiceRef); 101 return this; 102 } 103 104 /** 105 * Sets the core pool size 106 * 107 * @param poolSize the core pool size to keep minimum in the pool 108 * @return the builder 109 */ 110 public ThreadsDefinition poolSize(int poolSize) { 111 setPoolSize(poolSize); 112 return this; 113 } 114 115 /** 116 * Sets the maximum pool size 117 * 118 * @param maxPoolSize the maximum pool size 119 * @return the builder 120 */ 121 public ThreadsDefinition maxPoolSize(int maxPoolSize) { 122 setMaxPoolSize(maxPoolSize); 123 return this; 124 } 125 126 /** 127 * Sets the keep alive time for idle threads 128 * 129 * @param keepAliveTime keep alive time 130 * @return the builder 131 */ 132 public ThreadsDefinition keepAliveTime(long keepAliveTime) { 133 setKeepAliveTime(keepAliveTime); 134 return this; 135 } 136 137 /** 138 * Sets the keep alive time unit. By default SECONDS is used. 139 * 140 * @param keepAliveTimeUnits time unit 141 * @return the builder 142 */ 143 public ThreadsDefinition timeUnit(TimeUnit keepAliveTimeUnits) { 144 setTimeUnit(keepAliveTimeUnits); 145 return this; 146 } 147 148 /** 149 * Sets the maximum number of tasks in the work queue. 150 * <p/> 151 * Use <tt>-1</tt> or <tt>Integer.MAX_VALUE</tt> for an unbounded queue 152 * 153 * @param maxQueueSize the max queue size 154 * @return the builder 155 */ 156 public ThreadsDefinition maxQueueSize(int maxQueueSize) { 157 setMaxQueueSize(maxQueueSize); 158 return this; 159 } 160 161 /** 162 * Sets the handler for tasks which cannot be executed by the thread pool. 163 * 164 * @param rejectedPolicy the policy for the handler 165 * @return the builder 166 */ 167 public ThreadsDefinition rejectedPolicy(ThreadPoolRejectedPolicy rejectedPolicy) { 168 setRejectedPolicy(rejectedPolicy); 169 return this; 170 } 171 172 /** 173 * Sets the thread name to use. 174 * 175 * @param threadName the thread name 176 * @return the builder 177 */ 178 public ThreadsDefinition threadName(String threadName) { 179 setThreadName(threadName); 180 return this; 181 } 182 183 /** 184 * Whether or not to use as caller runs as <b>fallback</b> when a task is 185 * rejected being added to the thread pool (when its full). This is only 186 * used as fallback if no rejectedPolicy has been configured, or the thread 187 * pool has no configured rejection handler. 188 * <p/> 189 * Is by default <tt>true</tt> 190 * 191 * @param callerRunsWhenRejected whether or not the caller should run 192 * @return the builder 193 */ 194 public ThreadsDefinition callerRunsWhenRejected(boolean callerRunsWhenRejected) { 195 setCallerRunsWhenRejected(callerRunsWhenRejected); 196 return this; 197 } 198 199 /** 200 * Whether idle core threads is allowed to timeout and therefore can shrink 201 * the pool size below the core pool size 202 * <p/> 203 * Is by default <tt>false</tt> 204 * 205 * @param allowCoreThreadTimeOut <tt>true</tt> to allow timeout 206 * @return the builder 207 */ 208 public ThreadsDefinition allowCoreThreadTimeOut(boolean allowCoreThreadTimeOut) { 209 setAllowCoreThreadTimeOut(allowCoreThreadTimeOut); 210 return this; 211 } 212 213 @Override 214 public ExecutorService getExecutorService() { 215 return executorService; 216 } 217 218 @Override 219 public void setExecutorService(ExecutorService executorService) { 220 this.executorService = executorService; 221 } 222 223 @Override 224 public String getExecutorServiceRef() { 225 return executorServiceRef; 226 } 227 228 @Override 229 public void setExecutorServiceRef(String executorServiceRef) { 230 this.executorServiceRef = executorServiceRef; 231 } 232 233 public Integer getPoolSize() { 234 return poolSize; 235 } 236 237 public void setPoolSize(Integer poolSize) { 238 this.poolSize = poolSize; 239 } 240 241 public Integer getMaxPoolSize() { 242 return maxPoolSize; 243 } 244 245 public void setMaxPoolSize(Integer maxPoolSize) { 246 this.maxPoolSize = maxPoolSize; 247 } 248 249 public Long getKeepAliveTime() { 250 return keepAliveTime; 251 } 252 253 public void setKeepAliveTime(Long keepAliveTime) { 254 this.keepAliveTime = keepAliveTime; 255 } 256 257 public TimeUnit getTimeUnit() { 258 return timeUnit; 259 } 260 261 public void setTimeUnit(TimeUnit timeUnit) { 262 this.timeUnit = timeUnit; 263 } 264 265 public Integer getMaxQueueSize() { 266 return maxQueueSize; 267 } 268 269 public void setMaxQueueSize(Integer maxQueueSize) { 270 this.maxQueueSize = maxQueueSize; 271 } 272 273 public String getThreadName() { 274 return threadName; 275 } 276 277 public void setThreadName(String threadName) { 278 this.threadName = threadName; 279 } 280 281 public ThreadPoolRejectedPolicy getRejectedPolicy() { 282 return rejectedPolicy; 283 } 284 285 public void setRejectedPolicy(ThreadPoolRejectedPolicy rejectedPolicy) { 286 this.rejectedPolicy = rejectedPolicy; 287 } 288 289 public Boolean getCallerRunsWhenRejected() { 290 return callerRunsWhenRejected; 291 } 292 293 public void setCallerRunsWhenRejected(Boolean callerRunsWhenRejected) { 294 this.callerRunsWhenRejected = callerRunsWhenRejected; 295 } 296 297 public Boolean getAllowCoreThreadTimeOut() { 298 return allowCoreThreadTimeOut; 299 } 300 301 public void setAllowCoreThreadTimeOut(Boolean allowCoreThreadTimeOut) { 302 this.allowCoreThreadTimeOut = allowCoreThreadTimeOut; 303 } 304}