Skip to content

Commit

Permalink
#12 add timeout config (30 seconds) and also make sure we are closing…
Browse files Browse the repository at this point in the history
… off each request by consuming the response.
  • Loading branch information
Steve Swinsburg committed Dec 17, 2019
1 parent 124aaad commit 445c796
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
4 changes: 2 additions & 2 deletions examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>io.github.steveswinsburg</groupId>
<artifactId>xbdd-maven-plugin-example</artifactId>
<version>1.4-SNAPSHOT</version>
<version>1.5-SNAPSHOT</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down Expand Up @@ -55,7 +55,7 @@
<plugin>
<groupId>io.github.steveswinsburg</groupId>
<artifactId>xbdd-maven-plugin</artifactId>
<version>1.4-SNAPSHOT</version>
<version>1.5-SNAPSHOT</version>
<executions>
<execution>
<phase>post-integration-test</phase>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.apache.http.HttpHeaders;
import org.apache.http.auth.AuthenticationException;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
Expand All @@ -27,6 +28,7 @@
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.util.EntityUtils;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
Expand Down Expand Up @@ -197,10 +199,18 @@ protected void upload()
.loadTrustMaterial(null, (x509CertChain, authType) -> true)
.build();

// set a timeout
final int timeout = 30; // seconds
final RequestConfig timeoutConfig = RequestConfig.custom()
.setConnectTimeout(timeout * 1000)
.setConnectionRequestTimeout(timeout * 1000)
.setSocketTimeout(timeout * 1000).build();

final CloseableHttpClient httpClient = HttpClientBuilder
.create()
.setSSLContext(sslContext)
.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
.setDefaultRequestConfig(timeoutConfig)
.build();

final HttpPut request = new HttpPut(url);
Expand Down Expand Up @@ -235,6 +245,10 @@ protected void upload()

final int statusCode = response.getStatusLine().getStatusCode();

// close the content stream.
// Allows us to reuse the same client.
EntityUtils.consumeQuietly(response.getEntity());

// check response
if (statusCode == 200) {
getLog().info(String.format("XBDD upload result: %d %s", response.getStatusLine().getStatusCode(),
Expand Down

0 comments on commit 445c796

Please sign in to comment.