I am using hivemq-mqtt-client-1.3.0.jar and creating a hivemq mqtt5 client. I have specified a keep alive time of 60 secs from client side.
String id = "1";
Mqtt5ClientBuilder builder = Mqtt5Client.builder()
.identifier(id)
.serverHost("localhost")
.serverPort(1883)
.addConnectedListener(context -> System.out.println("Connected Now"))
.addDisconnectedListener(context -> System.out.println("Disconnected Connected Now"));
client = builder.build();
client.toBlocking().connectWith().cleanStart(false).keepAlive(keepAlive).send();
subscribe(id, "1");
Once I connect to hiveMq with the above client, I get a connected log from event.txt file as shown below.
2023-01-03 12:56:39,428 - Client ID: 1, IP: 127.0.0.1, Clean Start: false, Session Expiry: 0 connected.
Now i disconnect my client using below code after 10 secs.
Thread.sleep(10*1000);
client.toBlocking().disconnect();
My expectation is that even though the client disconnected after 10 sec, the broker should keep the connection alive for another 50 sec as keep alive is 60 sec.
But as soon as i disconnect the client, I get disconnected message from broker too, which means keep alive is not working.
2023-01-03 12:56:49,482 - Client ID: 1, IP: 127.0.0.1 disconnected gracefully.
Also I have attached below hivemq broker config.xml file.
<hivemq>
<listeners>
<tcp-listener>
<port>1883</port>
<bind-address>0.0.0.0</bind-address>
</tcp-listener>
<mqtt>
<keep-alive>
<allow-unlimited>false</allow-unlimited>
<max-keep-alive>60</max-keep-alive>
</keep-alive>
</mqtt>
</listeners>
<anonymous-usage-statistics>
<enabled>true</enabled>
</anonymous-usage-statistics>
</hivemq>
Please suggest me what I am doing wrong here and any suggestions would be deeply appreciated.
UPDATE******
Now I am going to subscribe to a topic without without sending disconnect message from my client. Pls refer above code again, I have added a function call to subscribe to topic.
public static void subscribe(String id, String cid) {
try {
client.toAsync().subscribeWith()
.topicFilter("TEMP/" id "/" cid).callback(publish -> {
String message = new String(publish.getPayloadAsBytes(), StandardCharsets.UTF_8);
System.out.println("Received message on topic " publish.getTopic() ": " message);
}).send();
System.out.println("Subscribing to " "TEMP/" id "/" cid);
Now after this I dont receive any messages on this topic. My keep alive time is 60 secs as shown in conf above, so broker should disconnect my client after 60*1.5 = 90 secs. But even after 90 secs, i dont see my client getting disconnected. Also broker does not send any PINGREQ to client too.
Kindly advice what I should do.
CodePudding user response:
Your understanding is wrong, the broker and client is behaving correctly.
If you call disconnect()
then the client will send an explicit DISCONNECT packet to the broker telling it that it has gone. So it will shut everything down.
Keep alive is there for if the network path between the client and broker is broken in the middle, so no packets can flow.