How to use the MqttClient lock method

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."); } } } 
0
source share
1 answer

which button? establishing a connection almost instantly.

There are asynchronous versions of mqtt. Code samples for this. If you want to do synchronous non-blocking. You can run it in another thread.

0
source

Source: https://habr.com/ru/post/1207822/


All Articles