01: /**
02: * Objective Database Abstraction Layer (ODAL)
03: * Copyright (c) 2004, The ODAL Development Group
04: * All rights reserved.
05: * For definition of the ODAL Development Group please refer to LICENCE.txt file
06: *
07: * Distributable under LGPL license.
08: * See terms of license at gnu.org.
09: */package com.completex.objective.components.pool.impl;
10:
11: import com.completex.objective.components.log.Log;
12:
13: /**
14: * @author Gennady Krizhevsky
15: */
16: public class ShrinkPolicyImpl implements ShrinkPolicy {
17: private int shrinkSize;
18: private long shrinkTimeInterval;
19: private long endOfTimeInterval;
20: private long numberOfChecks;
21: private double cummulIdleSize;
22: private Log log;
23:
24: public ShrinkPolicyImpl(int shrinkSize, long shrinkTimeInterval) {
25: this .shrinkSize = shrinkSize;
26: this .shrinkTimeInterval = shrinkTimeInterval;
27: reset();
28: }
29:
30: public void setLog(Log log) {
31: this .log = log;
32: }
33:
34: public boolean shouldShrink(int idleSize) {
35: boolean rc = false;
36: numberOfChecks++;
37: cummulIdleSize += idleSize;
38: double averagelIdleSize = cummulIdleSize / numberOfChecks;
39: long currRelativeTime = System.currentTimeMillis();
40:
41: if (log != null && log.isTraceEnabled()) {
42: log.trace("" + "shouldShrink: numberOfChecks "
43: + numberOfChecks + "; idleSize " + idleSize
44: + "; averagelIdleSize " + averagelIdleSize
45: + "; shrinkSize " + shrinkSize
46: + "; currRelativeTime " + currRelativeTime
47: + "; endOfTimeInterval " + endOfTimeInterval);
48: }
49:
50: if (currRelativeTime >= endOfTimeInterval) {
51: if (log != null && log.isTraceEnabled()) {
52: log.trace("averagelIdleSize > shrinkSize");
53: }
54: if (averagelIdleSize > shrinkSize) {
55: if (log != null && log.isDebugEnabled()) {
56: log
57: .debug("averagelIdleSize > shrinkSize : shouldShrink returns true");
58: }
59: rc = true;
60: }
61: reset();
62: }
63:
64: return rc;
65: }
66:
67: public void reset() {
68: if (log != null && log.isTraceEnabled()) {
69: log.trace("-- reset pool");
70: }
71: long beginOfTimeInterval = System.currentTimeMillis();
72: endOfTimeInterval = beginOfTimeInterval + shrinkTimeInterval;
73: numberOfChecks = 0L;
74: cummulIdleSize = 0;
75: }
76: }
|