Example usage for java.net ConnectException ConnectException

List of usage examples for java.net ConnectException ConnectException

Introduction

In this page you can find the example usage for java.net ConnectException ConnectException.

Prototype

public ConnectException() 

Source Link

Document

Construct a new ConnectException with no detailed message.

Usage

From source file:cn.caimatou.canting.utils.http.asynchttp.AsyncHttpRequest.java

private void makeRequestWithRetries() throws ConnectException {
    // This is an additional layer of retry logic lifted from droid-fu
    // See: https://github.com/kaeppler/droid-fu/blob/master/src/main/java/com/github/droidfu/http/BetterHttpRequestBase.java
    boolean retry = true;
    IOException cause = null;/*from ww w  .j a  v a2s.c o m*/
    HttpRequestRetryHandler retryHandler = client.getHttpRequestRetryHandler();
    while (retry) {
        try {
            makeRequest();
            return;
        } catch (UnknownHostException e) {
            if (responseHandler != null) {
                responseHandler.sendFailureMessage(e, "can't resolve host");
            }
            return;
        } catch (SocketException e) {
            // Added to detect host unreachable
            if (responseHandler != null) {
                responseHandler.sendFailureMessage(e, "can't resolve host");
            }
            return;
        } catch (SocketTimeoutException e) {
            if (responseHandler != null) {
                responseHandler.sendFailureMessage(e, "socket time out");
            }
            return;
        } catch (IOException e) {
            cause = e;
            retry = retryHandler.retryRequest(cause, ++executionCount, context);
        } catch (NullPointerException e) {
            // there's a bug in HttpClient 4.0.x that on some occasions causes
            // DefaultRequestExecutor to throw an NPE, see
            // http://code.google.com/p/android/issues/detail?id=5255
            cause = new IOException("NPE in HttpClient" + e.getMessage());
            retry = retryHandler.retryRequest(cause, ++executionCount, context);
        }
    }

    // no retries left, crap out with exception
    ConnectException ex = new ConnectException();
    ex.initCause(cause);
    throw ex;
}

From source file:com.elephant.http.AsyncHttpRequest.java

private void makeRequestWithRetries() throws ConnectException {
    // This is an additional layer of retry logic lifted from droid-fu
    // See://from  w w w . j a  v  a 2  s. c om
    // https://github.com/kaeppler/droid-fu/blob/master/src/main/java/com/github/droidfu/http/BetterHttpRequestBase.java
    boolean retry = true;
    IOException cause = null;
    HttpRequestRetryHandler retryHandler = client.getHttpRequestRetryHandler();
    while (retry) {
        try {
            makeRequest();
            return;
        } catch (UnknownHostException e) {
            if (responseHandler != null) {
                responseHandler.sendFailureMessage(e, "can't resolve host");
            }
            return;
        } catch (SocketException e) {
            // Added to detect host unreachable
            if (responseHandler != null) {
                responseHandler.sendFailureMessage(e, "can't resolve host");
            }
            return;
        } catch (SocketTimeoutException e) {
            if (responseHandler != null) {
                responseHandler.sendFailureMessage(e, "socket time out");
            }
            return;
        } catch (IOException e) {
            cause = e;
            retry = retryHandler.retryRequest(cause, ++executionCount, context);
        } catch (NullPointerException e) {
            // there's a bug in HttpClient 4.0.x that on some occasions
            // causes
            // DefaultRequestExecutor to throw an NPE, see
            // http://code.google.com/p/android/issues/detail?id=5255
            cause = new IOException("NPE in HttpClient" + e.getMessage());
            retry = retryHandler.retryRequest(cause, ++executionCount, context);
        }
    }

    // no retries left, crap out with exception
    ConnectException ex = new ConnectException();
    ex.initCause(cause);
    throw ex;
}

From source file:com.github.ignition.support.http.IgnitedHttpRequestBase.java

@Override
public IgnitedHttpResponse send() throws ConnectException {

    IgnitedHttpRequestRetryHandler retryHandler = new IgnitedHttpRequestRetryHandler(maxRetries);

    // tell HttpClient to user our own retry handler
    httpClient.setHttpRequestRetryHandler(retryHandler);

    HttpContext context = new BasicHttpContext();

    // Grab a coffee now and lean back, I'm not good at explaining stuff. This code realizes
    // a second retry layer on top of HttpClient. Rationale: HttpClient.execute sometimes craps
    // out even *before* the HttpRequestRetryHandler set above is called, e.g. on a
    // "Network unreachable" SocketException, which can happen when failing over from Wi-Fi to
    // 3G or vice versa. Hence, we catch these exceptions, feed it through the same retry
    // decision method *again*, and align the execution count along the way.
    boolean retry = true;
    IOException cause = null;/*w ww .j  ava 2 s  .co m*/
    while (retry) {
        try {
            return httpClient.execute(request, this, context);
        } catch (IOException e) {
            cause = e;
            retry = retryRequest(retryHandler, cause, context);
        } catch (NullPointerException e) {
            // there's a bug in HttpClient 4.0.x that on some occasions causes
            // DefaultRequestExecutor to throw an NPE, see
            // http://code.google.com/p/android/issues/detail?id=5255
            cause = new IOException("NPE in HttpClient" + e.getMessage());
            retry = retryRequest(retryHandler, cause, context);
        } finally {
            // if timeout was changed with this request using withTimeout(), reset it
            if (timeoutChanged) {
                ignitedHttp.setConnectionTimeout(oldConnTimeout);
                ignitedHttp.setSocketTimeout(oldSocketTimeout);
            }
        }
    }

    // no retries left, crap out with exception
    ConnectException ex = new ConnectException();
    ex.initCause(cause);
    throw ex;
}

From source file:cn.edu.szjm.support.http.IgnitedHttpRequestBase.java

public Object send() throws ConnectException {

    IgnitedHttpRequestRetryHandler retryHandler = new IgnitedHttpRequestRetryHandler(maxRetries);

    // tell HttpClient to user our own retry handler
    httpClient.setHttpRequestRetryHandler(retryHandler);

    HttpContext context = new BasicHttpContext();

    // Grab a coffee now and lean back, I'm not good at explaining stuff. This code realizes
    // a second retry layer on top of HttpClient. Rationale: HttpClient.execute sometimes craps
    // out even *before* the HttpRequestRetryHandler set above is called, e.g. on a
    // "Network unreachable" SocketException, which can happen when failing over from Wi-Fi to
    // 3G or vice versa. Hence, we catch these exceptions, feed it through the same retry
    // decision method *again*, and align the execution count along the way.
    boolean retry = true;
    IOException cause = null;/*  ww w.j av a 2 s.com*/
    while (retry) {
        try {
            if (wrapResponse) {
                return httpClient.execute(request, this, context);
            } else {
                return httpClient.execute(request, context);
            }
        } catch (IOException e) {
            cause = e;
            retry = retryRequest(retryHandler, cause, context);
        } catch (NullPointerException e) {
            // there's a bug in HttpClient 4.0.x that on some occasions causes
            // DefaultRequestExecutor to throw an NPE, see
            // http://code.google.com/p/android/issues/detail?id=5255
            cause = new IOException("NPE in HttpClient" + e.getMessage());
            retry = retryRequest(retryHandler, cause, context);
        } finally {
            // if timeout was changed with this request using withTimeout(), reset it
            if (timeoutChanged) {
                ignitedHttp.setConnectionTimeout(oldConnTimeout);
                ignitedHttp.setSocketTimeout(oldSocketTimeout);
            }
        }
    }

    // no retries left, crap out with exception
    ConnectException ex = new ConnectException();
    ex.initCause(cause);
    throw ex;
}

From source file:com.qk.applibrary.http.AsyncHttpRequest.java

private void makeRequestWithRetries() throws ConnectException {
    // This is an additional layer of retry logic lifted from droid-fu
    // See: https://github.com/kaeppler/droid-fu/blob/master/src/main/java/com/github/droidfu/http/BetterHttpRequestBase.java
    boolean retry = true;
    IOException cause = null;//from www  . j a v a 2  s  .  co  m
    HttpRequestRetryHandler retryHandler = client.getHttpRequestRetryHandler();
    while (retry) {
        try {
            makeRequest();
            return;
        } catch (UnknownHostException e) {
            if (responseHandler != null) {
                responseHandler.sendFailureMessage(e, "can't resolve host");
            }
            return;
        } catch (SocketException e) {
            // Added to detect host unreachable
            if (responseHandler != null) {
                responseHandler.sendFailureMessage(e, "can't resolve host");
            }
            return;
        } catch (SocketTimeoutException e) {
            if (responseHandler != null) {
                responseHandler.sendFailureMessage(e, "socket time out");
            }
            return;
        } catch (IOException e) {
            cause = e;
            retry = retryHandler.retryRequest(cause, ++executionCount, context);
        } catch (NullPointerException e) {
            // there's a bug in HttpClient 4.0.x that on some occasions causes
            // DefaultRequestExecutor to throw an NPE, see
            // http://code.google.com/p/android/issues/detail?id=5255
            cause = new IOException("NPE in HttpClient" + e.getMessage());
            retry = retryHandler.retryRequest(cause, ++executionCount, context);
        } catch (Exception e) {
            if (responseHandler != null) {
                responseHandler.sendFailureMessage(e, e.getMessage());
            }
            return;
        }
    }

    // no retries left, crap out with exception
    ConnectException ex = new ConnectException();
    ex.initCause(cause);
    throw ex;
}

From source file:com.waltz3d.common.httpclient.request.AsyncHttpRequest.java

private void makeRequestWithRetries() throws ConnectException {
    // This is an additional layer of retry logic lifted from droid-fu
    // See: https://github.com/kaeppler/droid-fu/blob/master/src/main/java/com/github/droidfu/http/BetterHttpRequestBase.java
    boolean retry = true;
    IOException cause = null;//  w ww . j  a v a  2s .c om
    HttpRequestRetryHandler retryHandler = client.getHttpRequestRetryHandler();
    while (retry) {
        try {
            makeRequest();
            return;
        } catch (UnknownHostException e) {
            e.printStackTrace();
            if (responseHandler != null) {
                responseHandler.sendFailureMessage(e, "can't resolve host");
            }
            return;
        } catch (SocketException e) {
            e.printStackTrace();
            // Added to detect host unreachable
            if (responseHandler != null) {
                responseHandler.sendFailureMessage(e, "can't resolve host");
            }
            return;
        } catch (SocketTimeoutException e) {
            e.printStackTrace();
            if (responseHandler != null) {
                responseHandler.sendFailureMessage(e, "socket time out");
            }
            return;
        } catch (IOException e) {
            e.printStackTrace();
            cause = e;
            retry = retryHandler.retryRequest(cause, ++executionCount, context);
        } catch (NullPointerException e) {
            e.printStackTrace();
            // there's a bug in HttpClient 4.0.x that on some occasions causes
            // DefaultRequestExecutor to throw an NPE, see
            // http://code.google.com/p/android/issues/detail?id=5255
            cause = new IOException("NPE in HttpClient" + e.getMessage());
            retry = retryHandler.retryRequest(cause, ++executionCount, context);
        }
    }

    // no retries left, crap out with exception
    ConnectException ex = new ConnectException();
    ex.initCause(cause);
    throw ex;
}

From source file:com.pyj.http.AsyncHttpRequest.java

private void makeRequestWithRetries() throws ConnectException {
    // This is an additional layer of retry logic lifted from droid-fu
    // See: https://github.com/kaeppler/droid-fu/blob/master/src/main/java/com/github/droidfu/http/BetterHttpRequestBase.java
    boolean retry = true;
    IOException cause = null;//w  ww  .  j  a  v  a  2s  .com
    HttpRequestRetryHandler retryHandler = client.getHttpRequestRetryHandler();
    while (retry) {
        try {
            makeRequest();
            return;
        } catch (UnknownHostException e) {
            if (responseHandler != null) {
                responseHandler.sendFailureMessage(
                        new HupuHttpException("???????"), (String) null,
                        reqType);
            }
            return;
        } catch (SocketException e) {
            // Added to detect host unreachable
            if (responseHandler != null) {
                responseHandler.sendFailureMessage(
                        new HupuHttpException("????"), (String) null,
                        reqType);
            }
            return;
        } catch (SocketTimeoutException e) {
            if (responseHandler != null) {
                responseHandler.sendFailureMessage(
                        new HupuHttpException("????"), (String) null,
                        reqType);
            }
            return;
        } catch (IOException e) {
            cause = e;
            retry = retryHandler.retryRequest(cause, ++executionCount, context);
        } catch (NullPointerException e) {
            // there's a bug in HttpClient 4.0.x that on some occasions causes
            // DefaultRequestExecutor to throw an NPE, see
            // http://code.google.com/p/android/issues/detail?id=5255
            cause = new IOException("NPE in HttpClient" + e.getMessage());
            retry = retryHandler.retryRequest(cause, ++executionCount, context);
        }
    }

    // no retries left, crap out with exception
    ConnectException ex = new ConnectException();
    ex.initCause(cause);
    throw ex;
}

From source file:org.apache.hadoop.hbase.ipc.AsyncRpcChannel.java

/**
 * Calls method on channel/* w  w w  . ja v  a  2  s. c om*/
 * @param method to call
 * @param controller to run call with
 * @param request to send
 * @param responsePrototype to construct response with
 */
public Promise<Message> callMethod(final Descriptors.MethodDescriptor method,
        final PayloadCarryingRpcController controller, final Message request, final Message responsePrototype) {
    final AsyncCall call = new AsyncCall(channel.eventLoop(), client.callIdCnt.getAndIncrement(), method,
            request, controller, responsePrototype);
    controller.notifyOnCancel(new RpcCallback<Object>() {
        @Override
        public void run(Object parameter) {
            // TODO: do not need to call AsyncCall.setFailed?
            synchronized (pendingCalls) {
                pendingCalls.remove(call.id);
            }
        }
    });
    // TODO: this should be handled by PayloadCarryingRpcController.
    if (controller.isCanceled()) {
        // To finish if the call was cancelled before we set the notification (race condition)
        call.cancel(true);
        return call;
    }

    synchronized (pendingCalls) {
        if (closed) {
            Promise<Message> promise = channel.eventLoop().newPromise();
            promise.setFailure(new ConnectException());
            return promise;
        }
        pendingCalls.put(call.id, call);
        // Add timeout for cleanup if none is present
        if (cleanupTimer == null && call.getRpcTimeout() > 0) {
            cleanupTimer = client.newTimeout(timeoutTask, call.getRpcTimeout(), TimeUnit.MILLISECONDS);
        }
        if (!connected) {
            return call;
        }
    }
    writeRequest(call);
    return call;
}

From source file:org.apache.hadoop.hbase.ipc.AsyncRpcChannelImpl.java

/**
 * Calls method on channel/* w  w w. j a  v a  2  s. co  m*/
 * @param method to call
 * @param request to send
 * @param cellScanner with cells to send
 * @param responsePrototype to construct response with
 * @param rpcTimeout timeout for request
 * @param priority for request
 * @return Promise for the response Message
 */
@Override
public <R extends Message, O> Future<O> callMethod(final Descriptors.MethodDescriptor method,
        final Message request, final CellScanner cellScanner, R responsePrototype,
        MessageConverter<R, O> messageConverter, IOExceptionConverter exceptionConverter, long rpcTimeout,
        int priority) {
    final AsyncCall<R, O> call = new AsyncCall<>(this, client.callIdCnt.getAndIncrement(), method, request,
            cellScanner, responsePrototype, messageConverter, exceptionConverter, rpcTimeout, priority,
            client.metrics);

    synchronized (pendingCalls) {
        if (closed) {
            call.setFailure(new ConnectException());
            return call;
        }
        pendingCalls.put(call.id, call);
        // Add timeout for cleanup if none is present
        if (cleanupTimer == null && call.getRpcTimeout() > 0) {
            cleanupTimer = client.newTimeout(timeoutTask, call.getRpcTimeout(), TimeUnit.MILLISECONDS);
        }
        if (!connected) {
            return call;
        }
    }
    writeRequest(call);
    return call;
}

From source file:org.apache.atlas.AtlasClientTest.java

@Test
public void shouldRetryAPICallsOnClientHandlerException() throws AtlasServiceException, URISyntaxException {
    setupRetryParams();//from  w w  w.j  av  a2s.  com

    ResourceCreator resourceCreator = mock(ResourceCreator.class);
    WebResource resourceObject = mock(WebResource.class);
    when(resourceObject.getURI()).thenReturn(new URI("http://localhost:31000/api/atlas/types"))
            .thenReturn(new URI("http://localhost:41000/api/atlas/types"))
            .thenReturn(new URI("http://localhost:41000/api/atlas/types"));

    WebResource.Builder builder = getBuilder(resourceObject);

    ClientResponse response = mock(ClientResponse.class);
    when(response.getStatus()).thenReturn(Response.Status.OK.getStatusCode());
    String activeStatus = "{\"Status\":\"ACTIVE\"}";
    when(response.getEntity(String.class)).thenReturn(activeStatus);
    when(response.getLength()).thenReturn(activeStatus.length());

    when(builder.method(AtlasClient.API.LIST_TYPES.getMethod(), ClientResponse.class, null))
            .thenThrow(
                    new ClientHandlerException("simulating exception in calling API", new ConnectException()))
            .thenReturn(response);

    when(resourceCreator.createResource()).thenReturn(resourceObject);

    AtlasClient atlasClient = getClientForTest("http://localhost:31000", "http://localhost:41000");

    atlasClient.setService(service);
    atlasClient.setConfiguration(configuration);

    atlasClient.callAPIWithRetries(AtlasClient.API.LIST_TYPES, null, resourceCreator);

    verify(client).destroy();
    verify(client).resource(UriBuilder.fromUri("http://localhost:31000").build());
    verify(client).resource(UriBuilder.fromUri("http://localhost:41000").build());
}