Java tutorial
/* * Copyright 2013 BIG TU Wien. * * Licensed 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. */ package at.ac.tuwien.big.testsuite.impl.util; import java.io.IOException; import java.io.InterruptedIOException; import java.net.ConnectException; import java.net.UnknownHostException; import javax.enterprise.context.ApplicationScoped; import javax.enterprise.inject.Disposes; import javax.enterprise.inject.Produces; import javax.net.ssl.SSLException; import org.apache.http.HttpEntityEnclosingRequest; import org.apache.http.HttpRequest; import org.apache.http.client.HttpClient; import org.apache.http.client.HttpRequestRetryHandler; import org.apache.http.impl.client.DecompressingHttpClient; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.impl.conn.PoolingClientConnectionManager; import org.apache.http.protocol.ExecutionContext; import org.apache.http.protocol.HttpContext; /** * * @author Christian, Florian Zoubek */ public class HttpClientProducer { private static final int MAX_RETRY_COUNT = 10; @Produces @ApplicationScoped HttpClient produceHttpClient() { PoolingClientConnectionManager cm = new PoolingClientConnectionManager(); cm.setMaxTotal(100); cm.setDefaultMaxPerRoute(20); DefaultHttpClient client = new DefaultHttpClient(cm); client.setHttpRequestRetryHandler(new HttpRequestRetryHandler() { @Override public boolean retryRequest(IOException exception, int executionCount, HttpContext context) { if (executionCount >= MAX_RETRY_COUNT) { // Do not retry if over max retry count return false; } if (exception instanceof InterruptedIOException) { // Timeout return true; } if (exception instanceof UnknownHostException) { // Unknown host return true; } if (exception instanceof ConnectException) { // Connection refused return true; } if (exception instanceof SSLException) { // SSL handshake exception return true; } HttpRequest request = (HttpRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST); boolean idempotent = !(request instanceof HttpEntityEnclosingRequest); if (idempotent) { // Retry if the request is considered idempotent return true; } return false; } }); return new DecompressingHttpClient(client); } void disposeHttpClient(@Disposes HttpClient httpClient) { httpClient.getConnectionManager().shutdown(); } }