This works well to initiate connection and publish the first message:
MemoryPersistence persistence = new MemoryPersistence();
client = new MqttClient("tcp://" + IrisProperties.MQTT_SERVER_ADDRESS,
IrisProperties.MQTT_USERNAME,persistence);
MqttConnectOptions connOpts = new MqttConnectOptions();
connOpts.setConnectionTimeout(1);
connOpts.setUserName(IrisProperties.MQTT_USERNAME);
connOpts.setPassword(IrisProperties.MQTT_PASSWORD.toCharArray());
connOpts.setCleanSession(true);
client.connect(connOpts);
This connection publishes more than 100 messages per second with no problem with this lines:
MqttMessage message = new MqttMessage(content.getBytes());
message.setQos(qos);
client.publish(topic, message);
BUT after a while (in a few minutes) the 3rd line of this method
sampleClient.publish(topic, message);
blocks the thread forever even I set the timeout as 1 second.
I use Moquette as MQTT Broker with websockets.
Use
client.setTimeToWait(timeToWaitInMillis);
By default this time is -1, so this Blocks the current thread until the action has completed.The timeout specifies the maximum time it will block the thread for.