Search in sources :

Example 36 with IotHubOutboundPacket

use of com.microsoft.azure.sdk.iot.device.transport.IotHubOutboundPacket in project azure-iot-sdk-java by Azure.

the class MqttTransport method sendMessages.

/**
     * <p>
     * Sends all messages on the transport queue, one at a time. If a previous
     * send attempt had failed, the function will attempt to resend the messages
     * in the previous attempt.
     * </p>
     * If one has not already been created, the function will initialize an
     * MQTT connection with the IoT Hub specified in the configuration.
     *
     * @throws IllegalStateException if the transport has not been opened or is closed.
     */
public void sendMessages() throws IllegalStateException {
    synchronized (sendMessagesLock) {
        // the function shall throw an IllegalStateException.]
        if (this.state == State.CLOSED) {
            throw new IllegalStateException("MQTT transport is closed.");
        }
        if (this.waitingList.size() <= 0) {
            return;
        }
        // on its waiting list, one at a time.]
        while (!this.waitingList.isEmpty()) {
            IotHubOutboundPacket packet = this.waitingList.remove();
            try {
                IotHubStatusCode status = this.mqttIotHubConnection.sendEvent(packet.getMessage());
                // Codes_SRS_MQTTTRANSPORT_15_010: [For each message being sent, the function shall add
                // the IoT Hub status code along with the callback and context to the callback list.]
                IotHubCallbackPacket callbackPacket = new IotHubCallbackPacket(status, packet.getCallback(), packet.getContext());
                this.callbackList.add(callbackPacket);
            }// shall be buffered to be sent again next time.]
             catch (IllegalStateException e) {
                this.waitingList.add(packet);
            }
        }
    }
}
Also used : IotHubCallbackPacket(com.microsoft.azure.sdk.iot.device.transport.IotHubCallbackPacket) IotHubOutboundPacket(com.microsoft.azure.sdk.iot.device.transport.IotHubOutboundPacket)

Example 37 with IotHubOutboundPacket

use of com.microsoft.azure.sdk.iot.device.transport.IotHubOutboundPacket in project azure-iot-sdk-java by Azure.

the class IotHubOutboundPacketTest method getMessageReturnsMessage.

// Tests_SRS_IOTHUBOUTBOUNDPACKET_11_001: [The constructor shall save the message, callback, and callback context.]
// Tests_SRS_IOTHUBOUTBOUNDPACKET_11_002: [The function shall return the message given in the constructor.]
@Test
public void getMessageReturnsMessage() {
    final Map<String, Object> context = new HashMap<>();
    IotHubOutboundPacket packet = new IotHubOutboundPacket(mockMsg, mockCallback, context);
    Message testMsg = packet.getMessage();
    final Message expectedMsg = mockMsg;
    assertThat(testMsg, is(expectedMsg));
}
Also used : Message(com.microsoft.azure.sdk.iot.device.Message) HashMap(java.util.HashMap) IotHubOutboundPacket(com.microsoft.azure.sdk.iot.device.transport.IotHubOutboundPacket) Test(org.junit.Test)

Aggregations

IotHubOutboundPacket (com.microsoft.azure.sdk.iot.device.transport.IotHubOutboundPacket)37 Test (org.junit.Test)25 HashMap (java.util.HashMap)17 AmqpsTransport (com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsTransport)15 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)14 AmqpsIotHubConnection (com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsIotHubConnection)12 IotHubCallbackPacket (com.microsoft.azure.sdk.iot.device.transport.IotHubCallbackPacket)10 AmqpsMessage (com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsMessage)5 Queue (java.util.Queue)5 MqttTransport (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttTransport)4 MqttIotHubConnection (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttIotHubConnection)3 IotHubEventCallback (com.microsoft.azure.sdk.iot.device.IotHubEventCallback)2 IotHubResponseCallback (com.microsoft.azure.sdk.iot.device.IotHubResponseCallback)2 LinkedBlockingDeque (java.util.concurrent.LinkedBlockingDeque)2 MessageImpl (org.apache.qpid.proton.message.impl.MessageImpl)2 Message (com.microsoft.azure.sdk.iot.device.Message)1 Map (java.util.Map)1 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)1 SizeLimitExceededException (javax.naming.SizeLimitExceededException)1 ApplicationProperties (org.apache.qpid.proton.amqp.messaging.ApplicationProperties)1