Subscribe to two different topics

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

//Subscribing to two topics : FAILED
.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:

.callback(e -> {})

Michael from the HiveMQ team


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:

                 .topicFilter(SHARE_GROUP_PREFIX + topic)

Thanks for your support, Michal

Hi @MichalB,

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


annotation isn’t the same as


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
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().

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

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

Michael from the HiveMQ team