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.TimeUnit; 020 021import javax.xml.bind.annotation.XmlAccessType; 022import javax.xml.bind.annotation.XmlAccessorType; 023import javax.xml.bind.annotation.XmlAttribute; 024import javax.xml.bind.annotation.XmlRootElement; 025import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; 026 027import org.apache.camel.spi.Metadata; 028import org.apache.camel.util.concurrent.ThreadPoolRejectedPolicy; 029 030/** 031 * To configure thread pools 032 */ 033@Metadata(label = "configuration") 034@XmlRootElement(name = "threadPoolProfile") 035@XmlAccessorType(XmlAccessType.FIELD) 036public class ThreadPoolProfileDefinition extends OptionalIdentifiedDefinition<ThreadPoolProfileDefinition> { 037 @XmlAttribute 038 @Metadata(javaType = "java.lang.Boolean") 039 private String defaultProfile; 040 @XmlAttribute 041 @Metadata(javaType = "java.lang.Integer") 042 private String poolSize; 043 @XmlAttribute 044 @Metadata(javaType = "java.lang.Integer") 045 private String maxPoolSize; 046 @XmlAttribute 047 @Metadata(javaType = "java.lang.Long") 048 private String keepAliveTime; 049 @XmlAttribute 050 @Metadata(javaType = "java.util.concurrent.TimeUnit", 051 enums = "NANOSECONDS,MICROSECONDS,MILLISECONDS,SECONDS,MINUTES,HOURS,DAYS") 052 private String timeUnit; 053 @XmlAttribute 054 @Metadata(javaType = "java.lang.Integer") 055 private String maxQueueSize; 056 @XmlAttribute 057 @Metadata(javaType = "java.lang.Boolean") 058 private String allowCoreThreadTimeOut; 059 @XmlAttribute 060 @Metadata(javaType = "org.apache.camel.util.concurrent.ThreadPoolRejectedPolicy") 061 private String rejectedPolicy; 062 063 public ThreadPoolProfileDefinition() { 064 } 065 066 @Override 067 public String getShortName() { 068 return "threadPoolProfile"; 069 } 070 071 @Override 072 public String getLabel() { 073 return "ThreadPoolProfile " + getId(); 074 } 075 076 public ThreadPoolProfileDefinition poolSize(int poolSize) { 077 return poolSize(Integer.toString(poolSize)); 078 } 079 080 public ThreadPoolProfileDefinition poolSize(String poolSize) { 081 setPoolSize(poolSize); 082 return this; 083 } 084 085 public ThreadPoolProfileDefinition maxPoolSize(int maxPoolSize) { 086 return maxPoolSize(Integer.toString(maxPoolSize)); 087 } 088 089 public ThreadPoolProfileDefinition maxPoolSize(String maxPoolSize) { 090 setMaxPoolSize(maxPoolSize); 091 return this; 092 } 093 094 public ThreadPoolProfileDefinition keepAliveTime(long keepAliveTime) { 095 return keepAliveTime(Long.toString(keepAliveTime)); 096 } 097 098 public ThreadPoolProfileDefinition keepAliveTime(String keepAliveTime) { 099 setKeepAliveTime(keepAliveTime); 100 return this; 101 } 102 103 public ThreadPoolProfileDefinition timeUnit(TimeUnit timeUnit) { 104 return timeUnit(timeUnit.name()); 105 } 106 107 public ThreadPoolProfileDefinition timeUnit(String timeUnit) { 108 setTimeUnit(timeUnit); 109 return this; 110 } 111 112 public ThreadPoolProfileDefinition maxQueueSize(int maxQueueSize) { 113 return maxQueueSize(Integer.toString(maxQueueSize)); 114 } 115 116 public ThreadPoolProfileDefinition maxQueueSize(String maxQueueSize) { 117 setMaxQueueSize(maxQueueSize); 118 return this; 119 } 120 121 public ThreadPoolProfileDefinition rejectedPolicy(ThreadPoolRejectedPolicy rejectedPolicy) { 122 setRejectedPolicy(rejectedPolicy.name()); 123 return this; 124 } 125 126 public ThreadPoolProfileDefinition rejectedPolicy(String rejectedPolicy) { 127 setRejectedPolicy(rejectedPolicy); 128 return this; 129 } 130 131 public ThreadPoolProfileDefinition allowCoreThreadTimeOut(boolean allowCoreThreadTimeOut) { 132 return allowCoreThreadTimeOut(Boolean.toString(allowCoreThreadTimeOut)); 133 } 134 135 public ThreadPoolProfileDefinition allowCoreThreadTimeOut(String allowCoreThreadTimeOut) { 136 setAllowCoreThreadTimeOut(allowCoreThreadTimeOut); 137 return this; 138 } 139 140 public String getDefaultProfile() { 141 return defaultProfile; 142 } 143 144 /** 145 * Whether this profile is the default thread pool profile 146 */ 147 public void setDefaultProfile(String defaultProfile) { 148 this.defaultProfile = defaultProfile; 149 } 150 151 public String getPoolSize() { 152 return poolSize; 153 } 154 155 /** 156 * Sets the core pool size 157 */ 158 public void setPoolSize(String poolSize) { 159 this.poolSize = poolSize; 160 } 161 162 public String getMaxPoolSize() { 163 return maxPoolSize; 164 } 165 166 /** 167 * Sets the maximum pool size 168 */ 169 public void setMaxPoolSize(String maxPoolSize) { 170 this.maxPoolSize = maxPoolSize; 171 } 172 173 public String getKeepAliveTime() { 174 return keepAliveTime; 175 } 176 177 /** 178 * Sets the keep alive time for idle threads in the pool 179 */ 180 public void setKeepAliveTime(String keepAliveTime) { 181 this.keepAliveTime = keepAliveTime; 182 } 183 184 public String getMaxQueueSize() { 185 return maxQueueSize; 186 } 187 188 /** 189 * Sets the maximum number of tasks in the work queue. 190 * <p/> 191 * Use <tt>-1</tt> or <tt>Integer.MAX_VALUE</tt> for an unbounded queue 192 */ 193 public void setMaxQueueSize(String maxQueueSize) { 194 this.maxQueueSize = maxQueueSize; 195 } 196 197 public String getAllowCoreThreadTimeOut() { 198 return allowCoreThreadTimeOut; 199 } 200 201 /** 202 * Whether idle core threads is allowed to timeout and therefore can shrink 203 * the pool size below the core pool size 204 * <p/> 205 * Is by default <tt>false</tt> 206 */ 207 public void setAllowCoreThreadTimeOut(String allowCoreThreadTimeOut) { 208 this.allowCoreThreadTimeOut = allowCoreThreadTimeOut; 209 } 210 211 public String getTimeUnit() { 212 return timeUnit; 213 } 214 215 /** 216 * Sets the time unit to use for keep alive time By default SECONDS is used. 217 */ 218 public void setTimeUnit(String timeUnit) { 219 this.timeUnit = timeUnit; 220 } 221 222 public String getRejectedPolicy() { 223 return rejectedPolicy; 224 } 225 226 /** 227 * Sets the handler for tasks which cannot be executed by the thread pool. 228 */ 229 public void setRejectedPolicy(String rejectedPolicy) { 230 this.rejectedPolicy = rejectedPolicy; 231 } 232 233}