Skip to content

Client not reconnecting issue #747

@Gansutianwenjun

Description

@Gansutianwenjun

version==1.3.12 jdk ==11; springboot==2.7.18

@Bean(name = "mqtt5AsyncClientSubscriber")
public Mqtt5AsyncClient mqtt5AsyncClientSubscriber(){
    String podName = System.getenv().getOrDefault("HOSTNAME", "local");
    String clientId = props.getSubscriber().getReceiveClientId() + "-" + podName + "-" + "tianwenjun";
    Mqtt5AsyncClient client = MqttClient.builder()
            .useMqttVersion5()
            .serverHost(props.getBrokerUrl())
            .serverPort(props.getPort())
            .identifier(clientId)
            .automaticReconnectWithDefaultConfig()
            .addConnectedListener(callback)
            .addDisconnectedListener(callback)
            .buildAsync();
    client.connectWith()
            .cleanStart(props.getSubscriber().isCleanSession())
            .keepAlive(props.getSubscriber().getKeepalive())
            .simpleAuth()
            .username(props.getUsername())
            .password(props.getPassword().getBytes(StandardCharsets.UTF_8))
            .applySimpleAuth()
            .send()
            .whenComplete((ack, ex) -> {
                if (ex != null) {
                    log.warn("❌ 创建 MQTT [Subscriber] 失败!客户端ID: {}", clientId, ex.getMessage());
                } else {
                    log.info("✅ 创建 MQTT [Subscriber] 成功!客户端ID: {}", clientId);
                    topicManager.subscribeDefaultTopics(client);
                }
            });
    return client;
}

❓ Question

When my application initializes, it creates one subscriber client and one publisher client. However, if I manually disconnect either of them from the EMQX server management console, they do not automatically reconnect. Likewise, when the EMQX server restarts or when the client loses network connection, they also fail to reconnect automatically. I’m really frustrated about this issue.
Would you like me to help you polish it further into a version suitable for posting on GitHub issues or a technical forum (more formal and detailed)?
2025-12-29 16:09:05 |ERROR |com.hivemq.client.mqtt-4-2 |MqttSubscriberClientCallback.java:44 |com.runhoodpower.cn.service.share.mqtt.callback.MqttSubscriberClientCallback |❌ MQTT 异常断开: CONNECT failed as CONNACK contained an Error Code: BAD_USER_NAME_OR_PASSWORD

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions