When I try to use the code below to connect to the mosquitto broker, as you know, connecting to the broker can take several seconds / minutes, and during this time, when the button is pressed to connect, it remains pressed until the connection and when the connection established the button, released it to normal. As far as I know, there are two ways to connect the client using the paho java API , blocking method and unblocking method . my question is how to use unblocking method ? beow is my attempt to use the lock method
Code_1
//mqttFactory public final class MQTTClientFactory { public static MqttClient newClient(String ip, int port, String clientID) throws MqttException { String serverURI = formURI(ip, port); MqttClient client = new MqttClient(serverURI, clientID).; return client; } MqttConnectOptions opts = getClientOptions(); client = MQTTClientFactory.newClient(broker, port, clientID); if (client != null) { System.out.println("Client is not Null"); client.setCallback(AsynchCallBack); if (opts != null) { client.connectWithResult(opts).setActionCallback(synchCallBack); if (client.isConnected()) { System.out.println("Client CONNECTED."); } } }
source share