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);
}
}
}
}
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));
}
Aggregations