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)); } });
The code calls the mqttAsyncClient.setCallback
method with an anonymous subclass of the MqttCallback
class as an argument. The setCallback
method requires...