if (logger.isLoggable(BasicLevel.DEBUG))
logger.log(BasicLevel.DEBUG,
"ReliableTcpClient[" + identity + ',' + key + "].connect(" + reconnect + ')');
if (status != INIT)
throw new IllegalStateException("Connect: state error");
long startTime = System.currentTimeMillis();
long endTime = startTime;
if (addresses.size() > 1) {
// infinite retry in case of HA.
endTime = Long.MAX_VALUE;
} else {
if (reconnect) {
endTime += reconnectTimeout;
} else {
endTime += params.connectingTimer * 1000L;
}
}
int attemptsC = 0;
long nextSleep = 100;
while (true) {
if (status == CLOSE)
throw new IllegalStateException("Closed connection");
attemptsC++;
for (int i = 0; i < addresses.size(); i++) {
ServerAddress sa = (ServerAddress)addresses.elementAt(i);
try {
doConnect(sa.hostName, sa.port);
setStatus(CONNECT);
return;
} catch (JMSSecurityException exc) {
throw exc;
} catch (UnknownHostException uhe) {
if (logger.isLoggable(BasicLevel.DEBUG))
logger.log(BasicLevel.DEBUG, "ReliableTcpClient.connect", uhe);
IllegalStateException jmsExc = new IllegalStateException("Server's host is unknown: " + sa.hostName);
jmsExc.setLinkedException(uhe);
throw jmsExc;
} catch (IOException ioe) {
if (logger.isLoggable(BasicLevel.DEBUG))
logger.log(BasicLevel.DEBUG, "ReliableTcpClient.connect", ioe);
// continue
} catch (JMSException jmse) {
if (logger.isLoggable(BasicLevel.DEBUG))
logger.log(BasicLevel.DEBUG, "ReliableTcpClient.connect", jmse);
// continue
} catch (Exception e) {
if (logger.isLoggable(BasicLevel.DEBUG))
logger.log(BasicLevel.DEBUG, "ReliableTcpClient.connect", e);
// continue
}
}
long currentTime = System.currentTimeMillis();
if (logger.isLoggable(BasicLevel.DEBUG))
logger.log(BasicLevel.DEBUG,
" -> currentTime = " + currentTime + ",endTime = " + endTime);
// Keep on trying as long as timer is ok:
if (currentTime < endTime) {
if (logger.isLoggable(BasicLevel.DEBUG))
logger.log(BasicLevel.DEBUG,
" -> retry connection " + identity + ',' + key);
if (currentTime + nextSleep > endTime) {
nextSleep = endTime - currentTime;
}
// Sleeping for a while:
try {
wait(nextSleep);
} catch (InterruptedException intExc) {
throw new IllegalStateException("Could not open the connection with " + addresses + ": interrupted");
}
// Trying again!
nextSleep = nextSleep * 2;
} else {
if (logger.isLoggable(BasicLevel.DEBUG))
logger.log(BasicLevel.DEBUG,
" -> close connection " + identity + ',' + key);
// If timer is over, throwing an IllegalStateException:
long attemptsT = (System.currentTimeMillis() - startTime) / 1000;
IllegalStateException jmsExc = new IllegalStateException("Could not connect to JMS server with "
+ addresses + " after " + attemptsC + " attempts during " + attemptsT
+ " secs: server is not listening or server protocol version is not compatible with client.");
throw jmsExc;
}
}