01: /*
02: * Copyright 1999-2004 The Apache Software Foundation.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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.apache.commons.pool.performance;
18:
19: import org.apache.commons.pool.PoolableObjectFactory;
20:
21: /**
22: * Sleepy ObjectFactory (everything takes a while longer)
23: *
24: * @author Dirk Verbeeck
25: * @version $Revision: 155430 $ $Date: 2005-02-26 08:13:28 -0500 (Sat, 26 Feb 2005) $
26: */
27: public class SleepingObjectFactory implements PoolableObjectFactory {
28:
29: private int counter = 0;
30: private boolean debug = false;
31:
32: public Object makeObject() throws Exception {
33: Object obj = new Integer(counter++);
34: debug("makeObject", obj);
35: sleep(500);
36: return obj;
37: }
38:
39: public void destroyObject(Object obj) throws Exception {
40: debug("destroyObject", obj);
41: sleep(250);
42: }
43:
44: public boolean validateObject(Object obj) {
45: debug("validateObject", obj);
46: sleep(30);
47: return true;
48: }
49:
50: public void activateObject(Object obj) throws Exception {
51: debug("activateObject", obj);
52: sleep(10);
53: }
54:
55: public void passivateObject(Object obj) throws Exception {
56: debug("passivateObject", obj);
57: sleep(10);
58: }
59:
60: private void debug(String method, Object obj) {
61: if (debug) {
62: String thread = "thread" + Thread.currentThread().getName();
63: System.out.println(thread + ": " + method + " " + obj);
64: }
65: }
66:
67: private void sleep(long millis) {
68: try {
69: Thread.sleep(millis);
70: } catch (InterruptedException e) {
71: }
72: }
73:
74: public boolean isDebug() {
75: return debug;
76: }
77:
78: public void setDebug(boolean b) {
79: debug = b;
80: }
81: }
|