Search in sources :

Example 1 with BindException

use of java.net.BindException in project camel by apache.

the class MllpTcpServerConsumer method doStart.

@Override
protected void doStart() throws Exception {
    log.debug("doStart() - creating acceptor thread");
    ServerSocket serverSocket = new ServerSocket();
    if (null != endpoint.receiveBufferSize) {
        serverSocket.setReceiveBufferSize(endpoint.receiveBufferSize);
    }
    serverSocket.setReuseAddress(endpoint.reuseAddress);
    // Accept Timeout
    serverSocket.setSoTimeout(endpoint.acceptTimeout);
    InetSocketAddress socketAddress;
    if (null == endpoint.getHostname()) {
        socketAddress = new InetSocketAddress(endpoint.getPort());
    } else {
        socketAddress = new InetSocketAddress(endpoint.getHostname(), endpoint.getPort());
    }
    long startTicks = System.currentTimeMillis();
    do {
        try {
            serverSocket.bind(socketAddress, endpoint.backlog);
        } catch (BindException bindException) {
            if (System.currentTimeMillis() > startTicks + endpoint.getBindTimeout()) {
                log.error("Failed to bind to address {} within timeout {}", socketAddress, endpoint.getBindTimeout());
                throw bindException;
            } else {
                log.warn("Failed to bind to address {} - retrying in {} milliseconds", socketAddress, endpoint.getBindRetryInterval());
                Thread.sleep(endpoint.getBindRetryInterval());
            }
        }
    } while (!serverSocket.isBound());
    serverSocketThread = new ServerSocketThread(serverSocket);
    serverSocketThread.start();
    super.doStart();
}
Also used : InetSocketAddress(java.net.InetSocketAddress) BindException(java.net.BindException) ServerSocket(java.net.ServerSocket)

Example 2 with BindException

use of java.net.BindException in project flink by apache.

the class TaskManagerStartupTest method testStartupWhenTaskmanagerActorPortIsUsed.

/**
	 * Tests that the TaskManager fails synchronously when the actor system port
	 * is in use.
	 * 
	 * @throws Throwable
	 */
@Test(expected = BindException.class)
public void testStartupWhenTaskmanagerActorPortIsUsed() throws BindException {
    ServerSocket blocker = null;
    try {
        final String localHostName = "localhost";
        final InetAddress localBindAddress = InetAddress.getByName(NetUtils.getWildcardIPAddress());
        // block some port
        blocker = new ServerSocket(0, 50, localBindAddress);
        final int port = blocker.getLocalPort();
        TaskManager.runTaskManager(localHostName, ResourceID.generate(), port, new Configuration(), TaskManager.class);
        fail("This should fail with an IOException");
    } catch (IOException e) {
        // expected. validate the error message
        List<Throwable> causes = StartupUtils.getExceptionCauses(e, new ArrayList<Throwable>());
        for (Throwable cause : causes) {
            if (cause instanceof BindException) {
                throw (BindException) cause;
            }
        }
        fail("This should fail with an exception caused by BindException");
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    } finally {
        if (blocker != null) {
            try {
                blocker.close();
            } catch (IOException e) {
            // no need to log here
            }
        }
    }
}
Also used : Configuration(org.apache.flink.configuration.Configuration) ArrayList(java.util.ArrayList) BindException(java.net.BindException) ServerSocket(java.net.ServerSocket) ArrayList(java.util.ArrayList) List(java.util.List) IOException(java.io.IOException) InetAddress(java.net.InetAddress) IllegalConfigurationException(org.apache.flink.configuration.IllegalConfigurationException) IOException(java.io.IOException) BindException(java.net.BindException) Test(org.junit.Test)

Example 3 with BindException

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

the class Server method bind.

public static void bind(ServerSocket socket, InetSocketAddress address, int backlog, Configuration conf, String rangeConf) throws IOException {
    try {
        IntegerRanges range = null;
        if (rangeConf != null) {
            range = conf.getRange(rangeConf, "");
        }
        if (range == null || range.isEmpty() || (address.getPort() != 0)) {
            socket.bind(address, backlog);
        } else {
            for (Integer port : range) {
                if (socket.isBound())
                    break;
                try {
                    InetSocketAddress temp = new InetSocketAddress(address.getAddress(), port);
                    socket.bind(temp, backlog);
                } catch (BindException e) {
                //Ignored
                }
            }
            if (!socket.isBound()) {
                throw new BindException("Could not find a free port in " + range);
            }
        }
    } catch (SocketException e) {
        throw NetUtils.wrapException(null, 0, address.getHostName(), address.getPort(), e);
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SocketException(java.net.SocketException) InetSocketAddress(java.net.InetSocketAddress) BindException(java.net.BindException) IntegerRanges(org.apache.hadoop.conf.Configuration.IntegerRanges)

Example 4 with BindException

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

the class HttpServer2 method bindForPortRange.

/**
   * Bind using port ranges. Keep on looking for a free port in the port range
   * and throw a bind exception if no port in the configured range binds.
   * @param listener jetty listener.
   * @param startPort initial port which is set in the listener.
   * @throws Exception
   */
private void bindForPortRange(ServerConnector listener, int startPort) throws Exception {
    BindException bindException = null;
    try {
        bindListener(listener);
        return;
    } catch (BindException ex) {
        // Ignore exception.
        bindException = ex;
    }
    for (Integer port : portRanges) {
        if (port == startPort) {
            continue;
        }
        Thread.sleep(100);
        listener.setPort(port);
        try {
            bindListener(listener);
            return;
        } catch (BindException ex) {
            // Ignore exception. Move to next port.
            bindException = ex;
        }
    }
    throw constructBindException(listener, bindException);
}
Also used : BindException(java.net.BindException)

Example 5 with BindException

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

the class TestNetUtils method testWrapBindException.

@Test
public void testWrapBindException() throws Throwable {
    IOException e = new BindException("failed");
    IOException wrapped = verifyExceptionClass(e, BindException.class);
    assertInException(wrapped, "failed");
    assertLocalDetailsIncluded(wrapped);
    assertNotInException(wrapped, DEST_PORT_NAME);
    assertInException(wrapped, "/BindException");
}
Also used : BindException(java.net.BindException) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

BindException (java.net.BindException)96 IOException (java.io.IOException)31 InetSocketAddress (java.net.InetSocketAddress)23 Test (org.junit.Test)22 ServerSocket (java.net.ServerSocket)21 File (java.io.File)11 SocketException (java.net.SocketException)10 Configuration (org.apache.hadoop.conf.Configuration)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 MiniDFSNNTopology (org.apache.hadoop.hdfs.MiniDFSNNTopology)5 InterruptedIOException (java.io.InterruptedIOException)4 InetAddress (java.net.InetAddress)4 RemoteException (java.rmi.RemoteException)4 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)3 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)3 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)3