in iothub/device/iot-device-client/src/main/java/com/microsoft/azure/sdk/iot/device/transport/IotHubTransport.java [471:543]
public void open(boolean withRetry) throws TransportException, IotHubClientException
{
if (this.connectionStatus == IotHubConnectionStatus.CONNECTED)
{
return;
}
if (this.connectionStatus == IotHubConnectionStatus.DISCONNECTED_RETRYING)
{
throw new TransportException("Open cannot be called while transport is reconnecting");
}
this.isClosing = false;
this.taskScheduler = Executors.newScheduledThreadPool(1);
if (withRetry)
{
int connectionAttempt = 0;
long startTime = System.currentTimeMillis();
// this loop either ends in throwing an exception when retry expires,
// a break statement upon a successful openConnection() call,
// or the user attempts to close the client
while (true)
{
if (this.isClosing)
{
throw new TransportException("Client was closed while attempting to open the connection");
}
RetryPolicy retryPolicy = isMultiplexing ? multiplexingRetryPolicy : this.getDefaultConfig().getRetryPolicy();
try
{
openConnection();
break; // openConnection() only returns without throwing if the connection attempt was successful
}
catch (TransportException transportException)
{
log.debug("Encountered an exception while opening the client. Checking the configured retry policy to see if another attempt should be made.", transportException);
RetryDecision retryDecision = retryPolicy.getRetryDecision(connectionAttempt, transportException);
if (!retryDecision.shouldRetry())
{
throw new TransportException("Retry expired while attempting to open the connection", transportException);
}
connectionAttempt++;
if (hasOperationTimedOut(startTime))
{
throw new TransportException("Open operation timed out. The nested exception is the most recent exception thrown while attempting to open the connection", transportException);
}
try
{
log.trace("The configured retry policy allows for another attempt. Sleeping for {} milliseconds before the next attempt", retryDecision.getDuration());
Thread.sleep(retryDecision.getDuration());
}
catch (InterruptedException e)
{
throw new TransportException("InterruptedException thrown while sleeping between connection attempts", e);
}
}
}
}
else
{
openConnection();
}
log.debug("Client connection opened successfully");
}