I'm using Azure Java SDK to learn to develop a Java IoT Edge module. I'm following this tutorial https://learn.microsoft.com/en-us/azure/iot-edge/tutorial-java-module.
So they explain how to send Message and receive Message, with the callback, like here :
// Send message:
client.sendEventAsync(msg, eventCallback, msg, App.OUTPUT_NAME);
// Receive message:
private static MessageCallbackMqtt msgCallback = new MessageCallbackMqtt();
client.setMessageCallback(App.INPUT_NAME, msgCallback, client);
protected static class MessageCallbackMqtt implements MessageCallback {
private int counter = 0;
@Override
public IotHubMessageResult execute(Message msg, Object context) {
System.out.println(String.format("Received message %d: %s", this.counter, new String(msg.getBytes(), Message.DEFAULT_IOTHUB_MESSAGE_CHARSET)));
}
}
So as you can see, the sent and received messages are of type Message.
How can I send Integer for example ? I see I can convert it into String with
String msgString = new String(msg.getBytes(), Message.DEFAULT_IOTHUB_MESSAGE_CHARSET);
But what about Integer ?
Also, in the sendEventAsync method what is the third parameter msg ? In the documentation they say it's Object callbackContext
but I don't understand what it is and why we use the msg
as this parameter.
Thank you for your answer
Sending an Integer as binary message is quite easy:
Message msg = new Message(ByteBuffer.allocate(4).putInt(1695609641).array());
The third parameter in the sendEventAsync method is the context object for the callback method in the second parameter with the following method signature
void execute(IotHubStatusCode responseStatus, Object callbackContext);
So passing in the message in the third parameter gives you access to the message in the callback method