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; 19 20 /** 21 * <p>Object created by {@link WaiterFactory}. Maintains active / valid state, 22 * last passivated and idle times. Waits with configurable latency when 23 * {@link #doWait()} method is called.</p> 24 * 25 * <p>This class is *not* threadsafe.</p> 26 */ 27 public class Waiter { 28 private boolean active = false; 29 private boolean valid = true; 30 private long latency = 0; 31 private long lastPassivated = 0; 32 private long lastIdleTimeMs = 0; 33 34 public Waiter(boolean active, boolean valid, long latency) { 35 this.active = active; 36 this.valid = valid; 37 this.latency = latency; 38 this.lastPassivated = System.currentTimeMillis(); 39 } 40 41 /** 42 * Wait for {@link #getLatency()} ms. 43 */ 44 public void doWait() { 45 try { 46 Thread.sleep(latency); 47 } catch (InterruptedException ex) { 48 // ignore 49 } 50 } 51 52 /** 53 * Whether or not the instance is active. 54 * 55 * @return true if the last lifecycle event for this instance was activation. 56 */ 57 public boolean isActive() { 58 return active; 59 } 60 61 /** 62 * <p>Sets the active state and updates {@link #getLastIdleTimeMs() lastIdleTime} 63 * or {@link #getLastPassivated() lastPassivated} as appropriate.</p> 64 * 65 * <p>If the active state is changing from inactive to active, lastIdleTime 66 * is updated with the current time minus lastPassivated. If the state is 67 * changing from active to inactive, lastPassivated is updated with the 68 * current time.</p> 69 * 70 * <p>{@link WaiterFactory#activateObject(Object)} and 71 * {@link WaiterFactory#passivateObject(Object)} invoke this method on their 72 * actual parameter, passing <code>true</code> and <code>false</code>, 73 * respectively.</p> 74 * 75 * @param active new active state 76 */ 77 public void setActive(boolean active) { 78 final boolean activeState = this.active; 79 if (activeState == active) { 80 return; 81 } 82 this.active = active; 83 final long currentTime = System.currentTimeMillis(); 84 if (active) { // activating 85 lastIdleTimeMs = currentTime - lastPassivated; 86 } else { // passivating 87 lastPassivated = currentTime; 88 } 89 } 90 91 public long getLatency() { 92 return latency; 93 } 94 95 public void setLatency(long latency) { 96 this.latency = latency; 97 } 98 99 public boolean isValid() { 100 return valid; 101 } 102 103 public void setValid(boolean valid) { 104 this.valid = valid; 105 } 106 107 /** 108 * <p>Returns the system time of this instance's last passivation.</p> 109 * 110 * <p>When an instance is created, this field is initialized to the system time.</p> 111 * 112 * @return time of last passivation 113 */ 114 public long getLastPassivated() { 115 return lastPassivated; 116 } 117 118 /** 119 * <p>Returns the last idle time for this instance in ms.</p> 120 * 121 * <p>When an instance is created, and each subsequent time it is passivated, 122 * the {@link #getLastPassivated() lastPassivated} property is updated with the 123 * current time. When the next activation occurs, <code>lastIdleTime</code> is 124 * updated with the elapsed time since passivation.<p> 125 * 126 * @return last idle time 127 */ 128 public long getLastIdleTimeMs() { 129 return lastIdleTimeMs; 130 } 131 132 }