in iothub/device/iot-device-client/src/main/java/com/microsoft/azure/sdk/iot/device/transport/IotHubReceiveTask.java [45:104]
public void run()
{
String threadName = "";
if (this.useIdentifiableThreadNames)
{
String deviceClientId = this.transport.getDeviceClientUniqueIdentifier();
String connectionId = transport.getTransportConnectionId();
threadName += deviceClientId + "-" + "Cxn" + connectionId + "-" + THREAD_NAME;
}
else
{
if (this.threadNamePrefix != null && !this.threadNamePrefix.isEmpty())
{
threadName += this.threadNamePrefix;
}
threadName += THREAD_NAME;
if (this.threadNameSuffix != null && !this.threadNameSuffix.isEmpty())
{
threadName += this.threadNameSuffix;
}
}
Thread.currentThread().setName(threadName);
try
{
// HTTP is the only protocol where the SDK must actively poll for received messages. Because of that, never
// wait on the IoTHubTransport layer to notify this thread that a received message is ready to be handled.
if (this.transport.getProtocol() != IotHubClientProtocol.HTTPS)
{
if (!this.transport.hasReceivedMessagesToHandle() && !this.transport.isClosed())
{
// IotHubTransport layer will make this semaphore available to acquire only once a received message
// is ready to be handled. Once it is made available to acquire, this thread will
// wake up and handle the received messages. Until then, do nothing.
//
// Note that this thread is not expected to release the semaphore once it is done handling messages.
// This semaphore is not acquired to safely modify shared resources, but instead is used to signal
// when to start working. It is more akin to the basic Java wait/notify pattern, but without the
// order of operations dependency that wait/notify has.
this.receiveThreadSemaphore.acquire();
}
}
this.transport.handleMessage();
}
catch (InterruptedException e)
{
// Typically happens if a disconnection event occurs and the DeviceIO layer cancels the send/receive threads
// while the reconnection takes place.
log.trace("Interrupted while waiting for work. Thread is now ending.");
}
catch (Throwable e)
{
log.warn("Receive task thread encountered exception while processing received messages", e);
}
}