Java Magazine, March/April 2017
ORACLE COM JAVAMAGAZINE MARCH APRIL 2017 51 iot options setPassword replace with your password toCharArray Replace with ssl and work with TLS SSL best practices in a production environment memoryPersistence new MemoryPersistence String serverURI tcp iot eclipse org 1883 clientId MqttAsyncClient generateClientId client new MqttAsyncClient serverURI clientId memoryPersistence I want to use this instance as the callback client setCallback this connectToken client connect options null this catch MqttException e e printStackTrace public boolean isConnected return client null client isConnected @ Override public void connectionLost Throwable cause The MQTT client lost the connection cause printStackTrace The line that calls the setCallback method uses this as an argument because I use the actual instance as the callback that will execute specific methods when some asynchronous events occur The setCallback method requires an argument of the MqttCallback type The Drone class implements the MqttCallback interface that requires the following three methods connectionLost messageArrived and deliveryComplete Ill get back to these methods later It is very important to call the setCallback method before establishing the connection with the MQTT broker The line that calls the client connect method specifies this as the last argument because I will also use the actual instance as the callback that will execute specific methods when some asynchronous events related to the connection occur The fourth argument for the connect method requires an argument of the IMqttActionListener type The Drone class implements the IMqttActionListener interface that requires these two methods onSuccess and onFailure @ Override public void onSuccess IMqttToken asyncActionToken if asyncActionToken equals connectToken System out println String format s successfully connected name try subscribeToken client subscribe TOPIC QUALITY_ OF_ SERVICE null this catch MqttException e e printStackTrace else if asyncActionToken equals subscribeToken System out println String format
You must have JavaScript enabled to view digital editions.