Search in sources :

Example 46 with ConnectException

use of java.net.ConnectException in project pinot by linkedin.

the class NettyTCPClientConnection method connect.

/**
   * Open a connection
   */
@Override
public boolean connect() {
    try {
        checkTransition(State.CONNECTED);
        //Connect synchronously. At the end of this line, _channel should have been set
        TimerContext t = MetricsHelper.startTimer();
        ChannelFuture f = _bootstrap.connect(_server.getHostname(), _server.getPort()).sync();
        /**
       * Waiting for future alone does not guarantee that _channel is set. _channel is set
       * only when the channelActive() async callback runs. So we should also wait for it.
       */
        f.get();
        _channelSet.await();
        t.stop();
        _connState = State.CONNECTED;
        _clientMetric.addConnectStats(t.getLatencyMs());
        return true;
    } catch (Exception ie) {
        if (ie instanceof ConnectException && ie.getMessage() != null && ie.getMessage().startsWith("Connection refused")) {
            // Most common case when a server is down. Don't print the entire stack and fill the logs.
            LOGGER.error("Could not connect to server {}:{} connId:{}", _server, ie.getMessage(), getConnId());
        } else {
            LOGGER.error("Got exception when connecting to server {} connId {}", _server, ie, getConnId());
        }
    }
    return false;
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) TimerContext(com.linkedin.pinot.common.metrics.MetricsHelper.TimerContext) ConnectException(java.net.ConnectException) ConnectException(java.net.ConnectException)

Example 47 with ConnectException

use of java.net.ConnectException in project rest.li by linkedin.

the class LoadBalancerUtilTest method testFindOriginalThrowable.

@Test
public void testFindOriginalThrowable() {
    ConnectException connectException = new ConnectException("Foo");
    RemoteInvocationException e = new RemoteInvocationException("Failed to get connect to a server", connectException);
    Throwable throwable = LoadBalancerUtil.findOriginalThrowable(e);
    Assert.assertEquals(throwable, connectException);
    //we only go as far as 100 level deep for finding exception
    Exception npe = new NullPointerException();
    Exception temp = npe;
    for (int i = 0; i < 100; i++) {
        e = new RemoteInvocationException(temp);
        temp = e;
    }
    throwable = LoadBalancerUtil.findOriginalThrowable(e);
    Assert.assertEquals(throwable, npe);
    //we add the 101th exception then we lost the reference to NullPointerException
    e = new RemoteInvocationException(temp);
    throwable = LoadBalancerUtil.findOriginalThrowable(e);
    Assert.assertFalse(throwable instanceof NullPointerException);
}
Also used : RemoteInvocationException(com.linkedin.r2.RemoteInvocationException) RemoteInvocationException(com.linkedin.r2.RemoteInvocationException) ConnectException(java.net.ConnectException) ConnectException(java.net.ConnectException) Test(org.testng.annotations.Test)

Example 48 with ConnectException

use of java.net.ConnectException in project jmxtrans by jmxtrans.

the class OpenTSDBWriter method internalWrite.

@Override
public void internalWrite(Server server, Query query, ImmutableList<Result> results) throws Exception {
    Socket socket = null;
    PrintWriter writer = null;
    try {
        socket = pool.borrowObject(address);
        writer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(), UTF_8), true);
        for (String formattedResult : messageFormatter.formatResults(results, server)) {
            log.debug("OpenTSDB Message: {}", formattedResult);
            writer.write("put " + formattedResult + "\n");
        }
    } catch (ConnectException e) {
        log.error("Error while connecting to OpenTSDB", e);
    } finally {
        if (writer != null && writer.checkError()) {
            log.error("Error writing to OpenTSDB, clearing OpenTSDB socket pool");
            pool.invalidateObject(address, socket);
        } else {
            pool.returnObject(address, socket);
        }
    }
}
Also used : OutputStreamWriter(java.io.OutputStreamWriter) Socket(java.net.Socket) PrintWriter(java.io.PrintWriter) ConnectException(java.net.ConnectException)

Example 49 with ConnectException

use of java.net.ConnectException in project hudson-2.x by hudson.

the class ProxyConfiguration method open.

/**
     * This method should be used wherever {@link URL#openConnection()} to internet URLs is invoked directly.
     */
public static URLConnection open(URL url) throws IOException {
    // this code might run on slaves
    Hudson hudson = Hudson.getInstance();
    ProxyConfiguration proxyConfig = hudson != null ? hudson.proxy : null;
    if (proxyConfig == null) {
        return url.openConnection();
    }
    if (proxyConfig.noProxyFor != null) {
        StringTokenizer tokenizer = new StringTokenizer(proxyConfig.noProxyFor, ",");
        while (tokenizer.hasMoreTokens()) {
            String noProxyHost = tokenizer.nextToken().trim();
            if (noProxyHost.contains("*")) {
                if (url.getHost().trim().contains(noProxyHost.replaceAll("\\*", ""))) {
                    return url.openConnection(Proxy.NO_PROXY);
                }
            } else if (url.getHost().trim().equals(noProxyHost)) {
                return url.openConnection(Proxy.NO_PROXY);
            }
        }
    }
    URLConnection urlConnection = url.openConnection(proxyConfig.createProxy());
    if (proxyConfig.isAuthNeeded()) {
        String credentials = proxyConfig.getUserName() + ":" + proxyConfig.getPassword();
        String encoded = new String(Base64.encodeBase64(credentials.getBytes()));
        urlConnection.setRequestProperty("Proxy-Authorization", "Basic " + encoded);
    }
    boolean connected = false;
    int count = 0;
    while (!connected) {
        try {
            urlConnection.connect();
            connected = true;
        } catch (SocketTimeoutException exc) {
            LOGGER.fine("Connection timed out. trying again " + count);
            if (++count > TIME_OUT_RETRY_COUNT) {
                throw new IOException("Could not connect to " + url.toExternalForm() + ". Connection timed out after " + TIME_OUT_RETRY_COUNT + " tries.");
            }
            connected = false;
        } catch (UnknownHostException exc) {
            throw new IOException2("Could not connect to " + url.toExternalForm() + ". Check your internet connection.", exc);
        } catch (ConnectException exc) {
            throw new IOException2("Could not connect to " + url.toExternalForm() + ". Check your internet connection.", exc);
        }
    }
    return urlConnection;
}
Also used : StringTokenizer(java.util.StringTokenizer) SocketTimeoutException(java.net.SocketTimeoutException) UnknownHostException(java.net.UnknownHostException) Hudson(hudson.model.Hudson) IOException(java.io.IOException) URLConnection(java.net.URLConnection) IOException2(hudson.util.IOException2) ConnectException(java.net.ConnectException)

Example 50 with ConnectException

use of java.net.ConnectException in project jsoup by jhy.

the class UrlConnectTest method invalidProxyFails.

@Test
public void invalidProxyFails() throws IOException {
    boolean caught = false;
    String url = "https://jsoup.org";
    try {
        Document doc = Jsoup.connect(url).proxy("localhost", 8889).get();
    } catch (IOException e) {
        caught = e instanceof ConnectException;
    }
    assertTrue(caught);
}
Also used : IOException(java.io.IOException) Document(org.jsoup.nodes.Document) ConnectException(java.net.ConnectException) Test(org.junit.Test)

Aggregations

ConnectException (java.net.ConnectException)253 IOException (java.io.IOException)110 Socket (java.net.Socket)57 Test (org.junit.Test)43 InetSocketAddress (java.net.InetSocketAddress)41 SocketTimeoutException (java.net.SocketTimeoutException)35 UnknownHostException (java.net.UnknownHostException)30 InetAddress (java.net.InetAddress)24 FileNotFoundException (java.io.FileNotFoundException)22 InterruptedIOException (java.io.InterruptedIOException)20 SocketException (java.net.SocketException)20 NoRouteToHostException (java.net.NoRouteToHostException)19 InputStreamReader (java.io.InputStreamReader)18 URL (java.net.URL)16 InputStream (java.io.InputStream)14 HttpURLConnection (java.net.HttpURLConnection)14 TimeoutTracker (org.opennms.core.utils.TimeoutTracker)13 BufferedReader (java.io.BufferedReader)12 OutputStream (java.io.OutputStream)12 Exchange (org.apache.camel.Exchange)12