1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.pool.performance;
19
20 import org.apache.commons.pool.PoolableObjectFactory;
21
22
23
24
25
26
27
28 public class SleepingObjectFactory implements PoolableObjectFactory {
29
30 private int counter = 0;
31 private boolean debug = false;
32
33 public Object makeObject() throws Exception {
34 Object obj = new Integer(counter++);
35 debug("makeObject", obj);
36 sleep(500);
37 return obj;
38 }
39
40 public void destroyObject(Object obj) throws Exception {
41 debug("destroyObject", obj);
42 sleep(250);
43 }
44
45 public boolean validateObject(Object obj) {
46 debug("validateObject", obj);
47 sleep(30);
48 return true;
49 }
50
51 public void activateObject(Object obj) throws Exception {
52 debug("activateObject", obj);
53 sleep(10);
54 }
55
56 public void passivateObject(Object obj) throws Exception {
57 debug("passivateObject", obj);
58 sleep(10);
59 }
60
61 private void debug(String method, Object obj) {
62 if (debug) {
63 String thread = "thread" + Thread.currentThread().getName();
64 System.out.println(thread + ": " + method + " " + obj);
65 }
66 }
67
68 private void sleep(long millis) {
69 try {
70 Thread.sleep(millis);
71 }
72 catch (InterruptedException e) {
73 }
74 }
75
76 public boolean isDebug() {
77 return debug;
78 }
79
80 public void setDebug(boolean b) {
81 debug = b;
82 }
83 }