hivemq / hivemq-mqtt-client

HiveMQ MQTT Client is an MQTT 5.0 and MQTT 3.1.1 compatible and feature-rich high-performance Java client library with different API flavours and backpressure support
https://hivemq.github.io/hivemq-mqtt-client/
Apache License 2.0
832 stars 153 forks source link

Reconnect not working depending on where credentials are provided #574

Open inad9300 opened 1 year ago

inad9300 commented 1 year ago

🐛 Bug Report

If credentials are provided to Mqtt5ClientBuilder, reconnections to a broker when the broker is restarted work. However, when provided to Mqtt5ConnectBuilder (as exemplified in the docs), initial connections work but reconnections don't! The following error message is displayed:

CONNECT failed as CONNACK contained an Error Code: BAD_USER_NAME_OR_PASSWORD.

What is the difference between providing them in one place versus the other? I was shocked to find that there were two possible places, to be honest, and it took me a long time to come to this lucky realization, given the lack of documentation.

During the course of my investigation, I sometimes saw a different warning related to SSL (which I'm not using), saying "Username: null; SSL certificate subject DN: unavailable". They appeared with the same frequency as I have configured reconnects to occur, so they must have been logged on reconnection attempts. I'm not sure if these warnings stem from the same problem or not, but they don't show up anymore.

🔬 How To Reproduce

Steps to reproduce the behavior:

  1. Start an MQTT client and server
  2. Restart the server (I'm using docker, so I use docker compose restart mqtt_server)
  3. Verify that the client is automatically reconnected to the server in one case but not the other

Code sample

var client = Mqtt5Client
    .builder()
    .serverHost("host")
    .serverPort(port)
    // When using these, both connecting and reconnecting work:
    .simpleAuth()
    .username("user")
    .password("password".getBytes())
    .applySimpleAuth()
    .automaticReconnect()
    .initialDelay(1, SECONDS)
    .maxDelay(1, SECONDS)
    .applyAutomaticReconnect()
    .identifier(randomUUID().toString())
    .buildAsync();

var connAck = client
    .connectWith()
    .sessionExpiryInterval(60)
    // When using these, connection works but reconnection does not:
    // .simpleAuth()
    // .username("user")
    // .password("password".getBytes())
    // .applySimpleAuth()
    .send()
    .get(30, SECONDS);

Environment

Where are you running/using this client? Docker What version of this client are you using? 1.3.1 JVM version? 17 Operating System? Ubuntu Which MQTT protocol version is being used? 5 Which MQTT broker (name and version)? Apache ActiveMQ Artemis 2.28.0