Skip to content

Commit

Permalink
HttpComponentsHttpInvokerRequestExecutor explicitly releases connecti…
Browse files Browse the repository at this point in the history
…on on HttpComponents 4.2

Issue: SPR-9833
  • Loading branch information
jhoeller committed Oct 10, 2012
1 parent e701197 commit 6ed589d
Showing 1 changed file with 19 additions and 5 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2011 the original author or authors.
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,6 +19,7 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.zip.GZIPInputStream;

import org.apache.http.Header;
Expand All @@ -40,6 +41,8 @@
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.remoting.support.RemoteInvocationResult;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils;

/**
Expand All @@ -63,6 +66,10 @@ public class HttpComponentsHttpInvokerRequestExecutor extends AbstractHttpInvoke

private static final int DEFAULT_READ_TIMEOUT_MILLISECONDS = (60 * 1000);

// Check whether the HttpComponents 4.2 releaseConnection method is available.
private static final Method releaseConnectionMethod =
ClassUtils.getMethodIfAvailable(HttpPost.class, "releaseConnection");

private HttpClient httpClient;


Expand Down Expand Up @@ -146,10 +153,17 @@ protected RemoteInvocationResult doExecuteRequest(

HttpPost postMethod = createHttpPost(config);
setRequestBody(config, postMethod, baos);
HttpResponse response = executeHttpPost(config, getHttpClient(), postMethod);
validateResponse(config, response);
InputStream responseBody = getResponseBody(config, response);
return readRemoteInvocationResult(responseBody, config.getCodebaseUrl());
try {
HttpResponse response = executeHttpPost(config, getHttpClient(), postMethod);
validateResponse(config, response);
InputStream responseBody = getResponseBody(config, response);
return readRemoteInvocationResult(responseBody, config.getCodebaseUrl());
}
finally {
if (releaseConnectionMethod != null){
ReflectionUtils.invokeMethod(releaseConnectionMethod, postMethod);
}
}
}

/**
Expand Down

0 comments on commit 6ed589d

Please sign in to comment.