View Javadoc

1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  package org.apache.http.pool;
28  
29  import org.apache.http.annotation.ThreadingBehavior;
30  import org.apache.http.annotation.Contract;
31  
32  import java.io.Serializable;
33  
34  /**
35   * Pool statistics.
36   * <p>
37   * The total number of connections in the pool is equal to {@code available} plus {@code leased}.
38   * </p>
39   *
40   * @since 4.2
41   */
42  @Contract(threading = ThreadingBehavior.IMMUTABLE)
43  public class PoolStats implements Serializable {
44  
45      private static final long serialVersionUID = -2807686144795228544L;
46  
47      private final int leased;
48      private final int pending;
49      private final int available;
50      private final int max;
51  
52      public PoolStats(final int leased, final int pending, final int free, final int max) {
53          super();
54          this.leased = leased;
55          this.pending = pending;
56          this.available = free;
57          this.max = max;
58      }
59  
60      /**
61       * Gets the number of persistent connections tracked by the connection manager currently being used to execute
62       * requests.
63       * <p>
64       * The total number of connections in the pool is equal to {@code available} plus {@code leased}.
65       * </p>
66       *
67       * @return the number of persistent connections.
68       */
69      public int getLeased() {
70          return this.leased;
71      }
72  
73      /**
74       * Gets the number of connection requests being blocked awaiting a free connection. This can happen only if there
75       * are more worker threads contending for fewer connections.
76       *
77       * @return the number of connection requests being blocked awaiting a free connection.
78       */
79      public int getPending() {
80          return this.pending;
81      }
82  
83      /**
84       * Gets the number idle persistent connections.
85       * <p>
86       * The total number of connections in the pool is equal to {@code available} plus {@code leased}.
87       * </p>
88       *
89       * @return number idle persistent connections.
90       */
91      public int getAvailable() {
92          return this.available;
93      }
94  
95      /**
96       * Gets the maximum number of allowed persistent connections.
97       *
98       * @return the maximum number of allowed persistent connections.
99       */
100     public int getMax() {
101         return this.max;
102     }
103 
104     @Override
105     public String toString() {
106         final StringBuilder buffer = new StringBuilder();
107         buffer.append("[leased: ");
108         buffer.append(this.leased);
109         buffer.append("; pending: ");
110         buffer.append(this.pending);
111         buffer.append("; available: ");
112         buffer.append(this.available);
113         buffer.append("; max: ");
114         buffer.append(this.max);
115         buffer.append("]");
116         return buffer.toString();
117     }
118 
119 }