Search in sources :

Example 26 with BindException

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

the class ListenerService method start.

@Override
public void start(StartContext context) throws StartException {
    started = true;
    preStart(context);
    serverService.getValue().registerListener(this);
    try {
        openListener = createOpenListener();
        HttpHandler handler = serverService.getValue().getRoot();
        for (HandlerWrapper wrapper : listenerHandlerWrappers) {
            handler = wrapper.wrap(handler);
        }
        openListener.setRootHandler(handler);
        if (enabled) {
            final InetSocketAddress socketAddress = binding.getValue().getSocketAddress();
            final ChannelListener<AcceptingChannel<StreamConnection>> acceptListener = ChannelListeners.openListenerAdapter(openListener);
            startListening(worker.getValue(), socketAddress, acceptListener);
        }
        registerBinding();
    } catch (IOException e) {
        cleanFailedStart();
        if (e instanceof BindException) {
            final StringBuilder sb = new StringBuilder().append(e.getLocalizedMessage());
            final InetSocketAddress socketAddress = binding.getValue().getSocketAddress();
            if (socketAddress != null)
                sb.append(" ").append(socketAddress);
            throw new StartException(sb.toString());
        } else {
            throw UndertowLogger.ROOT_LOGGER.couldNotStartListener(name, e);
        }
    }
    statisticsChangeListener = (enabled) -> {
        OptionMap options = openListener.getUndertowOptions();
        OptionMap.Builder builder = OptionMap.builder().addAll(options);
        builder.set(UndertowOptions.ENABLE_STATISTICS, enabled);
        openListener.setUndertowOptions(builder.getMap());
    };
    getUndertowService().registerStatisticsListener(statisticsChangeListener);
}
Also used : HttpHandler(io.undertow.server.HttpHandler) InetSocketAddress(java.net.InetSocketAddress) OptionMap(org.xnio.OptionMap) BindException(java.net.BindException) StartException(org.jboss.msc.service.StartException) IOException(java.io.IOException) HandlerWrapper(io.undertow.server.HandlerWrapper) AcceptingChannel(org.xnio.channels.AcceptingChannel)

Example 27 with BindException

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

the class ServerTestUtils method startVoldemortCluster.

/**
     * This method wraps up all of the work that is done in many different tests
     * to set up some number of Voldemort servers in a cluster. This method
     * masks an intermittent TOCTOU problem with the ports identified by
     * {@link #findFreePorts(int)} not actually being free when a server needs
     * to bind to them. If this method returns, it will return a non-null
     * cluster. This method is not guaranteed to return, but will likely
     * eventually do so...
     *
     * @param numServers
     * @param voldemortServers
     * @param partitionMap
     * @param socketStoreFactory
     * @param useNio
     * @param clusterFile
     * @param storeFile
     * @param properties
     * @param customCluster Use this specified cluster object
     * @return Cluster object that was used to successfully start all of the
     *         servers.
     * @throws IOException
     */
// TODO: numServers is likely not needed. If this method is refactored in
// the future, then try and drop the numServers argument.
// So, is the socketStoreFactory argument.. It should be entirely hidden
// within the helper method
private static Cluster startVoldemortCluster(int numServers, VoldemortServer[] voldemortServers, int[][] partitionMap, SocketStoreFactory socketStoreFactory, boolean useNio, String clusterFile, String storeFile, Properties properties, Cluster customCluster) throws IOException {
    boolean started = false;
    Cluster cluster = null;
    while (!started) {
        try {
            cluster = internalStartVoldemortCluster(numServers, voldemortServers, partitionMap, socketStoreFactory, useNio, clusterFile, storeFile, properties, customCluster);
            started = true;
        } catch (BindException be) {
            logger.debug("Caught BindException when starting cluster. Will retry.");
        }
    }
    return cluster;
}
Also used : Cluster(voldemort.cluster.Cluster) BindException(java.net.BindException)

Example 28 with BindException

use of java.net.BindException in project distributedlog by twitter.

the class LocalDLMEmulator method runZookeeperOnAnyPort.

/**
     * Try to start zookkeeper locally on any port beginning with some base port.
     * Dump some socket info when bind fails.
     */
public static Pair<ZooKeeperServerShim, Integer> runZookeeperOnAnyPort(int basePort, File zkDir) throws Exception {
    final int MAX_RETRIES = 20;
    final int MIN_PORT = 1025;
    final int MAX_PORT = 65535;
    ZooKeeperServerShim zks = null;
    int zkPort = basePort;
    boolean success = false;
    int retries = 0;
    while (!success) {
        try {
            LOG.info("zk trying to bind to port " + zkPort);
            zks = LocalBookKeeper.runZookeeper(1000, zkPort, zkDir);
            success = true;
        } catch (BindException be) {
            retries++;
            if (retries > MAX_RETRIES) {
                throw be;
            }
            zkPort++;
            if (zkPort > MAX_PORT) {
                zkPort = MIN_PORT;
            }
        }
    }
    return Pair.of(zks, zkPort);
}
Also used : ZooKeeperServerShim(org.apache.bookkeeper.shims.zk.ZooKeeperServerShim) BindException(java.net.BindException)

Example 29 with BindException

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

the class TransactionControlRunningTest method testTwoRequiredsNestedNoRollbackForInnerException.

@Test
public void testTwoRequiredsNestedNoRollbackForInnerException() {
    AtomicReference<TransactionStatus> finalStatusOuter = new AtomicReference<>();
    AtomicReference<TransactionStatus> finalStatusInner = new AtomicReference<>();
    Exception userEx = new BindException("Bang!");
    try {
        txControl.required(() -> {
            assertTrue(txControl.activeTransaction());
            Object key = txControl.getCurrentContext().getTransactionKey();
            txControl.getCurrentContext().postCompletion(finalStatusOuter::set);
            try {
                txControl.build().noRollbackFor(BindException.class).requiresNew(() -> {
                    assertFalse(key.equals(txControl.getCurrentContext().getTransactionKey()));
                    txControl.getCurrentContext().postCompletion(finalStatusInner::set);
                    throw userEx;
                });
                fail("Should not be reached!");
            } catch (ScopedWorkException swe) {
                throw swe.as(BindException.class);
            }
            return null;
        });
        fail("Should not be reached!");
    } catch (ScopedWorkException swe) {
        assertSame(userEx, swe.getCause());
    }
    assertEquals(ROLLED_BACK, finalStatusOuter.get());
    assertEquals(COMMITTED, finalStatusInner.get());
}
Also used : ScopedWorkException(org.osgi.service.transaction.control.ScopedWorkException) TransactionStatus(org.osgi.service.transaction.control.TransactionStatus) BindException(java.net.BindException) AtomicReference(java.util.concurrent.atomic.AtomicReference) ScopedWorkException(org.osgi.service.transaction.control.ScopedWorkException) BindException(java.net.BindException) Test(org.junit.Test)

Example 30 with BindException

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

the class GatewayReceiverImpl method start.

public void start() throws IOException {
    if (receiver == null) {
        receiver = this.cache.addCacheServer(true);
    }
    if (receiver.isRunning()) {
        logger.warn(LocalizedMessage.create(LocalizedStrings.GatewayReceiver_IS_ALREADY_RUNNING));
        return;
    }
    boolean started = false;
    this.port = getPortToStart();
    while (!started && this.port != -1) {
        receiver.setPort(this.port);
        receiver.setSocketBufferSize(socketBufferSize);
        receiver.setMaximumTimeBetweenPings(timeBetPings);
        receiver.setHostnameForClients(host);
        receiver.setBindAddress(bindAdd);
        receiver.setGroups(new String[] { GatewayReceiver.RECEIVER_GROUP });
        ((CacheServerImpl) receiver).setGatewayTransportFilter(this.filters);
        try {
            receiver.start();
            started = true;
        } catch (BindException be) {
            if (be.getCause() != null && be.getCause().getMessage().contains("assign requested address")) {
                throw new GatewayReceiverException(LocalizedStrings.SocketCreator_FAILED_TO_CREATE_SERVER_SOCKET_ON_0_1.toLocalizedString(new Object[] { bindAdd, this.port }));
            }
            // ignore as this port might have been used by other threads.
            logger.warn(LocalizedMessage.create(LocalizedStrings.GatewayReceiver_Address_Already_In_Use, this.port));
            this.port = getPortToStart();
        } catch (SocketException se) {
            if (se.getMessage().contains("Address already in use")) {
                logger.warn(LocalizedMessage.create(LocalizedStrings.GatewayReceiver_Address_Already_In_Use, this.port));
                this.port = getPortToStart();
            } else {
                throw se;
            }
        }
    }
    if (!started) {
        throw new IllegalStateException("No available free port found in the given range.");
    }
    logger.info(LocalizedMessage.create(LocalizedStrings.GatewayReceiver_STARTED_ON_PORT, this.port));
    InternalDistributedSystem system = this.cache.getInternalDistributedSystem();
    system.handleResourceEvent(ResourceEvent.GATEWAYRECEIVER_START, this);
}
Also used : SocketException(java.net.SocketException) BindException(java.net.BindException) CacheServerImpl(org.apache.geode.internal.cache.CacheServerImpl) InternalDistributedSystem(org.apache.geode.distributed.internal.InternalDistributedSystem)

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