From 70e9312721caed8010dee55ae89e1cd7c2d84f26 Mon Sep 17 00:00:00 2001 From: Oleg Kalnichevski Date: Sun, 1 Jul 2007 10:11:34 +0000 Subject: [PATCH] Added connection release sample git-svn-id: https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpclient/trunk@552268 13f79535-47bb-0310-9956-ffa450edef68 --- .../examples/client/ClientAbortMethod.java | 13 +-- .../client/ClientConnectionRelease.java | 101 ++++++++++++++++++ .../examples/client/ClientCustomContext.java | 14 +-- .../examples/client/ClientExecuteDirect.java | 1 - .../examples/client/ClientExecuteProxy.java | 1 - .../http/impl/client/DefaultHttpClient.java | 5 + 6 files changed, 109 insertions(+), 26 deletions(-) create mode 100644 module-client/src/examples/org/apache/http/examples/client/ClientConnectionRelease.java diff --git a/module-client/src/examples/org/apache/http/examples/client/ClientAbortMethod.java b/module-client/src/examples/org/apache/http/examples/client/ClientAbortMethod.java index 93b95ce53..ba3fb18e6 100644 --- a/module-client/src/examples/org/apache/http/examples/client/ClientAbortMethod.java +++ b/module-client/src/examples/org/apache/http/examples/client/ClientAbortMethod.java @@ -33,13 +33,9 @@ import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; -import org.apache.http.HttpVersion; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; -import org.apache.http.params.BasicHttpParams; -import org.apache.http.params.HttpParams; -import org.apache.http.params.HttpProtocolParams; /** * How to abort an HTTP method before its normal completion. @@ -52,14 +48,7 @@ public class ClientAbortMethod { public final static void main(String[] args) throws Exception { - - // Initialize default parameters - HttpParams params = new BasicHttpParams(); - HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); - HttpProtocolParams.setContentCharset(params, "UTF-8"); - HttpProtocolParams.setUserAgent(params, "Apache-HttpClient/4.0"); - - HttpClient httpclient = new DefaultHttpClient(params); + HttpClient httpclient = new DefaultHttpClient(); HttpGet httpget = new HttpGet("http://www.apache.org/"); diff --git a/module-client/src/examples/org/apache/http/examples/client/ClientConnectionRelease.java b/module-client/src/examples/org/apache/http/examples/client/ClientConnectionRelease.java new file mode 100644 index 000000000..297a61b26 --- /dev/null +++ b/module-client/src/examples/org/apache/http/examples/client/ClientConnectionRelease.java @@ -0,0 +1,101 @@ +/* + * $HeadURL$ + * $Revision$ + * $Date$ + * + * ==================================================================== + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ + +package org.apache.http.examples.client; + +import java.io.BufferedReader; +import java.io.InputStreamReader; + +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.client.HttpClient; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.DefaultHttpClient; + +/** + * This examples demonstrates the recommended way of using API to make sure + * the underlying connection gets released back to the connection manager. + * + * + * @version $Revision$ + * + * @since 4.0 + */ +public class ClientConnectionRelease { + + public final static void main(String[] args) throws Exception { + HttpClient httpclient = new DefaultHttpClient(); + + HttpGet httpget = new HttpGet("http://www.apache.org/"); + + // Execute HTTP request + System.out.println("executing request " + httpget.getURI()); + HttpResponse response = httpclient.execute(httpget); + + System.out.println("----------------------------------------"); + System.out.println(response.getStatusLine()); + System.out.println("----------------------------------------"); + + // Get hold of the response entity + HttpEntity entity = response.getEntity(); + + // If the response does not enclose an entity, there is no need + // to bother about connection release + if (entity != null) { + // do something useful with the response + BufferedReader reader = new BufferedReader( + new InputStreamReader(entity.getContent())); + System.out.println(reader.readLine()); + // In case of an IOException the connection will be released + // back to the connection manager automatically + // No need for ugly try-finally clause + + // If a runtime exception can be thrown in the process + // of response processing make sure the HTTP request + // gets aborted to ensure connection release. + try { + // Do something that can throw a non-fatal + // unchecked exception + } catch (RuntimeException ex) { + // Abort HTTP request to ensure connection release + httpget.abort(); + } + + // When done with the response either close the stream + reader.close(); + // Or call + entity.consumeContent(); + // Either of these methods will cause connection release + } + } + +} + diff --git a/module-client/src/examples/org/apache/http/examples/client/ClientCustomContext.java b/module-client/src/examples/org/apache/http/examples/client/ClientCustomContext.java index b3cb0445d..511194284 100644 --- a/module-client/src/examples/org/apache/http/examples/client/ClientCustomContext.java +++ b/module-client/src/examples/org/apache/http/examples/client/ClientCustomContext.java @@ -33,17 +33,13 @@ import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; -import org.apache.http.HttpVersion; -import org.apache.http.protocol.HttpContext; import org.apache.http.client.HttpClient; import org.apache.http.client.HttpState; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.protocol.HttpClientContext; import org.apache.http.cookie.Cookie; import org.apache.http.impl.client.DefaultHttpClient; -import org.apache.http.params.BasicHttpParams; -import org.apache.http.params.HttpParams; -import org.apache.http.params.HttpProtocolParams; +import org.apache.http.protocol.HttpContext; /** * This examples demonstrates the use of a local HTTP context @@ -57,14 +53,8 @@ public class ClientCustomContext { public final static void main(String[] args) throws Exception { - - // Initialize default parameters - HttpParams params = new BasicHttpParams(); - HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); - HttpProtocolParams.setContentCharset(params, "UTF-8"); - HttpProtocolParams.setUserAgent(params, "Apache-HttpClient/4.0"); - HttpClient httpclient = new DefaultHttpClient(params); + HttpClient httpclient = new DefaultHttpClient(); // Create a local instance of HttpState HttpState localState = new HttpState(); diff --git a/module-client/src/examples/org/apache/http/examples/client/ClientExecuteDirect.java b/module-client/src/examples/org/apache/http/examples/client/ClientExecuteDirect.java index 2a58edebc..f20f7e866 100644 --- a/module-client/src/examples/org/apache/http/examples/client/ClientExecuteDirect.java +++ b/module-client/src/examples/org/apache/http/examples/client/ClientExecuteDirect.java @@ -161,7 +161,6 @@ private final static void setup() { HttpParams params = new BasicHttpParams(); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); HttpProtocolParams.setContentCharset(params, "UTF-8"); - HttpProtocolParams.setUserAgent(params, "Jakarta-HttpClient/4.0"); HttpProtocolParams.setUseExpectContinue(params, true); defaultParameters = params; diff --git a/module-client/src/examples/org/apache/http/examples/client/ClientExecuteProxy.java b/module-client/src/examples/org/apache/http/examples/client/ClientExecuteProxy.java index c4b2425da..47a5ca3de 100644 --- a/module-client/src/examples/org/apache/http/examples/client/ClientExecuteProxy.java +++ b/module-client/src/examples/org/apache/http/examples/client/ClientExecuteProxy.java @@ -170,7 +170,6 @@ private final static void setup() { HttpParams params = new BasicHttpParams(); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); HttpProtocolParams.setContentCharset(params, "UTF-8"); - HttpProtocolParams.setUserAgent(params, "Jakarta-HttpClient/4.0"); HttpProtocolParams.setUseExpectContinue(params, true); defaultParameters = params; diff --git a/module-client/src/main/java/org/apache/http/impl/client/DefaultHttpClient.java b/module-client/src/main/java/org/apache/http/impl/client/DefaultHttpClient.java index 1e023fddd..4cee9361d 100644 --- a/module-client/src/main/java/org/apache/http/impl/client/DefaultHttpClient.java +++ b/module-client/src/main/java/org/apache/http/impl/client/DefaultHttpClient.java @@ -114,6 +114,11 @@ public DefaultHttpClient(final HttpParams params) { } + public DefaultHttpClient() { + super(null, null); + } + + protected HttpParams createHttpParams() { HttpParams params = new BasicHttpParams(); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);