View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.pool.impl;
19  
20  import java.util.Timer;
21  import java.util.TimerTask;
22  
23  /**
24   * <p>
25   * Provides a shared idle object eviction timer for all pools. This class wraps
26   * the standard {@link Timer} and keeps track of how many pools are using it.
27   * If no pools are using the timer, it is canceled. This prevents a thread
28   * being left running which, in application server environments, can lead to
29   * memory leads and/or prevent applications from shutting down or reloading
30   * cleanly.
31   * </p>
32   * <p>
33   * This class has package scope to prevent its inclusion in the pool public API.
34   * The class declaration below should *not* be changed to public.
35   * </p>
36   */
37  class EvictionTimer {
38      
39      /** Timer instance */
40      private static Timer _timer;
41      
42      /** Static usage count tracker */
43      private static int _usageCount;
44  
45      /** Prevent instantiation */
46      private EvictionTimer() {
47          // Hide the default constuctor
48      }
49  
50      /**
51       * Add the specified eviction task to the timer. Tasks that are added with a
52       * call to this method *must* call {@link #cancel(TimerTask)} to cancel the
53       * task to prevent memory and/or thread leaks in application server
54       * environments.
55       * @param task      Task to be scheduled
56       * @param delay     Delay in milliseconds before task is executed
57       * @param period    Time in milliseconds between executions
58       */
59      static synchronized void schedule(TimerTask task, long delay, long period) {
60          if (null == _timer) {
61              _timer = new Timer(true);
62          }
63          _usageCount++;
64          _timer.schedule(task, delay, period);
65      }
66  
67      /**
68       * Remove the specified eviction task from the timer.
69       * @param task      Task to be scheduled
70       */
71      static synchronized void cancel(TimerTask task) {
72          task.cancel();
73          _usageCount--;
74          if (_usageCount == 0) {
75              _timer.cancel();
76              _timer = null;
77          }
78      }
79  }