# No connect after disconnect

**URL:** <https://community.hivemq.com/t/no-connect-after-disconnect/1427>\
**Category:** Uncategorized\
**Created:** [September 11, 2022, 6:44pm UTC](https://community.hivemq.com/t/no-connect-after-disconnect/1427 "2022-09-11T18:44:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pvittali](https://avatars.discourse-cdn.com/v4/letter/p/f19dbf/32.png) [@pvittali](https://community.hivemq.com/u/pvittali)\
**Post date:** [September 11, 2022, 6:44pm UTC](https://community.hivemq.com/t/no-connect-after-disconnect/1427/1 "2022-09-11T18:44:03Z")

</div>

I have the following listeners:

```auto
var client = Mqtt3Client.builder().serverHost(host).serverPort(1883).identifier(clientId).addConnectedListener(
 				context -> System.out.println("connected " + LocalTime.now() + " host: " + context.getClientConfig().getServerHost()
 					+ " client: " + context.getClientConfig().getClientIdentifier())).addDisconnectedListener(context -> {
 				System.out.println("disconnected - " + " cause: " + context.getCause() + " source:" + context.getSource() + " " + LocalTime.now());
 				 context.getReconnector()
          .reconnect(context.getSource() != MqttDisconnectSource.USER)
          .delay(2 * context.getReconnector().getAttempts(), TimeUnit.SECONDS);
 			}).buildBlocking();
 			client.connectWith().simpleAuth().username(user).password(UTF_8.encode(passwd)).applySimpleAuth().send();

```

Occasionaly, I get this:

```auto
Sep 11 19:50:32 cama java[20789]: disconnected - cause: com.hivemq.client.mqtt.exceptions.ConnectionClosedException: Server closed connection without DISCONNECT. source:SERVER 19:50:32.254664938
Sep 11 19:50:32 cama java[20789]: disconnected - cause: com.hivemq.client.mqtt.exceptions.ConnectionClosedException: Server closed connection without DISCONNECT. source:SERVER 19:50:32.555904961
Sep 11 19:50:34 cama java[20789]: disconnected - cause: com.hivemq.client.mqtt.exceptions.ConnectionClosedException: Server closed connection without DISCONNECT. source:SERVER 19:50:34.609964
Sep 11 19:50:38 cama java[20789]: disconnected - cause: com.hivemq.client.mqtt.exceptions.ConnectionClosedException: Server closed connection without DISCONNECT. source:SERVER 19:50:38.658020411
....

```

However, there seems to be no reconnect attempt ?

Kind regards  
Peter

---

<div class="post-metadata">

**Author:** ![Daria\_H](https://avatars.discourse-cdn.com/v4/letter/d/dfb087/32.png) [@Daria\_H](https://community.hivemq.com/u/Daria_H)\
**Post date:** [September 12, 2022, 2:49pm UTC](https://community.hivemq.com/t/no-connect-after-disconnect/1427/2 "2022-09-12T14:49:20Z")

</div>

@pvittali , your code works correctly for me, both “connected” and “disconnected” messages are logged:

```auto
> Task :hivemq-mqtt-client-examples:ReconnectStrategy.main()
connected 16:28:02.560254 host: localhost client: Optional[javaExample]
disconnected - cause: com.hivemq.client.mqtt.mqtt5.exceptions.Mqtt5DisconnectException: Server sent DISCONNECT. source:SERVER 16:28:46.023927
connected 16:28:46.072637 host: localhost client: Optional[javaExample]
disconnected - cause: com.hivemq.client.mqtt.mqtt5.exceptions.Mqtt5DisconnectException: Server sent DISCONNECT. source:SERVER 16:29:45.974866
connected 16:29:46.034585 host: localhost client: Optional[javaExample]

```

I suggest you first to to disable authentication in your HiveMQ and test your Java code without username and password.  
While testing, watch the event.log for connection and disconnection events (where the `Client ID ` == `clientId` from your Java code).

I hope this helps  
Dasha from HiveMQ team

---

<div class="post-metadata">

**Author:** ![pvittali](https://avatars.discourse-cdn.com/v4/letter/p/f19dbf/32.png) [@pvittali](https://community.hivemq.com/u/pvittali)\
**Post date:** [September 12, 2022, 7:47pm UTC](https://community.hivemq.com/t/no-connect-after-disconnect/1427/3 "2022-09-12T19:47:10Z")

</div>

HI Daria  
Interestingly, you get the connected - disconnected pair, by I really don’t, as you can see, only the disconnect messages appear in my log …

I noticed that when I restart the application, thus recreating the Mqtt3BlockingClient is get a connection immediately. So somehow the reconnect listeners do not really perform a “fresh” connect ?
