# Subscribe to two different topics

**URL:** <https://community.hivemq.com/t/subscribe-to-two-different-topics/137>\
**Category:** HiveMQ Client Library\
**Created:** [January 9, 2020, 4:55pm UTC](https://community.hivemq.com/t/subscribe-to-two-different-topics/137 "2020-01-09T16:55:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dileepmandapam](https://avatars.discourse-cdn.com/v4/letter/d/a183cd/32.png) [@dileepmandapam](https://community.hivemq.com/u/dileepmandapam)\
**Post date:** [January 9, 2020, 4:55pm UTC](https://community.hivemq.com/t/subscribe-to-two-different-topics/137/1 "2020-01-09T16:55:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![michael\_w](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.hivemq.com/michael_w/32/200_2.png) [@michael\_w](https://community.hivemq.com/u/michael_w)\
**Post date:** [January 10, 2020, 9:40am UTC](https://community.hivemq.com/t/subscribe-to-two-different-topics/137/2 "2020-01-10T09:40:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![MichalB](https://avatars.discourse-cdn.com/v4/letter/m/58956e/32.png) [@MichalB](https://community.hivemq.com/u/MichalB)\
**Post date:** [February 12, 2020, 3:44pm UTC](https://community.hivemq.com/t/subscribe-to-two-different-topics/137/3 "2020-02-12T15:44:39Z")

</div>

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 😉

Thanks for your support, Michal

---

<div class="post-metadata">

**Author:** ![michael\_w](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.hivemq.com/michael_w/32/200_2.png) [@michael\_w](https://community.hivemq.com/u/michael_w)\
**Post date:** [February 12, 2020, 6:59pm UTC](https://community.hivemq.com/t/subscribe-to-two-different-topics/137/4 "2020-02-12T18:59:14Z")

</div>

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 [Add methods consuming Collection for Subscribe/UnsubscribeBuilder · Issue #170 · hivemq/hivemq-mqtt-client · GitHub](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
