MQTT client: how to configure message sending timeout?

Due to a severe bug that I recently encountered (Messages lost when using both MQTTv3 and MQTTv5 clients in async mode · Issue #618 · hivemq/hivemq-mqtt-client · GitHub), I am forced to use HiveMQ’s MQTT Java client in blocking mode. I would like to know how to make sure that send() does not block forever, but rather times out at some point. Here is what my code looks like now:

asyncClient
  .toBlocking()
  .publishWith()
  .topic(topic)
  .userProperties(...)
  .payload(message)
  .qos(EXACTLY_ONCE)
  .send();

In async mode, I could use CompletableFuture.get(timeout, timeUnit). In reactive mode, there is Flowable.timeout(timeout, timeUnit). What’s the equivalent in blocking mode?

Thanks.

        CompletableFuture<Void> publishFuture = CompletableFuture.runAsync(() -> {
            client.publishWith()
                    .topic("topic-1")
                    .payload(UTF_8.encode(payload))
                    .send();
        });

        try {
            publishFuture.get(10, TimeUnit.SECONDS);
            System.out.println("Publish completed successfully");
        } catch (Exception e) {
            if (null == e.getCause()){
                System.err.println("Publish operation timed out.");

            } else {
                System.err.println("Publish operation failed: " + e.getMessage());
            }
            client.disconnect();
        }

Thanks for your idea, Daria, I think I might go with it.

Could you please also have a look at the GitHub issue and pull request I posted?

Hi @DanielM

Please see the feedback on the PR you created. Do not hesitate to ask if you have further questions.

Dasha from HiveMQ Team