use of org.objectweb.proactive.core.exceptions.NotBoundException in project scheduling by ow2-proactive.
the class Connection method waitAndConnect.
/**
* Connects to the service with a specified timeout value. A timeout of
* zero is interpreted as an infinite timeout. The connection will then
* block until established or an error occurs.
*
* @param url the url on which to connect
* @param timeout the maximum amount of time to wait if it cannot connect
* @return the authentication if connection succeed
* @throws Exception if something wrong append
*/
public T waitAndConnect(String url, long timeout) throws Exception {
T authentication = null;
long leftTime = timeout == 0 ? Long.MAX_VALUE : timeout;
try {
// obtaining authentication active object
while (leftTime > 0) {
long startTime = System.currentTimeMillis();
try {
authentication = lookupAuthentication(url);
if (authentication == null) {
// simulating an exception during lookup
throw new Exception(ERROR_CANNOT_LOOKUP_AUTH);
}
// success
break;
} catch (NotBoundException e) {
// expected NotBoundException is not printed in the log
leftTime = sleepOrThrow(startTime, leftTime, e);
} catch (IOException e) {
leftTime = sleepOrThrow(startTime, leftTime, e);
} catch (Exception e) {
// unexpected Exception
logger.error("", e);
leftTime = sleepOrThrow(startTime, leftTime, e);
}
}
// waiting until scheduling is initialized
while (leftTime > 0) {
long startTime = System.currentTimeMillis();
BooleanWrapper future = authentication.isActivated();
try {
PAFuture.waitFor(future, leftTime);
} catch (ProActiveTimeoutException e) {
String errorMessage = "The ProActive server can not send a reply to the Node (usually due to a firewall). Please check the server logs for more information.";
logger.error(errorMessage, e);
throw new ProActiveRuntimeException(errorMessage, e);
}
if (authentication.isActivated().getBooleanValue()) {
// success
break;
} else {
leftTime = sleepOrThrow(startTime, leftTime, new Exception(ERROR_NOT_ACTIVATED));
}
}
} catch (InterruptedException e) {
throw new Exception(ERROR_CONNECTION_INTERRUPTED);
}
// TODO two cycles has the same pattern => the code can be unified
return authentication;
}
Aggregations