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  
28  package org.apache.hc.client5.http.config;
29  
30  import java.util.concurrent.TimeUnit;
31  
32  import org.apache.hc.core5.annotation.Contract;
33  import org.apache.hc.core5.annotation.ThreadingBehavior;
34  import org.apache.hc.core5.util.TimeValue;
35  import org.apache.hc.core5.util.Timeout;
36  
37  /**
38   * Immutable class encapsulating connection initialization and management settings.
39   *
40   * @since 5.2
41   */
42  @Contract(threading = ThreadingBehavior.IMMUTABLE)
43  public class ConnectionConfig implements Cloneable {
44  
45      private static final Timeout DEFAULT_CONNECT_TIMEOUT = Timeout.ofMinutes(3);
46  
47      public static final ConnectionConfig DEFAULT = new Builder().build();
48  
49      private final Timeout connectTimeout;
50      private final Timeout socketTimeout;
51      private final TimeValue validateAfterInactivity;
52      private final TimeValue timeToLive;
53  
54      /**
55       * Intended for CDI compatibility
56       */
57      protected ConnectionConfig() {
58          this(DEFAULT_CONNECT_TIMEOUT, null, null, null);
59      }
60  
61      ConnectionConfig(
62              final Timeout connectTimeout,
63              final Timeout socketTimeout,
64              final TimeValue validateAfterInactivity,
65              final TimeValue timeToLive) {
66          super();
67          this.connectTimeout = connectTimeout;
68          this.socketTimeout = socketTimeout;
69          this.validateAfterInactivity = validateAfterInactivity;
70          this.timeToLive = timeToLive;
71      }
72  
73      /**
74       * @see Builder#setSocketTimeout(Timeout)
75       */
76      public Timeout getSocketTimeout() {
77          return socketTimeout;
78      }
79  
80      /**
81       * @see Builder#setConnectTimeout(Timeout)
82       */
83      public Timeout getConnectTimeout() {
84          return connectTimeout;
85      }
86  
87      /**
88       * @see Builder#setValidateAfterInactivity(TimeValue)
89       */
90      public TimeValue getValidateAfterInactivity() {
91          return validateAfterInactivity;
92      }
93  
94      /**
95       * @see Builder#setTimeToLive(TimeValue) (TimeValue)
96       */
97      public TimeValue getTimeToLive() {
98          return timeToLive;
99      }
100 
101     @Override
102     protected ConnectionConfig clone() throws CloneNotSupportedException {
103         return (ConnectionConfig) super.clone();
104     }
105 
106     @Override
107     public String toString() {
108         final StringBuilder builder = new StringBuilder();
109         builder.append("[");
110         builder.append("connectTimeout=").append(connectTimeout);
111         builder.append(", socketTimeout=").append(socketTimeout);
112         builder.append(", validateAfterInactivity=").append(validateAfterInactivity);
113         builder.append(", timeToLive=").append(timeToLive);
114         builder.append("]");
115         return builder.toString();
116     }
117 
118     public static ConnectionConfig.Builder custom() {
119         return new Builder();
120     }
121 
122     public static ConnectionConfig.Builder copy(final ConnectionConfig config) {
123         return new Builder()
124                 .setConnectTimeout(config.getConnectTimeout())
125                 .setSocketTimeout(config.getSocketTimeout())
126                 .setValidateAfterInactivity(config.getValidateAfterInactivity())
127                 .setTimeToLive(config.getTimeToLive());
128     }
129 
130     public static class Builder {
131 
132         private Timeout socketTimeout;
133         private Timeout connectTimeout;
134         private TimeValue validateAfterInactivity;
135         private TimeValue timeToLive;
136 
137         Builder() {
138             super();
139             this.connectTimeout = DEFAULT_CONNECT_TIMEOUT;
140         }
141 
142         /**
143          * @return this instance.
144          * @see #setSocketTimeout(Timeout)
145          */
146         public Builder setSocketTimeout(final int soTimeout, final TimeUnit timeUnit) {
147             this.socketTimeout = Timeout.of(soTimeout, timeUnit);
148             return this;
149         }
150 
151         /**
152          * Determines the default socket timeout value for I/O operations on
153          * connections created by this configuration.
154          * A timeout value of zero is interpreted as an infinite timeout.
155          * <p>
156          * This value acts as a baseline at the connection management layer.
157          * This parameter overrides the socket timeout setting applied at the I/O layer
158          * and in its tuen can overridden by settings applied at the protocol layer
159          * for the duration of a message exchange.
160          * </p>
161          * <p>
162          * Default: {@code null} (undefined)
163          * </p>
164          *
165          * @return this instance.
166          */
167         public Builder setSocketTimeout(final Timeout soTimeout) {
168             this.socketTimeout = soTimeout;
169             return this;
170         }
171 
172         /**
173          * Determines the timeout until a new connection is fully established.
174          * <p>
175          * A timeout value of zero is interpreted as an infinite timeout.
176          * </p>
177          * <p>
178          * Default: 3 minutes
179          * </p>
180          *
181          * @return this instance.
182          */
183         public Builder setConnectTimeout(final Timeout connectTimeout) {
184             this.connectTimeout = connectTimeout;
185             return this;
186         }
187 
188         /**
189          * @return this instance.
190          * @see #setConnectTimeout(Timeout)
191          */
192         public Builder setConnectTimeout(final long connectTimeout, final TimeUnit timeUnit) {
193             this.connectTimeout = Timeout.of(connectTimeout, timeUnit);
194             return this;
195         }
196 
197         /**
198          * Defines period of inactivity after which persistent connections must
199          * be re-validated prior to being leased to the consumer. Negative values passed
200          * to this method disable connection validation.
201          * <p>
202          * Default: {@code null} (undefined)
203          * </p>
204          *
205          * @return this instance.
206          */
207         public Builder setValidateAfterInactivity(final TimeValue validateAfterInactivity) {
208             this.validateAfterInactivity = validateAfterInactivity;
209             return this;
210         }
211 
212         /**
213          * @return this instance.
214          * @see #setValidateAfterInactivity(TimeValue)
215          */
216         public Builder setValidateAfterInactivity(final long validateAfterInactivity, final TimeUnit timeUnit) {
217             this.validateAfterInactivity = TimeValue.of(validateAfterInactivity, timeUnit);
218             return this;
219         }
220 
221         /**
222          * Defines the total span of time connections can be kept alive or execute requests.
223          * <p>
224          * Default: {@code null} (undefined)
225          * </p>
226          *
227          * @return this instance.
228          */
229         public Builder setTimeToLive(final TimeValue timeToLive) {
230             this.timeToLive = timeToLive;
231             return this;
232         }
233 
234         /**
235          * @return this instance.
236          * @see #setTimeToLive(TimeValue)
237          */
238         public Builder setTimeToLive(final long timeToLive, final TimeUnit timeUnit) {
239             this.timeToLive = TimeValue.of(timeToLive, timeUnit);
240             return this;
241         }
242 
243         public ConnectionConfig build() {
244             return new ConnectionConfig(
245                     connectTimeout != null ? connectTimeout : DEFAULT_CONNECT_TIMEOUT,
246                     socketTimeout,
247                     validateAfterInactivity,
248                     timeToLive);
249         }
250 
251     }
252 
253 }