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