Search in sources :

Example 1 with ConnectException

use of java.net.ConnectException in project jetty.project by eclipse.

the class JettyStopTask method execute.

/** 
     * @see org.apache.tools.ant.Task#execute()
     */
public void execute() throws BuildException {
    try {
        Socket s = new Socket(InetAddress.getByName("127.0.0.1"), stopPort);
        if (stopWait > 0)
            s.setSoTimeout(stopWait * 1000);
        try {
            OutputStream out = s.getOutputStream();
            out.write((stopKey + "\r\nstop\r\n").getBytes());
            out.flush();
            if (stopWait > 0) {
                TaskLog.log("Waiting" + (stopWait > 0 ? (" " + stopWait + "sec") : "") + " for jetty to stop");
                LineNumberReader lin = new LineNumberReader(new InputStreamReader(s.getInputStream()));
                String response = lin.readLine();
                if ("Stopped".equals(response))
                    System.err.println("Stopped");
            }
        } finally {
            s.close();
        }
    } catch (ConnectException e) {
        TaskLog.log("Jetty not running!");
    } catch (Exception e) {
        TaskLog.log(e.getMessage());
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) OutputStream(java.io.OutputStream) Socket(java.net.Socket) ConnectException(java.net.ConnectException) BuildException(org.apache.tools.ant.BuildException) LineNumberReader(java.io.LineNumberReader) ConnectException(java.net.ConnectException)

Example 2 with ConnectException

use of java.net.ConnectException in project jetty.project by eclipse.

the class GracefulStopTest method testGracefulComplete.

/**
     * Test of standard graceful timeout mechanism when a block request does
     * complete. Note that even though the request completes after 100ms, the
     * stop always takes 1000ms
     * @throws Exception on test failure
     */
@Test
public void testGracefulComplete() throws Exception {
    assumeTrue(!OS.IS_WINDOWS);
    Server server = new Server();
    server.setStopTimeout(10000);
    ServerConnector connector = new ServerConnector(server);
    connector.setPort(0);
    server.addConnector(connector);
    TestHandler handler = new TestHandler();
    StatisticsHandler stats = new StatisticsHandler();
    server.setHandler(stats);
    stats.setHandler(handler);
    server.start();
    final int port = connector.getLocalPort();
    try (final Socket client1 = new Socket("127.0.0.1", port);
        final Socket client2 = new Socket("127.0.0.1", port)) {
        client1.getOutputStream().write(("POST / HTTP/1.0\r\n" + "Host: localhost:" + port + "\r\n" + "Content-Type: plain/text\r\n" + "Content-Length: 10\r\n" + "\r\n" + "12345").getBytes());
        client1.getOutputStream().flush();
        handler.latch.await();
        new Thread() {

            @Override
            public void run() {
                long now = TimeUnit.NANOSECONDS.toMillis(System.nanoTime());
                long end = now + 500;
                try {
                    Thread.sleep(100);
                    // Try creating a new connection
                    try {
                        new Socket("127.0.0.1", port);
                        throw new IllegalStateException();
                    } catch (ConnectException e) {
                    }
                    // Try another request on existing connection
                    client2.getOutputStream().write(("GET / HTTP/1.0\r\n" + "Host: localhost:" + port + "\r\n" + "\r\n").getBytes());
                    client2.getOutputStream().flush();
                    String response2 = IO.toString(client2.getInputStream());
                    assertThat(response2, containsString(" 503 "));
                    now = TimeUnit.NANOSECONDS.toMillis(System.nanoTime());
                    Thread.sleep(Math.max(1, end - now));
                    client1.getOutputStream().write("567890".getBytes());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
        long start = System.nanoTime();
        server.stop();
        long stop = System.nanoTime();
        assertThat(TimeUnit.NANOSECONDS.toMillis(stop - start), greaterThan(490L));
        assertThat(TimeUnit.NANOSECONDS.toMillis(stop - start), lessThan(10000L));
        String response = IO.toString(client1.getInputStream());
        assertThat(handler.handling.get(), Matchers.is(false));
        assertThat(response, containsString(" 200 OK"));
        assertThat(response, containsString("read 10/10"));
        assertThat(stats.getRequests(), Matchers.is(2));
        assertThat(stats.getResponses5xx(), Matchers.is(1));
    }
}
Also used : Matchers.containsString(org.hamcrest.Matchers.containsString) ServletException(javax.servlet.ServletException) TimeoutException(java.util.concurrent.TimeoutException) EofException(org.eclipse.jetty.io.EofException) ConnectException(java.net.ConnectException) ClosedChannelException(java.nio.channels.ClosedChannelException) IOException(java.io.IOException) EOFException(java.io.EOFException) StatisticsHandler(org.eclipse.jetty.server.handler.StatisticsHandler) Socket(java.net.Socket) ConnectException(java.net.ConnectException) Test(org.junit.Test)

Example 3 with ConnectException

use of java.net.ConnectException in project jetty.project by eclipse.

the class ClientConnectTest method testConnectionRefused.

@Test
public void testConnectionRefused() throws Exception {
    JettyTrackingSocket wsocket = new JettyTrackingSocket();
    // Intentionally bad port with nothing listening on it
    URI wsUri = new URI("ws://127.0.0.1:1");
    try {
        Future<Session> future = client.connect(wsocket, wsUri);
        // The attempt to get upgrade response future should throw error
        future.get(3, TimeUnit.SECONDS);
        Assert.fail("Expected ExecutionException -> ConnectException");
    } catch (ConnectException e) {
        Throwable t = wsocket.errorQueue.remove();
        Assert.assertThat("Error Queue[0]", t, instanceOf(ConnectException.class));
        wsocket.assertNotOpened();
    } catch (ExecutionException e) {
        if (OS.IS_WINDOWS) {
            // On windows, this is a SocketTimeoutException
            assertExpectedError(e, wsocket, SocketTimeoutException.class);
        } else {
            // Expected path - java.net.ConnectException
            assertExpectedError(e, wsocket, ConnectException.class);
        }
    }
}
Also used : SocketTimeoutException(java.net.SocketTimeoutException) ExecutionException(java.util.concurrent.ExecutionException) URI(java.net.URI) Session(org.eclipse.jetty.websocket.api.Session) ConnectException(java.net.ConnectException) Test(org.junit.Test)

Example 4 with ConnectException

use of java.net.ConnectException in project jetty.project by eclipse.

the class Main method stop.

public void stop(String host, int port, String key, int timeout) {
    if (host == null || host.length() == 0) {
        host = "127.0.0.1";
    }
    try {
        if ((port <= 0) || (port > 65535)) {
            System.err.println("STOP.PORT property must be specified with a valid port number");
            usageExit(ERR_BAD_STOP_PROPS);
        }
        if (key == null) {
            key = "";
            System.err.println("STOP.KEY property must be specified");
            System.err.println("Using empty key");
        }
        try (Socket s = new Socket(InetAddress.getByName(host), port)) {
            if (timeout > 0) {
                s.setSoTimeout(timeout * 1000);
            }
            try (OutputStream out = s.getOutputStream()) {
                out.write((key + "\r\nstop\r\n").getBytes());
                out.flush();
                if (timeout > 0) {
                    StartLog.info("Waiting %,d seconds for jetty to stop%n", timeout);
                    LineNumberReader lin = new LineNumberReader(new InputStreamReader(s.getInputStream()));
                    String response;
                    while ((response = lin.readLine()) != null) {
                        StartLog.debug("Received \"%s\"", response);
                        if ("Stopped".equals(response)) {
                            StartLog.warn("Server reports itself as Stopped");
                        }
                    }
                }
            }
        }
    } catch (SocketTimeoutException e) {
        StartLog.warn("Timed out waiting for stop confirmation");
        System.exit(ERR_UNKNOWN);
    } catch (ConnectException e) {
        usageExit(e, ERR_NOT_STOPPED, jsvcStartArgs.isTestingModeEnabled());
    } catch (Exception e) {
        usageExit(e, ERR_UNKNOWN, jsvcStartArgs.isTestingModeEnabled());
    }
}
Also used : SocketTimeoutException(java.net.SocketTimeoutException) InputStreamReader(java.io.InputStreamReader) OutputStream(java.io.OutputStream) Socket(java.net.Socket) SocketTimeoutException(java.net.SocketTimeoutException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) LineNumberReader(java.io.LineNumberReader) ConnectException(java.net.ConnectException)

Example 5 with ConnectException

use of java.net.ConnectException in project hadoop by apache.

the class TestWebAppProxyServlet method testAppReportForEmptyTrackingUrl.

@Test(timeout = 5000)
public void testAppReportForEmptyTrackingUrl() throws Exception {
    configuration.set(YarnConfiguration.PROXY_ADDRESS, "localhost:9090");
    // overriding num of web server threads, see HttpServer.HTTP_MAXTHREADS
    configuration.setInt("hadoop.http.max.threads", 10);
    WebAppProxyServerForTest proxy = new WebAppProxyServerForTest();
    proxy.init(configuration);
    proxy.start();
    int proxyPort = proxy.proxy.proxyServer.getConnectorAddress(0).getPort();
    AppReportFetcherForTest appReportFetcher = proxy.proxy.appReportFetcher;
    try {
        //set AHS_ENBALED = false to simulate getting the app report from RM
        configuration.setBoolean(YarnConfiguration.APPLICATION_HISTORY_ENABLED, false);
        ApplicationId app = ApplicationId.newInstance(0, 0);
        appReportFetcher.answer = 6;
        URL url = new URL("http://localhost:" + proxyPort + "/proxy/" + app.toString());
        HttpURLConnection proxyConn = (HttpURLConnection) url.openConnection();
        proxyConn.connect();
        try {
            proxyConn.getResponseCode();
        } catch (ConnectException e) {
        // Connection Exception is expected as we have set
        // appReportFetcher.answer = 6, which does not set anything for
        // original tracking url field in the app report.
        }
        String appAddressInRm = WebAppUtils.getResolvedRMWebAppURLWithScheme(configuration) + "/cluster" + "/app/" + app.toString();
        assertTrue("Webapp proxy servlet should have redirected to RM", proxyConn.getURL().toString().equals(appAddressInRm));
        //set AHS_ENBALED = true to simulate getting the app report from AHS
        configuration.setBoolean(YarnConfiguration.APPLICATION_HISTORY_ENABLED, true);
        proxyConn = (HttpURLConnection) url.openConnection();
        proxyConn.connect();
        try {
            proxyConn.getResponseCode();
        } catch (ConnectException e) {
        // Connection Exception is expected as we have set
        // appReportFetcher.answer = 6, which does not set anything for
        // original tracking url field in the app report.
        }
        String appAddressInAhs = WebAppUtils.getHttpSchemePrefix(configuration) + WebAppUtils.getAHSWebAppURLWithoutScheme(configuration) + "/applicationhistory" + "/app/" + app.toString();
        assertTrue("Webapp proxy servlet should have redirected to AHS", proxyConn.getURL().toString().equals(appAddressInAhs));
    } finally {
        proxy.close();
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) ApplicationId(org.apache.hadoop.yarn.api.records.ApplicationId) URL(java.net.URL) ConnectException(java.net.ConnectException) Test(org.junit.Test)

Aggregations

ConnectException (java.net.ConnectException)521 IOException (java.io.IOException)211 Socket (java.net.Socket)84 SocketTimeoutException (java.net.SocketTimeoutException)69 Test (org.junit.Test)69 UnknownHostException (java.net.UnknownHostException)57 InetSocketAddress (java.net.InetSocketAddress)56 WebServiceException (javax.xml.ws.WebServiceException)48 SOAPFaultException (javax.xml.ws.soap.SOAPFaultException)48 ErrorCode (org.olat.modules.vitero.model.ErrorCode)48 FileNotFoundException (java.io.FileNotFoundException)39 URL (java.net.URL)36 InputStream (java.io.InputStream)33 NoRouteToHostException (java.net.NoRouteToHostException)33 SocketException (java.net.SocketException)33 ArrayList (java.util.ArrayList)31 InetAddress (java.net.InetAddress)29 InputStreamReader (java.io.InputStreamReader)28 OutputStream (java.io.OutputStream)28 HttpURLConnection (java.net.HttpURLConnection)28