001package org.apache.commons.jcs3.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 * This object holds configuration data for a thread pool.
024 */
025public final class PoolConfiguration
026    implements Cloneable
027{
028    /**
029     * DEFAULT SETTINGS
030     */
031    private static final boolean DEFAULT_USE_BOUNDARY = true;
032
033    /** Default queue size limit */
034    private static final int DEFAULT_BOUNDARY_SIZE = 2000;
035
036    /** Default max size */
037    private static final int DEFAULT_MAXIMUM_POOL_SIZE = 150;
038
039    /** Default min */
040    private static final int DEFAULT_MINIMUM_POOL_SIZE = Runtime.getRuntime().availableProcessors();
041
042    /** Default keep alive */
043    private static final int DEFAULT_KEEPALIVE_TIME = 1000 * 60 * 5;
044
045    /** Default when blocked */
046    private static final WhenBlockedPolicy DEFAULT_WHEN_BLOCKED_POLICY = WhenBlockedPolicy.RUN;
047
048    /** Default startup size */
049    private static final int DEFAULT_STARTUP_SIZE = DEFAULT_MINIMUM_POOL_SIZE;
050
051    /** Should we bound the queue */
052    private boolean useBoundary = DEFAULT_USE_BOUNDARY;
053
054    /** If the queue is bounded, how big can it get */
055    private int boundarySize = DEFAULT_BOUNDARY_SIZE;
056
057    /** only has meaning if a boundary is used */
058    private int maximumPoolSize = DEFAULT_MAXIMUM_POOL_SIZE;
059
060    /**
061     * the exact number that will be used in a boundless queue. If the queue has a boundary, more
062     * will be created if the queue fills.
063     */
064    private int minimumPoolSize = DEFAULT_MINIMUM_POOL_SIZE;
065
066    /** How long idle threads above the minimum should be kept alive. */
067    private int keepAliveTime = DEFAULT_KEEPALIVE_TIME;
068
069    public enum WhenBlockedPolicy {
070        /** abort when queue is full and max threads is reached. */
071        ABORT,
072
073        /** block when queue is full and max threads is reached. */
074        BLOCK,
075
076        /** run in current thread when queue is full and max threads is reached. */
077        RUN,
078
079        /** wait when queue is full and max threads is reached. */
080        WAIT,
081
082        /** discard oldest when queue is full and max threads is reached. */
083        DISCARDOLDEST
084    }
085
086    /** should be ABORT, BLOCK, RUN, WAIT, DISCARDOLDEST, */
087    private WhenBlockedPolicy whenBlockedPolicy = DEFAULT_WHEN_BLOCKED_POLICY;
088
089    /** The number of threads to create on startup */
090    private int startUpSize = DEFAULT_MINIMUM_POOL_SIZE;
091
092    /**
093     * @param useBoundary The useBoundary to set.
094     */
095    public void setUseBoundary( final boolean useBoundary )
096    {
097        this.useBoundary = useBoundary;
098    }
099
100    /**
101     * @return Returns the useBoundary.
102     */
103    public boolean isUseBoundary()
104    {
105        return useBoundary;
106    }
107
108    /**
109     * Default
110     */
111    public PoolConfiguration()
112    {
113        this( DEFAULT_USE_BOUNDARY, DEFAULT_BOUNDARY_SIZE, DEFAULT_MAXIMUM_POOL_SIZE,
114              DEFAULT_MINIMUM_POOL_SIZE, DEFAULT_KEEPALIVE_TIME,
115              DEFAULT_WHEN_BLOCKED_POLICY, DEFAULT_STARTUP_SIZE );
116    }
117
118    /**
119     * Construct a completely configured instance.
120     * <p>
121     * @param useBoundary
122     * @param boundarySize
123     * @param maximumPoolSize
124     * @param minimumPoolSize
125     * @param keepAliveTime
126     * @param whenBlockedPolicy
127     * @param startUpSize
128     */
129    public PoolConfiguration( final boolean useBoundary, final int boundarySize, final int maximumPoolSize, final int minimumPoolSize,
130                              final int keepAliveTime, final WhenBlockedPolicy whenBlockedPolicy, final int startUpSize )
131    {
132        setUseBoundary( useBoundary );
133        setBoundarySize( boundarySize );
134        setMaximumPoolSize( maximumPoolSize );
135        setMinimumPoolSize( minimumPoolSize );
136        setKeepAliveTime( keepAliveTime );
137        setWhenBlockedPolicy( whenBlockedPolicy );
138        setStartUpSize( startUpSize );
139    }
140
141    /**
142     * @param boundarySize The boundarySize to set.
143     */
144    public void setBoundarySize( final int boundarySize )
145    {
146        this.boundarySize = boundarySize;
147    }
148
149    /**
150     * @return Returns the boundarySize.
151     */
152    public int getBoundarySize()
153    {
154        return boundarySize;
155    }
156
157    /**
158     * @param maximumPoolSize The maximumPoolSize to set.
159     */
160    public void setMaximumPoolSize( final int maximumPoolSize )
161    {
162        this.maximumPoolSize = maximumPoolSize;
163    }
164
165    /**
166     * @return Returns the maximumPoolSize.
167     */
168    public int getMaximumPoolSize()
169    {
170        return maximumPoolSize;
171    }
172
173    /**
174     * @param minimumPoolSize The minimumPoolSize to set.
175     */
176    public void setMinimumPoolSize( final int minimumPoolSize )
177    {
178        this.minimumPoolSize = minimumPoolSize;
179    }
180
181    /**
182     * @return Returns the minimumPoolSize.
183     */
184    public int getMinimumPoolSize()
185    {
186        return minimumPoolSize;
187    }
188
189    /**
190     * @param keepAliveTime The keepAliveTime to set.
191     */
192    public void setKeepAliveTime( final int keepAliveTime )
193    {
194        this.keepAliveTime = keepAliveTime;
195    }
196
197    /**
198     * @return Returns the keepAliveTime.
199     */
200    public int getKeepAliveTime()
201    {
202        return keepAliveTime;
203    }
204
205    /**
206     * @param whenBlockedPolicy The whenBlockedPolicy to set.
207     */
208    public void setWhenBlockedPolicy( final String whenBlockedPolicy )
209    {
210        if ( whenBlockedPolicy != null )
211        {
212            final WhenBlockedPolicy policy = WhenBlockedPolicy.valueOf(whenBlockedPolicy.trim().toUpperCase());
213            setWhenBlockedPolicy(policy);
214        }
215        else
216        {
217            // the value is null, default to RUN
218            this.whenBlockedPolicy = WhenBlockedPolicy.RUN;
219        }
220    }
221
222    /**
223     * @param whenBlockedPolicy The whenBlockedPolicy to set.
224     */
225    public void setWhenBlockedPolicy( final WhenBlockedPolicy whenBlockedPolicy )
226    {
227        if ( whenBlockedPolicy != null )
228        {
229            this.whenBlockedPolicy = whenBlockedPolicy;
230        }
231        else
232        {
233            // the value is null, default to RUN
234            this.whenBlockedPolicy = WhenBlockedPolicy.RUN;
235        }
236    }
237
238    /**
239     * @return Returns the whenBlockedPolicy.
240     */
241    public WhenBlockedPolicy getWhenBlockedPolicy()
242    {
243        return whenBlockedPolicy;
244    }
245
246    /**
247     * @param startUpSize The startUpSize to set.
248     */
249    public void setStartUpSize( final int startUpSize )
250    {
251        this.startUpSize = startUpSize;
252    }
253
254    /**
255     * @return Returns the startUpSize.
256     */
257    public int getStartUpSize()
258    {
259        return startUpSize;
260    }
261
262    /**
263     * To string for debugging purposes.
264     * @return String
265     */
266    @Override
267    public String toString()
268    {
269        final StringBuilder buf = new StringBuilder();
270        buf.append( "useBoundary = [" + isUseBoundary() + "] " );
271        buf.append( "boundarySize = [" + boundarySize + "] " );
272        buf.append( "maximumPoolSize = [" + maximumPoolSize + "] " );
273        buf.append( "minimumPoolSize = [" + minimumPoolSize + "] " );
274        buf.append( "keepAliveTime = [" + keepAliveTime + "] " );
275        buf.append( "whenBlockedPolicy = [" + getWhenBlockedPolicy() + "] " );
276        buf.append( "startUpSize = [" + startUpSize + "]" );
277        return buf.toString();
278    }
279
280    /**
281     * Copies the instance variables to another instance.
282     * <p>
283     * @return PoolConfiguration
284     */
285    @Override
286    public PoolConfiguration clone()
287    {
288        return new PoolConfiguration( isUseBoundary(), boundarySize, maximumPoolSize, minimumPoolSize, keepAliveTime,
289                                      getWhenBlockedPolicy(), startUpSize );
290    }
291}