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);
}
}
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();
}
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");
}
}
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) {
}
}
}
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();
}
Aggregations