1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.pool2.impl;
18
19 import java.util.concurrent.atomic.AtomicInteger;
20
21 import org.apache.commons.pool2.BasePooledObjectFactory;
22 import org.apache.commons.pool2.PooledObject;
23 import org.apache.commons.pool2.Waiter;
24
25
26
27
28
29
30
31 public class AtomicIntegerFactory
32 extends BasePooledObjectFactory<AtomicInteger> {
33
34 private long activateLatency;
35 private long passivateLatency;
36 private long createLatency;
37 private long destroyLatency;
38 private long validateLatency;
39
40 @Override
41 public void activateObject(final PooledObject<AtomicInteger> p) {
42 p.getObject().incrementAndGet();
43 Waiter.sleepQuietly(activateLatency);
44 }
45
46 @Override
47 public AtomicInteger create() {
48 Waiter.sleepQuietly(createLatency);
49 return new AtomicInteger(0);
50 }
51
52 @Override
53 public void destroyObject(final PooledObject<AtomicInteger> p) {
54 Waiter.sleepQuietly(destroyLatency);
55 }
56
57 @Override
58 public void passivateObject(final PooledObject<AtomicInteger> p) {
59 p.getObject().decrementAndGet();
60 Waiter.sleepQuietly(passivateLatency);
61 }
62
63
64
65
66 public void setActivateLatency(final long activateLatency) {
67 this.activateLatency = activateLatency;
68 }
69
70
71
72
73 public void setCreateLatency(final long createLatency) {
74 this.createLatency = createLatency;
75 }
76
77
78
79
80
81 public void setDestroyLatency(final long destroyLatency) {
82 this.destroyLatency = destroyLatency;
83 }
84
85
86
87
88
89 public void setPassivateLatency(final long passivateLatency) {
90 this.passivateLatency = passivateLatency;
91 }
92
93
94
95
96
97 public void setValidateLatency(final long validateLatency) {
98 this.validateLatency = validateLatency;
99 }
100
101
102 @Override
103 public boolean validateObject(final PooledObject<AtomicInteger> instance) {
104 Waiter.sleepQuietly(validateLatency);
105 return instance.getObject().intValue() == 1;
106 }
107
108
109 @Override
110 public PooledObject<AtomicInteger> wrap(final AtomicInteger integer) {
111 return new DefaultPooledObject<>(integer);
112 }
113 }