Skip to content

Commit 6ed589d

Browse files
committed
HttpComponentsHttpInvokerRequestExecutor explicitly releases connection on HttpComponents 4.2
Issue: SPR-9833
1 parent e701197 commit 6ed589d

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

org.springframework.web/src/main/java/org/springframework/remoting/httpinvoker/HttpComponentsHttpInvokerRequestExecutor.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 the original author or authors.
2+
* Copyright 2002-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@
1919
import java.io.ByteArrayOutputStream;
2020
import java.io.IOException;
2121
import java.io.InputStream;
22+
import java.lang.reflect.Method;
2223
import java.util.zip.GZIPInputStream;
2324

2425
import org.apache.http.Header;
@@ -40,6 +41,8 @@
4041
import org.springframework.context.i18n.LocaleContextHolder;
4142
import org.springframework.remoting.support.RemoteInvocationResult;
4243
import org.springframework.util.Assert;
44+
import org.springframework.util.ClassUtils;
45+
import org.springframework.util.ReflectionUtils;
4346
import org.springframework.util.StringUtils;
4447

4548
/**
@@ -63,6 +66,10 @@ public class HttpComponentsHttpInvokerRequestExecutor extends AbstractHttpInvoke
6366

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

69+
// Check whether the HttpComponents 4.2 releaseConnection method is available.
70+
private static final Method releaseConnectionMethod =
71+
ClassUtils.getMethodIfAvailable(HttpPost.class, "releaseConnection");
72+
6673
private HttpClient httpClient;
6774

6875

@@ -146,10 +153,17 @@ protected RemoteInvocationResult doExecuteRequest(
146153

147154
HttpPost postMethod = createHttpPost(config);
148155
setRequestBody(config, postMethod, baos);
149-
HttpResponse response = executeHttpPost(config, getHttpClient(), postMethod);
150-
validateResponse(config, response);
151-
InputStream responseBody = getResponseBody(config, response);
152-
return readRemoteInvocationResult(responseBody, config.getCodebaseUrl());
156+
try {
157+
HttpResponse response = executeHttpPost(config, getHttpClient(), postMethod);
158+
validateResponse(config, response);
159+
InputStream responseBody = getResponseBody(config, response);
160+
return readRemoteInvocationResult(responseBody, config.getCodebaseUrl());
161+
}
162+
finally {
163+
if (releaseConnectionMethod != null){
164+
ReflectionUtils.invokeMethod(releaseConnectionMethod, postMethod);
165+
}
166+
}
153167
}
154168

155169
/**

0 commit comments

Comments
 (0)