Search in sources :

Example 21 with BindException

use of java.net.BindException in project orientdb by orientechnologies.

the class OProxyServerListener method run.

public void run() {
    // WAIT THE REMOTE SOCKET IS AVAILABLE FIRST. IN THIS WAY THE LOCAL SOCKET IS CREATED ONLY AFTER THE REMOTE SOCKET IS
    // AVAILABLE. THIS ALLOWS TO RETURN A CONNECTION REFUSED TO THE CLIENT
    OLogManager.instance().info(this, "Proxy server: local port %d is waiting for the remote port %s:%d to be available...", localPort, server.getRemoteHost(), remotePort);
    if (server.isWaitUntilRemotePortsAreOpen()) {
        while (running) {
            try {
                final Socket remoteSocket = connectTargetServer();
                remoteSocket.close();
                break;
            } catch (Exception e) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e1) {
                    return;
                }
            }
        }
    }
    OLogManager.instance().info(this, "Proxy server: remote port %s:%d is available, creating the channel...", server.getRemoteHost(), remotePort);
    try {
        localSocket = new ServerSocket(localPort);
    } catch (Exception e) {
        OLogManager.instance().error(this, "Proxy server: error on creating local socket for proxy channel %d->%s:%d", e, localPort, server.getRemoteHost(), remotePort);
    }
    while (running && !localSocket.isClosed()) try {
        final Socket sourceSocket = localSocket.accept();
        final OProxyChannel channel = new OProxyChannel(this, sourceSocket, localPort, remotePort);
        channels.add(channel);
        channel.start();
    } catch (BindException e) {
        OLogManager.instance().error(this, "Proxy server: error on listening port %d->%s:%d", e, localPort, server.getRemoteHost(), remotePort);
        break;
    } catch (SocketException e) {
        OLogManager.instance().debug(this, "Proxy server: listening port %d is closed", e, localPort);
        break;
    } catch (Exception e) {
        OLogManager.instance().error(this, "Proxy server: closing proxy server %d->%s:%d", e, localPort, server.getRemoteHost(), remotePort);
        break;
    }
    shutdown();
}
Also used : SocketException(java.net.SocketException) BindException(java.net.BindException) ServerSocket(java.net.ServerSocket) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket) SocketException(java.net.SocketException) IOException(java.io.IOException) BindException(java.net.BindException)

Example 22 with BindException

use of java.net.BindException in project OpenRefine by OpenRefine.

the class ShutdownSignalHandler method init.

public void init(String host, int port) throws Exception {
    logger.info("Starting Server bound to '" + host + ":" + port + "'");
    String memory = Configurations.get("refine.memory");
    if (memory != null) {
        logger.info("refine.memory size: " + memory + " JVM Max heap: " + Runtime.getRuntime().maxMemory());
    }
    int maxThreads = Configurations.getInteger("refine.queue.size", 30);
    int maxQueue = Configurations.getInteger("refine.queue.max_size", 300);
    long keepAliveTime = Configurations.getInteger("refine.queue.idle_time", 60);
    LinkedBlockingQueue<Runnable> queue = new LinkedBlockingQueue<Runnable>(maxQueue);
    threadPool = new ThreadPoolExecutor(maxThreads, maxQueue, keepAliveTime, TimeUnit.SECONDS, queue);
    this.setThreadPool(new ThreadPoolExecutorAdapter(threadPool));
    Connector connector = new SocketConnector();
    connector.setPort(port);
    connector.setHost(host);
    connector.setMaxIdleTime(Configurations.getInteger("refine.connection.max_idle_time", 60000));
    connector.setStatsOn(false);
    this.addConnector(connector);
    File webapp = new File(Configurations.get("refine.webapp", "main/webapp"));
    if (!isWebapp(webapp)) {
        webapp = new File("main/webapp");
        if (!isWebapp(webapp)) {
            webapp = new File("webapp");
            if (!isWebapp(webapp)) {
                logger.warn("Warning: Failed to find web application at '" + webapp.getAbsolutePath() + "'");
                System.exit(-1);
            }
        }
    }
    final String contextPath = Configurations.get("refine.context_path", "/");
    final int maxFormContentSize = Configurations.getInteger("refine.max_form_content_size", 1048576);
    logger.info("Initializing context: '" + contextPath + "' from '" + webapp.getAbsolutePath() + "'");
    WebAppContext context = new WebAppContext(webapp.getAbsolutePath(), contextPath);
    context.setMaxFormContentSize(maxFormContentSize);
    this.setHandler(context);
    this.setStopAtShutdown(true);
    this.setSendServerVersion(true);
    // Enable context autoreloading
    if (Configurations.getBoolean("refine.autoreload", false)) {
        scanForUpdates(webapp, context);
    }
    // start the server
    try {
        this.start();
    } catch (BindException e) {
        logger.error("Failed to start server - is there another copy running already on this port/address?");
        throw e;
    }
    configure(context);
}
Also used : Connector(org.mortbay.jetty.Connector) SocketConnector(org.mortbay.jetty.bio.SocketConnector) ThreadPoolExecutorAdapter(com.google.util.threads.ThreadPoolExecutorAdapter) BindException(java.net.BindException) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) WebAppContext(org.mortbay.jetty.webapp.WebAppContext) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) SocketConnector(org.mortbay.jetty.bio.SocketConnector) File(java.io.File)

Example 23 with BindException

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

the class KafkaTestEnvironmentImpl method getKafkaServer.

/**
	 * Copied from com.github.sakserv.minicluster.KafkaLocalBrokerIntegrationTest (ASL licensed)
	 */
protected KafkaServer getKafkaServer(int brokerId, File tmpFolder) throws Exception {
    Properties kafkaProperties = new Properties();
    // properties have to be Strings
    kafkaProperties.put("advertised.host.name", KAFKA_HOST);
    kafkaProperties.put("broker.id", Integer.toString(brokerId));
    kafkaProperties.put("log.dir", tmpFolder.toString());
    kafkaProperties.put("zookeeper.connect", zookeeperConnectionString);
    kafkaProperties.put("message.max.bytes", String.valueOf(50 * 1024 * 1024));
    kafkaProperties.put("replica.fetch.max.bytes", String.valueOf(50 * 1024 * 1024));
    // for CI stability, increase zookeeper session timeout
    kafkaProperties.put("zookeeper.session.timeout.ms", zkTimeout);
    kafkaProperties.put("zookeeper.connection.timeout.ms", zkTimeout);
    if (additionalServerProperties != null) {
        kafkaProperties.putAll(additionalServerProperties);
    }
    final int numTries = 5;
    for (int i = 1; i <= numTries; i++) {
        int kafkaPort = NetUtils.getAvailablePort();
        kafkaProperties.put("port", Integer.toString(kafkaPort));
        //to support secure kafka cluster
        if (secureMode) {
            LOG.info("Adding Kafka secure configurations");
            kafkaProperties.put("listeners", "SASL_PLAINTEXT://" + KAFKA_HOST + ":" + kafkaPort);
            kafkaProperties.put("advertised.listeners", "SASL_PLAINTEXT://" + KAFKA_HOST + ":" + kafkaPort);
            kafkaProperties.putAll(getSecureProperties());
        }
        KafkaConfig kafkaConfig = new KafkaConfig(kafkaProperties);
        try {
            scala.Option<String> stringNone = scala.Option.apply(null);
            KafkaServer server = new KafkaServer(kafkaConfig, SystemTime$.MODULE$, stringNone);
            server.startup();
            return server;
        } catch (KafkaException e) {
            if (e.getCause() instanceof BindException) {
                // port conflict, retry...
                LOG.info("Port conflict when starting Kafka Broker. Retrying...");
            } else {
                throw e;
            }
        }
    }
    throw new Exception("Could not start Kafka after " + numTries + " retries due to port conflicts.");
}
Also used : KafkaServer(kafka.server.KafkaServer) BindException(java.net.BindException) KafkaException(kafka.common.KafkaException) NetUtils.hostAndPortToUrlString(org.apache.flink.util.NetUtils.hostAndPortToUrlString) Properties(java.util.Properties) KafkaException(kafka.common.KafkaException) BindException(java.net.BindException) KafkaConfig(kafka.server.KafkaConfig)

Example 24 with BindException

use of java.net.BindException in project Openfire by igniterealtime.

the class RelayChannel method createLocalRelayChannel.

public static RelayChannel createLocalRelayChannel(final String host, final int minPort, final int maxPort) throws IOException {
    int range = maxPort - minPort;
    IOException be = null;
    for (int t = 0; t < 50; t++) {
        try {
            int a = Math.round((int) (Math.random() * range)) + minPort;
            a = a % 2 == 0 ? a : a + 1;
            return new RelayChannel(host, a);
        } catch (BindException e) {
            be = e;
        } catch (IOException e) {
            be = e;
        }
    }
    throw be;
}
Also used : BindException(java.net.BindException) IOException(java.io.IOException)

Example 25 with BindException

use of java.net.BindException in project jdk8u_jdk by JetBrains.

the class RmiBootstrapTest method testConfigurationFile.

/**
     * Test a configuration file. Determines whether the bootstrap
     * should succeed or fail depending on the file name:
     *     *ok.properties: bootstrap should succeed.
     *     *ko.properties: bootstrap or connection should fail.
     * @return null if the test succeeds, an error message otherwise.
     **/
private String testConfigurationFile(String fileName) {
    File file = new File(fileName);
    final String portStr = System.getProperty("rmi.port", null);
    final int port = portStr != null ? Integer.parseInt(portStr) : basePort;
    if (fileName.endsWith("ok.properties")) {
        String errStr = null;
        for (int i = 0; i < PORT_TEST_LEN; i++) {
            try {
                errStr = testConfiguration(file, port + testPort++);
                return errStr;
            } catch (BindException e) {
            // port conflict; try another port
            }
        }
        return "Can not locate available port";
    }
    if (fileName.endsWith("ko.properties")) {
        return testConfigurationKo(file, port + testPort++);
    }
    return fileName + ": test file suffix must be one of [ko|ok].properties";
}
Also used : BindException(java.net.BindException) File(java.io.File)

Aggregations

BindException (java.net.BindException)103 IOException (java.io.IOException)34 InetSocketAddress (java.net.InetSocketAddress)25 ServerSocket (java.net.ServerSocket)22 Test (org.junit.Test)22 File (java.io.File)12 SocketException (java.net.SocketException)10 Configuration (org.apache.hadoop.conf.Configuration)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 InterruptedIOException (java.io.InterruptedIOException)5 MiniDFSNNTopology (org.apache.hadoop.hdfs.MiniDFSNNTopology)5 InetAddress (java.net.InetAddress)4 UnknownHostException (java.net.UnknownHostException)4 RemoteException (java.rmi.RemoteException)4 NIOServerCnxnFactory (org.apache.zookeeper.server.NIOServerCnxnFactory)4 ZooKeeperServer (org.apache.zookeeper.server.ZooKeeperServer)4 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)3 Socket (java.net.Socket)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3