Search in sources :

Example 46 with TransportException

use of com.microsoft.azure.sdk.iot.device.exceptions.TransportException in project azure-iot-sdk-java by Azure.

the class MqttIotHubConnection method sendMessageResult.

/**
 * Sends an ACK to the service for the provided message
 * @param message the message to acknowledge to the service
 * @param result Ignored. The only ack that can be sent in MQTT is COMPLETE
 * @return true if the ACK was sent successfully and false otherwise
 * @throws TransportException if the ACK could not be sent successfully
 */
@Override
public boolean sendMessageResult(IotHubTransportMessage message, IotHubMessageResult result) throws TransportException {
    if (message == null || result == null) {
        throw new TransportException(new IllegalArgumentException("message and result must be non-null"));
    }
    int messageId;
    log.trace("Checking if MQTT layer can acknowledge the received message ({})", message);
    if (receivedMessagesToAcknowledge.containsKey(message)) {
        messageId = receivedMessagesToAcknowledge.get(message);
    } else {
        TransportException e = new TransportException(new IllegalArgumentException("Provided message cannot be acknowledged because it was already acknowledged or was never received from service"));
        log.error("Mqtt layer could not acknowledge received message because it has no mapping to an outstanding mqtt message id ({})", message, e);
        throw e;
    }
    log.trace("Sending MQTT ACK for a received message ({})", message);
    if (message.getMessageType() == DEVICE_METHODS) {
        this.deviceMethod.start();
        this.deviceMethod.sendMessageAcknowledgement(messageId);
    } else if (message.getMessageType() == DEVICE_TWIN) {
        this.deviceTwin.start();
        this.deviceTwin.sendMessageAcknowledgement(messageId);
    } else {
        this.deviceMessaging.sendMessageAcknowledgement(messageId);
    }
    log.trace("MQTT ACK was sent for a received message so it has been removed from the messages to acknowledge list ({})", message);
    this.receivedMessagesToAcknowledge.remove(message);
    return true;
}
Also used : TransportException(com.microsoft.azure.sdk.iot.device.exceptions.TransportException)

Aggregations

TransportException (com.microsoft.azure.sdk.iot.device.exceptions.TransportException)46 Test (org.junit.Test)18 IOException (java.io.IOException)14 ProtocolException (com.microsoft.azure.sdk.iot.device.exceptions.ProtocolException)8 MqttException (org.eclipse.paho.client.mqttv3.MqttException)7 CountDownLatch (java.util.concurrent.CountDownLatch)4 Expectations (mockit.Expectations)4 NonStrictExpectations (mockit.NonStrictExpectations)4 Message (com.microsoft.azure.sdk.iot.device.Message)3 HttpsConnection (com.microsoft.azure.sdk.iot.device.transport.https.HttpsConnection)3 HttpsMethod (com.microsoft.azure.sdk.iot.device.transport.https.HttpsMethod)3 MqttMessaging (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttMessaging)3 Pair (org.apache.commons.lang3.tuple.Pair)3 IotHubConnectionString (com.microsoft.azure.sdk.iot.device.IotHubConnectionString)2 ModuleClientException (com.microsoft.azure.sdk.iot.device.exceptions.ModuleClientException)2 URISyntaxException (java.net.URISyntaxException)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 IotHubSSLContext (com.microsoft.azure.sdk.iot.deps.auth.IotHubSSLContext)1 IotHubAuthenticationProvider (com.microsoft.azure.sdk.iot.device.auth.IotHubAuthenticationProvider)1 SignatureProvider (com.microsoft.azure.sdk.iot.device.auth.SignatureProvider)1