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