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();
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 public void setDestroyLatency(final long destroyLatency) {
81 this.destroyLatency = destroyLatency;
82 }
83
84
85
86
87 public void setPassivateLatency(final long passivateLatency) {
88 this.passivateLatency = passivateLatency;
89 }
90
91
92
93
94 public void setValidateLatency(final long validateLatency) {
95 this.validateLatency = validateLatency;
96 }
97
98 @Override
99 public boolean validateObject(final PooledObject<AtomicInteger> instance) {
100 Waiter.sleepQuietly(validateLatency);
101 return instance.getObject().intValue() == 1;
102 }
103
104 @Override
105 public PooledObject<AtomicInteger> wrap(final AtomicInteger integer) {
106 return new DefaultPooledObject<>(integer);
107 }
108 }