Search in sources :

Example 86 with BindException

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

the class ServerLauncherLocalIntegrationTest method testStartWithDefaultPortInUseFails.

@Test
public void testStartWithDefaultPortInUseFails() throws Throwable {
    String rootFolder = this.temporaryFolder.getRoot().getCanonicalPath();
    // generate one free port and then use TEST_OVERRIDE_DEFAULT_PORT_PROPERTY
    this.socket = SocketCreatorFactory.getSocketCreatorForComponent(SecurableCommunicationChannel.CLUSTER).createServerSocket(this.serverPort, 50, null, -1);
    assertFalse(AvailablePort.isPortAvailable(this.serverPort, AvailablePort.SOCKET));
    // build and start the server
    final Builder builder = new Builder().setMemberName(getUniqueName()).setRedirectOutput(true).setWorkingDirectory(rootFolder).set(LOG_LEVEL, "config").set(MCAST_PORT, "0");
    this.launcher = builder.build();
    RuntimeException expected = null;
    try {
        this.launcher.start();
        // why did it not fail like it's supposed to?
        final String property = System.getProperty(AbstractCacheServer.TEST_OVERRIDE_DEFAULT_PORT_PROPERTY);
        assertNotNull(property);
        assertEquals(this.serverPort, Integer.valueOf(property).intValue());
        assertFalse(AvailablePort.isPortAvailable(this.serverPort, AvailablePort.SOCKET));
        fail("Server port is " + this.launcher.getCache().getCacheServers().get(0).getPort());
        fail("ServerLauncher start should have thrown RuntimeException caused by BindException");
    } catch (RuntimeException e) {
        expected = e;
        assertNotNull(expected.getMessage());
    // BindException text varies by platform
    } catch (Throwable e) {
        this.errorCollector.addError(e);
    }
    try {
        assertNotNull(expected);
        final Throwable cause = expected.getCause();
        assertNotNull(cause);
        assertTrue(cause instanceof BindException);
    // BindException string varies by platform
    } catch (Throwable e) {
        this.errorCollector.addError(e);
    }
    try {
        this.pidFile = new File(this.temporaryFolder.getRoot(), ProcessType.SERVER.getPidFileName());
        assertFalse("Pid file should not exist: " + this.pidFile, this.pidFile.exists());
        // creation of log file seems to be random -- look into why sometime
        final String logFileName = getUniqueName() + ".log";
        assertFalse("Log file should not exist: " + logFileName, new File(this.temporaryFolder.getRoot(), logFileName).exists());
    } catch (Throwable e) {
        this.errorCollector.addError(e);
    }
    // just in case the launcher started...
    ServerState status = null;
    try {
        status = this.launcher.stop();
    } catch (Throwable t) {
    // ignore
    }
    try {
        waitForFileToDelete(this.pidFile);
        assertEquals(getExpectedStopStatusForNotRunning(), status.getStatus());
    } catch (Throwable e) {
        this.errorCollector.addError(e);
    }
}
Also used : Builder(org.apache.geode.distributed.ServerLauncher.Builder) ServerState(org.apache.geode.distributed.ServerLauncher.ServerState) BindException(java.net.BindException) File(java.io.File) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 87 with BindException

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

the class RestAPIsWithSSLDUnitTest method startManager.

private int startManager(final String locators, final String[] regions, final Properties sslProperties) throws IOException {
    IgnoredException.addIgnoredException("java.net.BindException");
    IgnoredException.addIgnoredException("java.rmi.server.ExportException");
    IgnoredException.addIgnoredException("org.apache.geode.management.ManagementException");
    Properties props = new Properties();
    props.setProperty(MCAST_PORT, "0");
    props.setProperty(LOCATORS, locators);
    props.setProperty(JMX_MANAGER, "true");
    props.setProperty(JMX_MANAGER_START, "true");
    Cache cache = null;
    configureSSL(props, sslProperties, false);
    while (true) {
        try {
            DistributedSystem ds = getSystem(props);
            System.out.println("Creating cache with http-service-port " + props.getProperty(HTTP_SERVICE_PORT, "7070") + " and jmx-manager-port " + props.getProperty(JMX_MANAGER_PORT, "1099"));
            cache = CacheFactory.create(ds);
            System.out.println("Successfully created cache.");
            break;
        } catch (ManagementException ex) {
            if ((ex.getCause() instanceof BindException) || (ex.getCause() != null && ex.getCause().getCause() instanceof BindException)) {
                // close cache and disconnect
                InternalCache existingInstance = GemFireCacheImpl.getInstance();
                if (existingInstance != null) {
                    existingInstance.close();
                }
                InternalDistributedSystem ids = InternalDistributedSystem.getConnectedInstance();
                if (ids != null) {
                    ids.disconnect();
                }
                // try a different port
                int httpServicePort = AvailablePortHelper.getRandomAvailableTCPPort();
                int jmxManagerPort = AvailablePortHelper.getRandomAvailableTCPPort();
                props.setProperty(HTTP_SERVICE_PORT, Integer.toString(httpServicePort));
                props.setProperty(JMX_MANAGER_PORT, Integer.toString(jmxManagerPort));
                System.out.println("Try a different http-service-port " + httpServicePort);
                System.out.println("Try a different jmx-manager-port " + jmxManagerPort);
            } else {
                throw ex;
            }
        }
    }
    AttributesFactory factory = new AttributesFactory();
    factory.setEnableBridgeConflation(true);
    factory.setDataPolicy(DataPolicy.REPLICATE);
    RegionAttributes attrs = factory.create();
    for (int i = 0; i < regions.length; i++) {
        cache.createRegion(regions[i], attrs);
    }
    CacheServer server = cache.addCacheServer();
    server.setPort(0);
    server.start();
    return server.getPort();
}
Also used : ManagementException(org.apache.geode.management.ManagementException) AttributesFactory(org.apache.geode.cache.AttributesFactory) RegionAttributes(org.apache.geode.cache.RegionAttributes) BindException(java.net.BindException) InternalCache(org.apache.geode.internal.cache.InternalCache) CacheServer(org.apache.geode.cache.server.CacheServer) InternalDistributedSystem(org.apache.geode.distributed.internal.InternalDistributedSystem) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties) InternalDistributedSystem(org.apache.geode.distributed.internal.InternalDistributedSystem) DistributedSystem(org.apache.geode.distributed.DistributedSystem) Cache(org.apache.geode.cache.Cache) ClientCache(org.apache.geode.cache.client.ClientCache) InternalCache(org.apache.geode.internal.cache.InternalCache)

Example 88 with BindException

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

the class Reuse method main.

public static void main(String[] args) throws Exception {
    MulticastSocket s1, s2;
    try {
        s1 = new MulticastSocket(4160);
        s2 = new MulticastSocket(4160);
        s1.close();
        s2.close();
    } catch (BindException e) {
        throw new RuntimeException("MulticastSocket do no set SO_REUSEADDR");
    }
}
Also used : MulticastSocket(java.net.MulticastSocket) BindException(java.net.BindException)

Example 89 with BindException

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

the class BindFailTest method main.

public static void main(String[] args) throws Exception {
    DatagramSocket s = new DatagramSocket();
    int port = s.getLocalPort();
    for (int i = 0; i < 32000; i++) {
        try {
            DatagramSocket s2 = new DatagramSocket(port);
        } catch (BindException e) {
        }
    }
}
Also used : DatagramSocket(java.net.DatagramSocket) BindException(java.net.BindException)

Example 90 with BindException

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

the class Server method start.

public static String start() throws Exception {
    int serverPort = 12345;
    ObjectName name = new ObjectName("test", "foo", "bar");
    MBeanServer jmxServer = ManagementFactory.getPlatformMBeanServer();
    SteMBean bean = new Ste();
    jmxServer.registerMBean(bean, name);
    boolean exported = false;
    Random rnd = new Random(System.currentTimeMillis());
    do {
        try {
            LocateRegistry.createRegistry(serverPort);
            exported = true;
        } catch (ExportException ee) {
            if (ee.getCause() instanceof BindException) {
                serverPort = rnd.nextInt(10000) + 4096;
            } else {
                throw ee;
            }
        }
    } while (!exported);
    JMXServiceURL serverUrl = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:" + serverPort + "/test");
    JMXConnectorServer jmxConnector = JMXConnectorServerFactory.newJMXConnectorServer(serverUrl, null, jmxServer);
    jmxConnector.start();
    return serverUrl.toString();
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) Random(java.util.Random) BindException(java.net.BindException) ObjectName(javax.management.ObjectName) MBeanServer(javax.management.MBeanServer) ExportException(java.rmi.server.ExportException) JMXConnectorServer(javax.management.remote.JMXConnectorServer)

Aggregations

BindException (java.net.BindException)104 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