HiveMQ

Subscribe to two different topics

How to subscribe to two different topics using MQTT java client?.
//Example code with single topic sub: SUCCESS
mqtt3AsyncClient.subscribeWith().topicFilter(“tenant1/topic”)
.qos(MqttQos.AT_LEAST_ONCE).callback(e -> {}).send();

//Subscribing to two topics : FAILED
mqtt3AsyncClient.subscribeWith().topicFilter(“tenant1/topic”).topicFilter(“tenant2/topic”)
.qos(MqttQos.AT_LEAST_ONCE).callback(e -> {}).send();

Hi @dileepmandapam,

in your example with two topics you are overriding “tenant1/topic” with “tenant2/topic”.

One way to subscribe to two topics (or more) is:

mqtt3AsyncClient.subscribeWith()
.addSubscription().topicFilter(“tenant1/topic”).qos(MqttQos.AT_LEAST_ONCE).applySubscription()
.addSubscription().topicFilter(“tenant2/topic”).qos(MqttQos.AT_LEAST_ONCE).applySubscription()
.callback(e -> {})
.send();

Greetings,
Michael from the HiveMQ team

Hello,

I have similar issue with hivemq-mqtt-client version 1.1.3. :

  1. @michael_w .applySubscription() has @DoNotImplement annotation - deprecated?
    How to do this right way?

  2. How to build SUBSCRIBE dynamically depending on the number of topics along with qos?
    I know, vanilla issue - sorry for that :wink:

             client3.subscribeWith()
                 .topicFilter(SHARE_GROUP_PREFIX + topic)
                 .qos(MqttQos.AT_LEAST_ONCE)
                 .callback(this::acceptMessage3)
                 .executor(Executors.newFixedThreadPool(30))
                 .send();
    

Thanks for your support, Michal

Hi @MichalB,

about 1.) can you elaborate what you mean? The

@DoNotImplement

annotation isn’t the same as

@Deprecated

It means that you should not implement the interface for custom logic to avoid unwanted behaviour, instead you should use the provided implementation (in this case the builder).

For the second point, for an out of the box solution you will need to wait for the 1.2 release of the client (see https://github.com/hivemq/hivemq-mqtt-client/issues/170).
As I also needed this feature I can give you a workaround (adapt as needed) that you could use in the meantime:

    // connect client
    
    final List<String> topics = List.of("test", "topic", "asdf", "qwer");

    final int subCount = topics.size();

    final Mqtt3SubscribeBuilder builder = Mqtt3Subscribe.builder();
    
    Mqtt3Subscribe beeSubscribe = null;

    for (int i = 0; i < subCount; i++) {
        final String topic = topics.get(i);

        final Mqtt3Subscription beeSubscription = Mqtt3Subscription.builder().
                topicFilter(topic).qos(MqttQos.AT_LEAST_ONCE).build();

        if (i + 1 == subCount) {
            // build the subscribe if it is the last loop iteration
            beeSubscribe = builder.addSubscription(beeSubscription).build();
        } else {
            builder.addSubscription(beeSubscription);
        }
    }

    if (beeSubscribe != null) {
        client.subscribe(beeSubscribe).get();
    } else {
        throw new NullPointerException("Should not happen");
    }

Greetings,
Michael from the HiveMQ team