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