1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 }
71 catch (InterruptedException e) {
72 }
73 }
74
75 public boolean isDebug() {
76 return debug;
77 }
78
79 public void setDebug(boolean b) {
80 debug = b;
81 }
82 }