# Cancel automatic reconnects

**URL:** <https://community.hivemq.com/t/cancel-automatic-reconnects/345>\
**Category:** HiveMQ Client Library\
**Created:** [September 28, 2020, 11:20am UTC](https://community.hivemq.com/t/cancel-automatic-reconnects/345 "2020-09-28T11:20:27Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![aloz77](https://avatars.discourse-cdn.com/v4/letter/a/dec6dc/32.png) [@aloz77](https://community.hivemq.com/u/aloz77)\
**Post date:** [September 28, 2020, 11:20am UTC](https://community.hivemq.com/t/cancel-automatic-reconnects/345/1 "2020-09-28T11:20:27Z")

</div>

I found that the running automatic reconnects (e.g. for authentication failure) are not stopped even by calling mqttClient.disconnect(). In this state ending the activity can even produce a memory leak.

According to [https://github.com/hivemq/hivemq-mqtt-client/issues/306](https://github.com/hivemq/hivemq-mqtt-client/issues/306) we can apply following code to cancel reconnects:

```
  final AtomicBoolean cancelReconnect = new AtomicBoolean(false);
  Mqtt3Client.builder()
        .automaticReconnectWithDefaultConfig()
        .addDisconnectedListener(context -> {
            if (cancelReconnect.get()) {
                context.getReconnector().reconnect(false);
            }
        })
    ...

```

Is it possible to subclass the Mqtt3AsyncClient in order to make _cancelReconnect_ an object field and make some method like this available:

```
public void cancelReconnects() {
    this.cancelReconnect.set(true);
}

```

Or do I **have to** make a wrapper?

Or is there a better way to tell the client to stop anything as it’s going to be destroyed?
