KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > util > ConcurrencyThrottleSupport


1 /*
2  * Copyright 2002-2007 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.util;
18
19 import java.io.IOException JavaDoc;
20 import java.io.ObjectInputStream JavaDoc;
21 import java.io.Serializable JavaDoc;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25
26 /**
27  * Support class for throttling concurrent access to a specific resource.
28  *
29  * <p>Designed for use as a base class, with the subclass invoking
30  * the {@link #beforeAccess()} and {@link #afterAccess()} methods at
31  * appropriate points of its workflow. Note that <code>afterAccess</code>
32  * should usually be called in a finally block!
33  *
34  * <p>The default concurrency limit of this support class is -1
35  * ("unbounded concurrency"). Subclasses may override this default;
36  * check the javadoc of the concrete class that you're using.
37  *
38  * @author Juergen Hoeller
39  * @since 1.2.5
40  * @see #setConcurrencyLimit
41  * @see #beforeAccess()
42  * @see #afterAccess()
43  * @see org.springframework.aop.interceptor.ConcurrencyThrottleInterceptor
44  * @see java.io.Serializable
45  */

46 public abstract class ConcurrencyThrottleSupport implements Serializable JavaDoc {
47
48     /**
49      * Permit any number of concurrent invocations: that is, don't throttle concurrency.
50      */

51     public static final int UNBOUNDED_CONCURRENCY = -1;
52
53     /**
54      * Switch concurrency 'off': that is, don't allow any concurrent invocations.
55      */

56     public static final int NO_CONCURRENCY = 0;
57     
58
59     /** Transient to optimize serialization */
60     protected transient Log logger = LogFactory.getLog(getClass());
61
62     private transient Object JavaDoc monitor = new Object JavaDoc();
63
64     private int concurrencyLimit = UNBOUNDED_CONCURRENCY;
65
66     private int concurrencyCount = 0;
67
68
69     /**
70      * Set the maximum number of concurrent access attempts allowed.
71      * -1 indicates unbounded concurrency.
72      * <p>In principle, this limit can be changed at runtime,
73      * although it is generally designed as a config time setting.
74      * <p>NOTE: Do not switch between -1 and any concrete limit at runtime,
75      * as this will lead to inconsistent concurrency counts: A limit
76      * of -1 effectively turns off concurrency counting completely.
77      */

78     public void setConcurrencyLimit(int concurrencyLimit) {
79         this.concurrencyLimit = concurrencyLimit;
80     }
81
82     /**
83      * Return the maximum number of concurrent access attempts allowed.
84      */

85     public int getConcurrencyLimit() {
86         return this.concurrencyLimit;
87     }
88
89     /**
90      * Return whether this throttle is currently active.
91      * @return <code>true</code> if the concurrency limit for this instance is active
92      * @see #getConcurrencyLimit()
93      */

94     public boolean isThrottleActive() {
95         return (this.concurrencyLimit > 0);
96     }
97
98
99     /**
100      * To be invoked before the main execution logic of concrete subclasses.
101      * <p>This implementation applies the concurrency throttle.
102      * @see #afterAccess()
103      */

104     protected void beforeAccess() {
105         if (this.concurrencyLimit == NO_CONCURRENCY) {
106             throw new IllegalStateException JavaDoc(
107                     "Currently no invocations allowed - concurrency limit set to NO_CONCURRENCY");
108         }
109         if (this.concurrencyLimit > 0) {
110             boolean debug = logger.isDebugEnabled();
111             synchronized (this.monitor) {
112                 while (this.concurrencyCount >= this.concurrencyLimit) {
113                     if (debug) {
114                         logger.debug("Concurrency count " + this.concurrencyCount +
115                                 " has reached limit " + this.concurrencyLimit + " - blocking");
116                     }
117                     try {
118                         this.monitor.wait();
119                     }
120                     catch (InterruptedException JavaDoc ex) {
121                         // Re-interrupt current thread, to allow other threads to react.
122
Thread.currentThread().interrupt();
123                     }
124                 }
125                 if (debug) {
126                     logger.debug("Entering throttle at concurrency count " + this.concurrencyCount);
127                 }
128                 this.concurrencyCount++;
129             }
130         }
131     }
132
133     /**
134      * To be invoked after the main execution logic of concrete subclasses.
135      * @see #beforeAccess()
136      */

137     protected void afterAccess() {
138         if (this.concurrencyLimit >= 0) {
139             synchronized (this.monitor) {
140                 this.concurrencyCount--;
141                 if (logger.isDebugEnabled()) {
142                     logger.debug("Returning from throttle at concurrency count " + this.concurrencyCount);
143                 }
144                 this.monitor.notify();
145             }
146         }
147     }
148
149
150     //---------------------------------------------------------------------
151
// Serialization support
152
//---------------------------------------------------------------------
153

154     private void readObject(ObjectInputStream JavaDoc ois) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
155         // Rely on default serialization, just initialize state after deserialization.
156
ois.defaultReadObject();
157
158         // Initialize transient fields.
159
this.logger = LogFactory.getLog(getClass());
160         this.monitor = new Object JavaDoc();
161     }
162
163 }
164
Popular Tags