View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * Factory that sources PooledObjects that wrap AtomicIntegers.
27   * {@link #create()} creates an AtomicInteger with value 0, activate increments
28   * the value of the wrapped AtomicInteger and passivate decrements it. Latency
29   * of factory methods is configurable.
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       * @param activateLatency the activateLatency to set
65       */
66      public void setActivateLatency(final long activateLatency) {
67          this.activateLatency = activateLatency;
68      }
69  
70      /**
71       * @param createLatency the createLatency to set
72       */
73      public void setCreateLatency(final long createLatency) {
74          this.createLatency = createLatency;
75      }
76  
77  
78      /**
79       * @param destroyLatency the destroyLatency to set
80       */
81      public void setDestroyLatency(final long destroyLatency) {
82          this.destroyLatency = destroyLatency;
83      }
84  
85  
86      /**
87       * @param passivateLatency the passivateLatency to set
88       */
89      public void setPassivateLatency(final long passivateLatency) {
90          this.passivateLatency = passivateLatency;
91      }
92  
93  
94      /**
95       * @param validateLatency the validateLatency to set
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 }