# Mqtt5 client (JAVA) not receiving messages when not connected even using cleanStart false

**URL:** <https://community.hivemq.com/t/mqtt5-client-java-not-receiving-messages-when-not-connected-even-using-cleanstart-false/1942>\
**Category:** HiveMQ Client Library\
**Created:** [March 31, 2023, 11:52am UTC](https://community.hivemq.com/t/mqtt5-client-java-not-receiving-messages-when-not-connected-even-using-cleanstart-false/1942 "2023-03-31T11:52:58Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![fkmass](https://avatars.discourse-cdn.com/v4/letter/f/c57346/32.png) [@fkmass](https://community.hivemq.com/u/fkmass)\
**Post date:** [March 31, 2023, 11:52am UTC](https://community.hivemq.com/t/mqtt5-client-java-not-receiving-messages-when-not-connected-even-using-cleanstart-false/1942/1 "2023-03-31T11:52:58Z")

</div>

Hi. I need some help. I am using the Java Class client in order to build a subscriber client that must receive QOS 1 and QOS 2 messages send from a publisher.  
The client connects with an async connection, with .cleanStart(false).  
The connAck states .isSessionPresent() as true.  
Subscription is made with .qos(MqttQos.EXACTLY\_ONCE).

But either after ABORTING the client, or CLOSING the connection, whatever message is published with QOS 1 or 2 is lost and not received when connecting the subscriber client again.

I have been messing around with bigger or smaller sessionExpireInterval and keepAlive parameters with no progress.

I will appreciate any help on this subject.  
Thanks in advance!

Best regards,  
Freddy (from Uruguay)

```auto
type or paste code here

```

---

<div class="post-metadata">

**Author:** ![pglombardo](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.hivemq.com/pglombardo/32/588_2.png) [@pglombardo](https://community.hivemq.com/u/pglombardo)\
**Post date:** [April 6, 2023, 12:43pm UTC](https://community.hivemq.com/t/mqtt5-client-java-not-receiving-messages-when-not-connected-even-using-cleanstart-false/1942/2 "2023-04-06T12:43:47Z")

</div>

Hi Freddy from Uruguay!

A couple questions:

1. Which MQTT broker, edition and version are you using?
2. Could you confirm: Are you using the HiveMQ Java MQTT client 1.3.0?
3. How are you CLOSING the connection? With a DISCONNECT?
4. If the client is aborted/killed, do you reconnect with the same client ID?

For reference, there is also a [list of session persistence best practices available here](https://www.hivemq.com/blog/mqtt-essentials-part-7-persistent-session-queuing-messages/) that might be useful.

Let me know - it could be a couple of things but likely something small.

Best,  
Peter Giacomo Lombardo

---

<div class="post-metadata">

**Author:** ![fkmass](https://avatars.discourse-cdn.com/v4/letter/f/c57346/32.png) [@fkmass](https://community.hivemq.com/u/fkmass)\
**Post date:** [April 12, 2023, 6:25pm UTC](https://community.hivemq.com/t/mqtt5-client-java-not-receiving-messages-when-not-connected-even-using-cleanstart-false/1942/3 "2023-04-12T18:25:23Z")

</div>

Hi Peter!!!  
Thanks for the prontp response, sorry for the delayed answer. I wasn’t available last week.

1.- Broker is mosquitto version: 2.0.11-1ubuntu1  
2.- Client is 1.3.0  
3.- I did with disconnect, and also just aborting the program  
4.- Yes, always the same id

Thanks!

---

<div class="post-metadata">

**Author:** ![pglombardo](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.hivemq.com/pglombardo/32/588_2.png) [@pglombardo](https://community.hivemq.com/u/pglombardo)\
**Post date:** [April 13, 2023, 8:00am UTC](https://community.hivemq.com/t/mqtt5-client-java-not-receiving-messages-when-not-connected-even-using-cleanstart-false/1942/4 "2023-04-13T08:00:16Z")

</div>

Excellent - welcome back. From the Java MQTT client side, I’m sure this functionality works as it’s a core feature - and we support this extensively when using the HiveMQ broker. So it may likely be an Mosquitto related issue - or at least it’s worth validating.

In the [Mosquitto config](https://mosquitto.org/man/mosquitto-conf-5.html), double check the `max_queued_bytes` and `max_queued_messages` settings.

It sounds like you’re doing everything correctly in the client code, so unfortunately the only option is to use the process of elimination.

With your Mosquitto setup, attempt to replace the Java client by using `mosquitto_sub` or mqtt-cli and see if the same issue exists. This should tell us a lot - where the problem likely exists - in client code or in broker.

Let me know if I explained this well enough and if you discover something.

Best,  
Peter

---

<div class="post-metadata">

**Author:** ![fkmass](https://avatars.discourse-cdn.com/v4/letter/f/c57346/32.png) [@fkmass](https://community.hivemq.com/u/fkmass)\
**Post date:** [April 13, 2023, 1:47pm UTC](https://community.hivemq.com/t/mqtt5-client-java-not-receiving-messages-when-not-connected-even-using-cleanstart-false/1942/5 "2023-04-13T13:47:21Z")

</div>

Peter,  
I did what you suggested and used the command line cli. With proper parameters, everything works fine.  
So it’s a matter of propper programming.

Thanks for the help!

---

<div class="post-metadata">

**Author:** ![pglombardo](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.hivemq.com/pglombardo/32/588_2.png) [@pglombardo](https://community.hivemq.com/u/pglombardo)\
**Post date:** [April 13, 2023, 2:41pm UTC](https://community.hivemq.com/t/mqtt5-client-java-not-receiving-messages-when-not-connected-even-using-cleanstart-false/1942/6 "2023-04-13T14:41:46Z")

</div>

Ok great - Then if that’s the case, also make sure that you call subscribe before reconnecting. If not, the queued messages may be sent down before you have a handler setup to handle the incoming messages.

You could verify this by watching the MQTT traffic in tcpdump or Wireshark.

This solution was also covered in [this issue](https://github.com/hivemq/hivemq-mqtt-client/issues/527) and [this issue](https://github.com/hivemq/hivemq-mqtt-client/issues/521).

That is another common cause of lost messages but… If neither of these solve the issue for you, share your client code and I’ll try to reproduce the problem here locally.

Best,  
Peter

---

<div class="post-metadata">

**Author:** ![fkmass](https://avatars.discourse-cdn.com/v4/letter/f/c57346/32.png) [@fkmass](https://community.hivemq.com/u/fkmass)\
**Post date:** [April 18, 2023, 12:43pm UTC](https://community.hivemq.com/t/mqtt5-client-java-not-receiving-messages-when-not-connected-even-using-cleanstart-false/1942/7 "2023-04-18T12:43:22Z")

</div>

Peter, how should I subscribe before reconnect? Is that possible?  
If I don´t have an active connection, how can I send the subscribe command?

Thanks!

---

<div class="post-metadata">

**Author:** ![AaronTLFranz](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.hivemq.com/aarontlfranz/32/638_2.png) [@AaronTLFranz](https://community.hivemq.com/u/AaronTLFranz)\
**Post date:** [April 18, 2023, 3:58pm UTC](https://community.hivemq.com/t/mqtt5-client-java-not-receiving-messages-when-not-connected-even-using-cleanstart-false/1942/8 "2023-04-18T15:58:36Z")

</div>

Hello, @fkmass

When utilizing the Java client, you are able to submit a subscribe before a connect request. Peter has actually covered an implementation of this in the thread [here](https://github.com/hivemq/hivemq-mqtt-client/issues/527#issuecomment-1318778608).

This will prevent a case in which messages are being delivered to fulfill QoS 1/2 before the client has appropriately subscribed.

Let us know your thoughts, or if there are any further questions!

Best,  
Aaron from HiveMQ Team
