001 package org.apache.jcs.utils.threadpool;
002
003 /*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements. See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership. The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License. You may obtain a copy of the License at
011 *
012 * http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied. See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022
023 /**
024 * This object holds configuration data for a thread pool.
025 * <p>
026 * @author Aaron Smuts
027 */
028 public class PoolConfiguration
029 implements Cloneable
030 {
031 /** Should we bound the queue */
032 private boolean useBoundary = true;
033
034 /** If the queue is bounded, how big can it get */
035 private int boundarySize = 2000;
036
037 /** only has meaning if a boundary is used */
038 private int maximumPoolSize = 150;
039
040 /**
041 * the exact number that will be used in a boundless queue. If the queue has a boundary, more
042 * will be created if the queue fills.
043 */
044 private int minimumPoolSize = 4;
045
046 /** How long idle threads above the minimum should be kept alive. */
047 private int keepAliveTime = 1000 * 60 * 5;
048
049 public enum WhenBlockedPolicy {
050 /** abort when queue is full and max threads is reached. */
051 ABORT,
052
053 /** block when queue is full and max threads is reached. */
054 BLOCK,
055
056 /** run in current thread when queue is full and max threads is reached. */
057 RUN,
058
059 /** wait when queue is full and max threads is reached. */
060 WAIT,
061
062 /** discard oldest when queue is full and max threads is reached. */
063 DISCARDOLDEST
064 }
065
066 /** should be ABORT, BLOCK, RUN, WAIT, DISCARDOLDEST, */
067 private WhenBlockedPolicy whenBlockedPolicy = WhenBlockedPolicy.RUN;
068
069 /** The number of threads to create on startup */
070 private int startUpSize = 4;
071
072 /**
073 * @param useBoundary The useBoundary to set.
074 */
075 public void setUseBoundary( boolean useBoundary )
076 {
077 this.useBoundary = useBoundary;
078 }
079
080 /**
081 * @return Returns the useBoundary.
082 */
083 public boolean isUseBoundary()
084 {
085 return useBoundary;
086 }
087
088 /**
089 * Default
090 */
091 public PoolConfiguration()
092 {
093 // nop
094 }
095
096 /**
097 * Construct a completely configured instance.
098 * <p>
099 * @param useBoundary
100 * @param boundarySize
101 * @param maximumPoolSize
102 * @param minimumPoolSize
103 * @param keepAliveTime
104 * @param whenBlockedPolicy
105 * @param startUpSize
106 */
107 public PoolConfiguration( boolean useBoundary, int boundarySize, int maximumPoolSize, int minimumPoolSize,
108 int keepAliveTime, WhenBlockedPolicy whenBlockedPolicy, int startUpSize )
109 {
110 setUseBoundary( useBoundary );
111 setBoundarySize( boundarySize );
112 setMaximumPoolSize( maximumPoolSize );
113 setMinimumPoolSize( minimumPoolSize );
114 setKeepAliveTime( keepAliveTime );
115 setWhenBlockedPolicy( whenBlockedPolicy );
116 setStartUpSize( startUpSize );
117 }
118
119 /**
120 * @param boundarySize The boundarySize to set.
121 */
122 public void setBoundarySize( int boundarySize )
123 {
124 this.boundarySize = boundarySize;
125 }
126
127 /**
128 * @return Returns the boundarySize.
129 */
130 public int getBoundarySize()
131 {
132 return boundarySize;
133 }
134
135 /**
136 * @param maximumPoolSize The maximumPoolSize to set.
137 */
138 public void setMaximumPoolSize( int maximumPoolSize )
139 {
140 this.maximumPoolSize = maximumPoolSize;
141 }
142
143 /**
144 * @return Returns the maximumPoolSize.
145 */
146 public int getMaximumPoolSize()
147 {
148 return maximumPoolSize;
149 }
150
151 /**
152 * @param minimumPoolSize The minimumPoolSize to set.
153 */
154 public void setMinimumPoolSize( int minimumPoolSize )
155 {
156 this.minimumPoolSize = minimumPoolSize;
157 }
158
159 /**
160 * @return Returns the minimumPoolSize.
161 */
162 public int getMinimumPoolSize()
163 {
164 return minimumPoolSize;
165 }
166
167 /**
168 * @param keepAliveTime The keepAliveTime to set.
169 */
170 public void setKeepAliveTime( int keepAliveTime )
171 {
172 this.keepAliveTime = keepAliveTime;
173 }
174
175 /**
176 * @return Returns the keepAliveTime.
177 */
178 public int getKeepAliveTime()
179 {
180 return keepAliveTime;
181 }
182
183 /**
184 * @param whenBlockedPolicy The whenBlockedPolicy to set.
185 */
186 public void setWhenBlockedPolicy( String whenBlockedPolicy )
187 {
188 if ( whenBlockedPolicy != null )
189 {
190 WhenBlockedPolicy policy = WhenBlockedPolicy.valueOf(whenBlockedPolicy.trim().toUpperCase());
191 setWhenBlockedPolicy(policy);
192 }
193 else
194 {
195 // the value is null, default to RUN
196 this.whenBlockedPolicy = WhenBlockedPolicy.RUN;
197 }
198 }
199
200 /**
201 * @param whenBlockedPolicy The whenBlockedPolicy to set.
202 */
203 public void setWhenBlockedPolicy( WhenBlockedPolicy whenBlockedPolicy )
204 {
205 if ( whenBlockedPolicy != null )
206 {
207 this.whenBlockedPolicy = whenBlockedPolicy;
208 }
209 else
210 {
211 // the value is null, default to RUN
212 this.whenBlockedPolicy = WhenBlockedPolicy.RUN;
213 }
214 }
215
216 /**
217 * @return Returns the whenBlockedPolicy.
218 */
219 public WhenBlockedPolicy getWhenBlockedPolicy()
220 {
221 return whenBlockedPolicy;
222 }
223
224 /**
225 * @param startUpSize The startUpSize to set.
226 */
227 public void setStartUpSize( int startUpSize )
228 {
229 this.startUpSize = startUpSize;
230 }
231
232 /**
233 * @return Returns the startUpSize.
234 */
235 public int getStartUpSize()
236 {
237 return startUpSize;
238 }
239
240 /**
241 * To string for debugging purposes.
242 * @return String
243 */
244 @Override
245 public String toString()
246 {
247 StringBuffer buf = new StringBuffer();
248 buf.append( "useBoundary = [" + isUseBoundary() + "] " );
249 buf.append( "boundarySize = [" + boundarySize + "] " );
250 buf.append( "maximumPoolSize = [" + maximumPoolSize + "] " );
251 buf.append( "minimumPoolSize = [" + minimumPoolSize + "] " );
252 buf.append( "keepAliveTime = [" + keepAliveTime + "] " );
253 buf.append( "whenBlockedPolicy = [" + getWhenBlockedPolicy() + "] " );
254 buf.append( "startUpSize = [" + startUpSize + "]" );
255 return buf.toString();
256 }
257
258 /**
259 * Copies the instance variables to another instance.
260 * <p>
261 * @return PoolConfiguration
262 */
263 @Override
264 public Object clone()
265 {
266 return new PoolConfiguration( isUseBoundary(), boundarySize, maximumPoolSize, minimumPoolSize, keepAliveTime,
267 getWhenBlockedPolicy(), startUpSize );
268 }
269 }