Working with asynchronous notifications and callbacks in Java
The following lines continue the declaration of the public static main
method. The code file for the sample is included in the mqtt_essentials_gaston_hillar_04
folder, in the Java01/src/Main.java
file:
mqttAsyncClient.setCallback(new MqttCallback() { @Override public void connectionLost(Throwable cause) { cause.printStackTrace(); } @Override public void deliveryComplete(IMqttDeliveryToken token) { } @Override public void messageArrived(String topic, MqttMessage message) throws Exception { if (!topic.equals(topic_for_led01)) { return; } String messageText = new String(message.getPayload(), "UTF-8"); System.out.println( String.format("Topic: %s. Payload: %s", topic, messageText)); } }); ...
Get MQTT Essentials - A Lightweight IoT Protocol now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.