Search in sources :

Example 1 with MessageImpl

use of org.apache.qpid.proton.message.impl.MessageImpl in project azure-iot-sdk-java by Azure.

the class AmqpsTransport method sendMessages.

/**
     * <p>
     * Sends all messages from the waiting list, one at a time. If a previous
     * send attempt had failed, the function will attempt to resend the messages
     * in the previous attempt.
     * </p>
     *
     * @throws IOException if the server could not be reached.
     * @throws IllegalStateException if the transport has not been opened or is
     * already closed.
     */
public void sendMessages() throws IOException, IllegalStateException {
    // Codes_SRS_AMQPSTRANSPORT_15_012: [If the AMQPS session is closed, the function shall throw an IllegalStateException.]
    if (this.state == State.CLOSED) {
        logger.LogError("Cannot send messages when the AMQPS transport is closed, method name is %s ", logger.getMethodName());
        throw new IllegalStateException("Cannot send messages when the AMQPS transport is closed.");
    }
    // Codes_SRS_AMQPSTRANSPORT_15_013: [If there are no messages in the waiting list, the function shall return.]
    if (this.waitingMessages.size() <= 0) {
        return;
    }
    Collection<IotHubOutboundPacket> failedMessages = new ArrayList<>();
    // Codes_SRS_AMQPSTRANSPORT_15_014: [The function shall attempt to send every message on its waiting list, one at a time.]
    while (!this.waitingMessages.isEmpty()) {
        logger.LogInfo("Get the message from waiting message queue to be sent to IoT Hub, method name is %s ", logger.getMethodName());
        IotHubOutboundPacket packet = this.waitingMessages.remove();
        Message message = packet.getMessage();
        // Codes_SRS_AMQPSTRANSPORT_15_015: [The function shall skip messages with null or empty body.]
        if (message != null && message.getBytes().length > 0) {
            // with the MESSAGE_EXPIRED status and add it to the callback list.]
            if (message.isExpired()) {
                logger.LogInfo("Creating a callback for the expired message with MESSAGE_EXPIRED status, method name is %s ", logger.getMethodName());
                IotHubCallbackPacket callbackPacket = new IotHubCallbackPacket(IotHubStatusCode.MESSAGE_EXPIRED, packet.getCallback(), packet.getContext());
                this.callbackList.add(callbackPacket);
            } else {
                logger.LogInfo("Converting the IoT Hub message into AmqpsMessage, method name is %s ", logger.getMethodName());
                // Codes_SRS_AMQPSTRANSPORT_15_036: [The function shall create a new Proton message from the IoTHub message.]
                MessageImpl protonMessage = iotHubMessageToProtonMessage(message);
                // Codes_SRS_AMQPSTRANSPORT_15_037: [The function shall attempt to send the Proton message to IoTHub using the underlying AMQPS connection.]
                Integer sendHash = connection.sendMessage(protonMessage);
                // Codes_SRS_AMQPSTRANSPORT_15_016: [If the sent message hash is valid, it shall be added to the in progress map.]
                if (sendHash != -1) {
                    this.inProgressMessages.put(sendHash, packet);
                } else // Codes_SRS_AMQPSTRANSPORT_15_017: [If the sent message hash is not valid, it shall be buffered to be sent in a subsequent attempt.]
                {
                    failedMessages.add(packet);
                }
            }
        }
    }
    this.waitingMessages.addAll(failedMessages);
}
Also used : IotHubCallbackPacket(com.microsoft.azure.sdk.iot.device.transport.IotHubCallbackPacket) IotHubOutboundPacket(com.microsoft.azure.sdk.iot.device.transport.IotHubOutboundPacket) MessageImpl(org.apache.qpid.proton.message.impl.MessageImpl)

Example 2 with MessageImpl

use of org.apache.qpid.proton.message.impl.MessageImpl in project azure-iot-sdk-java by Azure.

the class AmqpsTransportTest method sendMessagesAddsUserPropertiesToProtonApplicationProperties.

// Tests_SRS_AMQPSTRANSPORT_15_038: [The function shall add all user properties to the application properties of the Proton message.]
@Test
public void sendMessagesAddsUserPropertiesToProtonApplicationProperties(@Mocked final Message mockMsg, @Mocked final IotHubEventCallback mockCallback, @Mocked final IotHubOutboundPacket mockPacket) throws IOException {
    final Map<String, Object> context = new HashMap<>();
    final byte[] messageBytes = new byte[] { 1, 2 };
    final MessageProperty[] iotHubMessageProperties = new MessageProperty[] { new MessageProperty("key1", "value1"), new MessageProperty("key2", "value2") };
    final Map<String, String> userProperties = new HashMap<>(2);
    userProperties.put(iotHubMessageProperties[0].getName(), iotHubMessageProperties[0].getValue());
    userProperties.put(iotHubMessageProperties[1].getName(), iotHubMessageProperties[1].getValue());
    new NonStrictExpectations() {

        {
            new AmqpsIotHubConnection(mockConfig, false);
            result = mockConnection;
            new IotHubOutboundPacket(mockMsg, mockCallback, context);
            result = mockPacket;
            mockPacket.getMessage();
            result = mockMsg;
            mockMsg.getBytes();
            result = messageBytes;
            new MessageImpl();
            result = mockProtonMessage;
            mockMsg.getProperties();
            result = iotHubMessageProperties;
            mockConnection.sendMessage(mockProtonMessage);
            result = 1;
            new ApplicationProperties(userProperties);
        }
    };
    AmqpsTransport transport = new AmqpsTransport(mockConfig, false);
    transport.open();
    transport.addMessage(mockMsg, mockCallback, context);
    transport.sendMessages();
    new Verifications() {

        {
            new IotHubOutboundPacket(mockMsg, mockCallback, context);
            times = 1;
            mockPacket.getMessage();
            times = 1;
            mockConnection.sendMessage(mockProtonMessage);
            times = 1;
            new ApplicationProperties(userProperties);
            times = 1;
            mockProtonMessage.setApplicationProperties((ApplicationProperties) any);
            times = 1;
        }
    };
}
Also used : AmqpsTransport(com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsTransport) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) AmqpsIotHubConnection(com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsIotHubConnection) IotHubOutboundPacket(com.microsoft.azure.sdk.iot.device.transport.IotHubOutboundPacket) ApplicationProperties(org.apache.qpid.proton.amqp.messaging.ApplicationProperties) MessageImpl(org.apache.qpid.proton.message.impl.MessageImpl) Test(org.junit.Test)

Example 3 with MessageImpl

use of org.apache.qpid.proton.message.impl.MessageImpl in project azure-iot-sdk-java by Azure.

the class AmqpsTransportTest method handleMessagePutsMessageBackIntoQueueIfCannotSendResultBackToServer.

// Tests_SRS_AMQPSTRANSPORT_15_028: [If the result could not be sent to IoTHub, the message shall be put back in the received messages queue to be processed again.]
// Tests_SRS_AMQPSTRANSPORT_15_028: [If the result could not be sent to IoTHub, the message shall be put back in the received messages queue to be processed again.]
@Test
public void handleMessagePutsMessageBackIntoQueueIfCannotSendResultBackToServer() throws IOException {
    new NonStrictExpectations() {

        {
            new AmqpsIotHubConnection(mockConfig, false);
            result = mockConnection;
            mockConfig.getMessageCallback();
            result = mockMessageCallback;
            mockMessageCallback.execute((Message) any, any);
            result = IotHubMessageResult.COMPLETE;
            mockConnection.sendMessageResult(mockAmqpsMessage, IotHubMessageResult.COMPLETE);
            result = false;
        }
    };
    new MockUp<AmqpsTransport>() {

        @Mock
        Message protonMessageToIoTHubMessage(MessageImpl protonMessage) {
            return new Message();
        }
    };
    AmqpsTransport transport = new AmqpsTransport(mockConfig, false);
    transport.open();
    Queue<AmqpsMessage> receivedMessages = new LinkedBlockingQueue<>();
    receivedMessages.add(mockAmqpsMessage);
    receivedMessages.add(mockAmqpsMessage);
    Deencapsulation.setField(transport, "receivedMessages", receivedMessages);
    transport.handleMessage();
    Queue<AmqpsMessage> receivedTransportMessages = Deencapsulation.getField(transport, "receivedMessages");
    Assert.assertTrue(receivedTransportMessages.size() == 2);
    new Verifications() {

        {
            mockMessageCallback.execute((Message) any, any);
            times = 1;
            mockConnection.sendMessageResult(mockAmqpsMessage, IotHubMessageResult.COMPLETE);
            times = 1;
        }
    };
    Assert.assertTrue(receivedTransportMessages.size() == 2);
}
Also used : AmqpsIotHubConnection(com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsIotHubConnection) AmqpsMessage(com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsMessage) AmqpsTransport(com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsTransport) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) MessageImpl(org.apache.qpid.proton.message.impl.MessageImpl) AmqpsMessage(com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsMessage) Test(org.junit.Test)

Example 4 with MessageImpl

use of org.apache.qpid.proton.message.impl.MessageImpl in project azure-iot-sdk-java by Azure.

the class AmqpsTransportTest method handleMessageConsumesAMessage.

// Tests_SRS_AMQPSTRANSPORT_15_023: [The function shall attempt to consume a message from the IoT Hub.]
// Tests_SRS_AMQPSTRANSPORT_15_026: [The function shall invoke the callback on the message.]
// Tests_SRS_AMQPSTRANSPORT_15_027: [The function shall return the message result (one of COMPLETE, ABANDON, or REJECT) to the IoT Hub.]
@Test
public void handleMessageConsumesAMessage() throws IOException {
    new NonStrictExpectations() {

        {
            new AmqpsIotHubConnection(mockConfig, false);
            result = mockConnection;
            mockConfig.getMessageCallback();
            result = mockMessageCallback;
            mockMessageCallback.execute((Message) any, any);
            result = IotHubMessageResult.COMPLETE;
            mockConnection.sendMessageResult(mockAmqpsMessage, IotHubMessageResult.COMPLETE);
            result = true;
        }
    };
    new MockUp<AmqpsTransport>() {

        @Mock
        Message protonMessageToIoTHubMessage(MessageImpl protonMessage) {
            return new Message();
        }
    };
    AmqpsTransport transport = new AmqpsTransport(mockConfig, false);
    transport.open();
    Queue<AmqpsMessage> receivedMessages = new LinkedBlockingQueue<>();
    receivedMessages.add(mockAmqpsMessage);
    receivedMessages.add(mockAmqpsMessage);
    Deencapsulation.setField(transport, "receivedMessages", receivedMessages);
    transport.handleMessage();
    Queue<AmqpsMessage> receivedTransportMessages = Deencapsulation.getField(transport, "receivedMessages");
    new Verifications() {

        {
            mockMessageCallback.execute((Message) any, any);
            times = 1;
            mockConnection.sendMessageResult(mockAmqpsMessage, IotHubMessageResult.COMPLETE);
            times = 1;
        }
    };
    Assert.assertTrue(receivedTransportMessages.size() == 1);
}
Also used : AmqpsIotHubConnection(com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsIotHubConnection) AmqpsMessage(com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsMessage) AmqpsTransport(com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsTransport) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) MessageImpl(org.apache.qpid.proton.message.impl.MessageImpl) AmqpsMessage(com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsMessage) Test(org.junit.Test)

Example 5 with MessageImpl

use of org.apache.qpid.proton.message.impl.MessageImpl in project azure-iot-sdk-java by Azure.

the class AmqpsTransport method iotHubMessageToProtonMessage.

/**
     * Creates a proton message from the IoTHub message.
     * @param message the IoTHub input message.
     * @return the proton message.
     */
private MessageImpl iotHubMessageToProtonMessage(com.microsoft.azure.sdk.iot.device.Message message) {
    logger.LogInfo("Started converting IoT Hub message into AmpqsMessage, method name is %s ", logger.getMethodName());
    MessageImpl outgoingMessage = (MessageImpl) Proton.message();
    logger.LogInfo("Content of message is %s, method name is %s ", new String(message.getBytes(), Message.DEFAULT_IOTHUB_MESSAGE_CHARSET), logger.getMethodName());
    Properties properties = new Properties();
    if (message.getMessageId() != null) {
        properties.setMessageId(message.getMessageId());
    }
    outgoingMessage.setProperties(properties);
    // Codes_SRS_AMQPSTRANSPORT_15_038: [The function shall add all user properties to the application properties of the Proton message.]
    if (message.getProperties().length > 0) {
        Map<String, String> userProperties = new HashMap<>(message.getProperties().length);
        for (MessageProperty messageProperty : message.getProperties()) {
            if (!MessageProperty.RESERVED_PROPERTY_NAMES.contains(messageProperty.getName())) {
                userProperties.put(messageProperty.getName(), messageProperty.getValue());
            }
        }
        ApplicationProperties applicationProperties = new ApplicationProperties(userProperties);
        outgoingMessage.setApplicationProperties(applicationProperties);
    }
    Binary binary = new Binary(message.getBytes());
    Section section = new Data(binary);
    outgoingMessage.setBody(section);
    logger.LogInfo("Started converting IoT Hub message into AmpqsMessage, method name is %s ", logger.getMethodName());
    return outgoingMessage;
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ApplicationProperties(org.apache.qpid.proton.amqp.messaging.ApplicationProperties) Data(org.apache.qpid.proton.amqp.messaging.Data) Binary(org.apache.qpid.proton.amqp.Binary) Properties(org.apache.qpid.proton.amqp.messaging.Properties) ApplicationProperties(org.apache.qpid.proton.amqp.messaging.ApplicationProperties) MessageImpl(org.apache.qpid.proton.message.impl.MessageImpl) Section(org.apache.qpid.proton.amqp.messaging.Section)

Aggregations

MessageImpl (org.apache.qpid.proton.message.impl.MessageImpl)5 AmqpsIotHubConnection (com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsIotHubConnection)3 AmqpsTransport (com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsTransport)3 Test (org.junit.Test)3 IotHubOutboundPacket (com.microsoft.azure.sdk.iot.device.transport.IotHubOutboundPacket)2 AmqpsMessage (com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsMessage)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)2 ApplicationProperties (org.apache.qpid.proton.amqp.messaging.ApplicationProperties)2 IotHubCallbackPacket (com.microsoft.azure.sdk.iot.device.transport.IotHubCallbackPacket)1 HashMap (java.util.HashMap)1 Binary (org.apache.qpid.proton.amqp.Binary)1 Data (org.apache.qpid.proton.amqp.messaging.Data)1 Properties (org.apache.qpid.proton.amqp.messaging.Properties)1 Section (org.apache.qpid.proton.amqp.messaging.Section)1