How to get the QoS of a client in HiveMQ Client?

空扰寡人 提交于 2019-12-11 15:28:53

问题


I'm working with HiveMQ Client and I wanted to know if there was a way to get the quality of service (QoS) that a client is subscribing with (in terms of a specific topic or in general)? I would be looking for a method I could invoke on a client like so:

    Mqtt5BlockingClient subscriber = Mqtt5Client.builder()
            .identifier(UUID.randomUUID().toString()) // the unique identifier of the MQTT client
            .serverHost("localhost") 
            .serverPort(1883) 
            .buildBlocking();

subscriber.getQoS("topic") // returns the QoS of the subscriber is subscribing to the given topic 

I would just want this information so I could print in to the console.


回答1:


I think you have to read more about MQTT concepts. The Quality of Service (QoS) level is an agreement between a sender and receiver of a message regarding the guarantees of delivering a message. Therefore, the QoS is used in the publish() and subscribe() methods not the connect().

This is the scenario:

1. Connect:
You have to connect your client to any broker with username/password. Every mqtt library has a connect() method. In this step, you have not specified qos yet.
After successful connection (every mqtt library has a callback for the connect method) and you can publish or subscribe to any desired (or allowed) topics.
Example:
Eclipse Paho library:

IMqttToken token = clientPhone.connect();

HiveMQ Library:

client.connect();
//or
client.connectWith().keepAlive(10).send(); 
//or
Mqtt5Connect connectMessage = Mqtt5Connect.builder().keepAlive(10).build();
client.connect(connectMessage);

2. Publish:
When you want to publish() a message, you have to specify a qos, so that the broker will respond to the client according with this qos:

Qos=0:
Client  ---- Publish method ----> broker
Qos=1:
Client  ---- Publish method  ----> broker
Client <---- PubAck callback ----  broker
Qos=2:
Client  ---- Publish method   ----> broker
Client <---- PubRec callback  ----  broker
Client  ---- PubRel method    ----> broker
Client <---- PubComp callback ----  broker

Example:

Eclipse Paho library:

IMqttDeliveryToken tokenPub = clientPhone.publish(topicPub, message);

HiveMQ Library:

client.publishWith()
        .topic("test/topic")
        .qos(MqttQos.AT_LEAST_ONCE)
        .payload("payload".getBytes())
        .send();
//or:
Mqtt5Publish publishMessage = Mqtt5Publish.builder()
        .topic("test/topic")
        .qos(MqttQos.AT_LEAST_ONCE)
        .payload("payload".getBytes())
        .build();
client.publish(publishMessage);

3. Subscribe:
A SUBSCRIBE message can contain an arbitrary number of subscriptions for a client. Each subscription is a pair of a topic and QoS level. The topic in the subscribe message can also contain wildcards, which makes it possible to subscribe to certain topic patterns. If there are overlapping subscriptions for one client, the highest QoS level for that topic wins and will be used by the broker for delivering the message.
Example:

Eclipse Paho library:

IMqttToken subToken = MqttAndroidClientInstance.subscribe(topics, qos);

HiveMQ Library:

client.subscribeWith().topicFilter("test/topic").qos(MqttQos.EXACTLY_ONCE).send();
//or:
Mqtt5Subscribe subscribeMessage = Mqtt5Subscribe.builder()
        .topicFilter("test/topic")
        .qos(MqttQos.EXACTLY_ONCE)
        .build();
client.subscribe(subscribeMessage);

Edit(1):
A mqtt client have to use the following parameters, if wants to receive the already subscribed topics after reconnecting:
A- connect with cleanSession false.
B- Subscribe with QOS>0.



来源:https://stackoverflow.com/questions/57116316/how-to-get-the-qos-of-a-client-in-hivemq-client

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!