这个客户端订阅了全量设备上线通知,期间cpu内存和网络资源正常。看下为啥会重连,以及偶现的重连失败原因
服务端日志:
{“time”:“2026-10-20T07:00:25.388376+08:00”,“level”:“info”,“msg”:“terminate”,“username”:“detector-prod”,“reason”:“{shutdown,#{max => 1000,reason => message_queue_too_long,value => 1678}}”,“pid”:“<0.14088.2802>”,“peername”:“隐私”,“mfa”:“{emqx_connection,terminate,2}”,“line”:678,“clientid”:“detector-prod_1789576441982_3pOg”}
客户端自动重连失败日志:
{“timestamp”:“2026-10-20T07:01:35.901906705+08:00”,“level”:“ERROR”,“logger”:“reactor.core.publisher.Operators”,“message”:“Operator called default onErrorDropped”,“thread”:“parallel-1”,“trace_id”:“b0410222435662d6f0e95f918abd9e93”,“trace_flags”:“01”,“span_id”:“7e2be7294c78d495”,“stack_trace”:“reactor.core.Exceptions$ErrorCallbackNotImplemented: java.lang.RuntimeException: Client is not connected (32104)\nCaused by: java.lang.RuntimeException: Client is not connected (32104)\n\tat com.sgm.vehiclestatusdetector.common.config.MqttClientConfig.sendDetectMessage(MqttClientConfig.java:161)\n\tat com.sgm.vehiclestatusdetector.service.impl.DetectServiceImpl.lambda$detectVehicleOnlineStatus$0(DetectServiceImpl.java:50)\n\tat reactor.core.publisher.MonoRunnable.subscribe(MonoRunnable.java:49)\n\tat reactor.core.publisher.Mono.subscribe(Mono.java:4576)\n\tat reactor.core.publisher.FluxFlatMap$FlatMapMain.onNext(FluxFlatMap.java:430)\n\tat io.opentelemetry.javaagent.shaded.instrumentation.reactor.v3_1.TracingSubscriber.lambda$onNext$1(TracingSubscriber.java:64)\n\tat io.opentelemetry.javaagent.shaded.instrumentation.reactor.v3_1.TracingSubscriber.withActiveSpan(TracingSubscriber.java:97)\n\tat io.opentelemetry.javaagent.shaded.instrumentation.reactor.v3_1.TracingSubscriber.withActiveSpan(TracingSubscriber.java:91)\n\tat io.opentelemetry.javaagent.shaded.instrumentation.reactor.v3_1.TracingSubscriber.onNext(TracingSubscriber.java:64)\n\tat reactor.core.publisher.FluxLimitRequest$FluxLimitRequestSubscriber.onNext(FluxLimitRequest.java:99)\n\tat io.opentelemetry.javaagent.shaded.instrumentation.reactor.v3_1.TracingSubscriber.lambda$onNext$1(TracingSubscriber.java:64)\n\tat io.opentelemetry.javaagent.shaded.instrumentation.reactor.v3_1.TracingSubscriber.withActiveSpan(TracingSubscriber.java:97)\n\tat io.opentelemetry.javaagent.shaded.instrumentation.reactor.v3_1.TracingSubscriber.withActiveSpan(TracingSubscriber.java:91)\n\tat io.opentelemetry.javaagent.shaded.instrumentation.reactor.v3_1.TracingSubscriber.onNext(TracingSubscriber.java:64)\n\tat reactor.core.publisher.FluxInterval$IntervalRunnable.run(FluxInterval.java:124)\n\tat reactor.core.scheduler.PeriodicWorkerTask.call(PeriodicWorkerTask.java:59)\n\tat reactor.core.scheduler.PeriodicWorkerTask.run(PeriodicWorkerTask.java:73)\n\tat java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:539)\n\tat java.base/java.util.concurrent.FutureTask.runAndReset(FutureTask.java:305)\n\tat java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:305)\n\tat java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)\n\tat java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)\n\tat java.base/java.lang.Thread.run(Thread.java:840)\nCaused by: org.eclipse.paho.mqttv5.common.MqttException: Client is not connected\n\tat org.eclipse.paho.mqttv5.client.internal.ExceptionHelper.createMqttException(ExceptionHelper.java:32)\n\tat org.eclipse.paho.mqttv5.client.internal.ClientComms.sendNoWait(ClientComms.java:231)\n\tat org.eclipse.paho.mqttv5.client.MqttAsyncClient.publish(MqttAsyncClient.java:1530)\n\tat com.sgm.vehiclestatusdetector.common.config.MqttClientConfig.sendDetectMessage(MqttClientConfig.java:148)\n\t… 22 common frames omitted\n”,“class”:“reactor.util.Loggers$Slf4JLogger”,“method”:“error”,“caller_file_name”:“Loggers.java”,“line”:324}
使用的是java paho客户端代码如下,使用了自动重连:
@Slf4j
@Configuration
public class MqttClientConfig {
@Value("${mqtt.brokerUrl}")
private String brokerUrl;
@Value("${mqtt.username}")
private String username;
@Value("${mqtt.password}")
private String password;
@Value("${mqtt.connectTimeout}")
private int connectTimeout;
@Value("${mqtt.keepalive}")
private int keepalive;
@Value("${mqtt.subTopics}")
private String subTopics;
@Value("${mqtt.detectTopicSuffix}")
private String detectTopicSuffix;
private final MqttMessage detectMessage = new MqttMessage("1".getBytes(), 0, false, new MqttProperties());
private final Sinks.Many<String> eventSink = Sinks.many().multicast().directBestEffort();
private MqttAsyncClient mqttAsyncClient;
@Autowired
private MetricsConfig metricsConfig;
@Bean
public Sinks.Many<String> mqttMessageSink() {
return eventSink;
}
@Bean
public MqttAsyncClient mqttClient() throws MqttException {
MqttAsyncClient client = new MqttAsyncClient(brokerUrl, MqttClientIdGenerator.generateClientId(username), new MemoryPersistence());
client.setCallback(new MqttCallback() {
@Override
public void disconnected(MqttDisconnectResponse disconnectResponse) {
log.error("MQTT回调: 连接断开 {}", disconnectResponse.toString());
}
@Override
public void mqttErrorOccurred(MqttException exception) {
log.error("MQTT回调: MQTT异常: {}", exception.toString());
System.out.println("MQTT异常: " + exception.toString());
}
@Override
public void messageArrived(String topic, MqttMessage message) {
log.info("MQTT回调: 收到MQTT消息, payload: {}, topic: {}", message, topic);
String vin = topic.split("/")[0]; // 提取VIN号
eventSink.tryEmitNext(vin);
}
@Override
public void deliveryComplete(IMqttToken token) {
log.info("MQTT回调: 消息投递成功: {}", token.getMessageId());
}
@Override
public void connectComplete(boolean reconnect, String serverURI) {
if (reconnect) {
log.info("MQTT回调: 重连成功:{}", serverURI);
subscribeTopics(client, subTopics);
} else {
log.info("MQTT回调: 连接成功: {}", serverURI);
subscribeTopics(client, subTopics);
}
}
@Override
public void authPacketArrived(int reasonCode, MqttProperties properties) {
log.info("MQTT回调: Auth packet arrived: {}", properties);
}
});
MqttConnectionOptions options = getMqttConnectionOptions();
IMqttToken connectToken = client.connect(options);
try{
connectToken.waitForCompletion();
}catch (Exception e){
e.printStackTrace();
}
this.mqttAsyncClient = client;
return client;
}
private MqttConnectionOptions getMqttConnectionOptions() {
MqttConnectionOptions options = new MqttConnectionOptions();
options.setAutomaticReconnect(true);
options.setCleanStart(true);
options.setKeepAliveInterval(keepalive);
options.setConnectionTimeout(connectTimeout);
options.setUserName(username);
options.setPassword(password.getBytes());
return options;
}
private void subscribeTopics(MqttAsyncClient client, String topics) {
String[] topicsArray = topics.split(",");
if (client == null || topicsArray.length == 0) {
log.warn("MQTT订阅异常: MQTT client is null or no topics to subscribe.");
return;
}
int[] qosLevels = new int[topicsArray.length];
Arrays.fill(qosLevels, 1);
try {
IMqttToken token = client.subscribe(topicsArray, qosLevels);
token.waitForCompletion();
} catch (MqttException e) {
log.error("MQTT订阅异常: Failed to subscribe topics: ", e);
throw new RuntimeException("MQTT Topic订阅异常");
}
}
public Flux<String> getFluxFilterByVin(String vin) {
return eventSink.asFlux()
.filter(v -> v.equals(vin));
}
public void sendDetectMessage(String vin) {
try {
String topic = vin.concat(detectTopicSuffix);
mqttAsyncClient.publish(topic, detectMessage, null, new MqttActionListener() {
@Override
public void onSuccess(IMqttToken asyncActionToken) {
}
@Override
public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
metricsConfig.incrementMqttDetectSendFailCounter();
log.error("MQTT PUBLISH FAIL, topic:{}", topic, exception);
}
});
} catch (MqttException e) {
metricsConfig.incrementMqttDetectSendFailCounter();
log.error("MQTT PUBLISH FAIL 发送探测消息异常, vin: {}", vin, e);
throw new RuntimeException(e);
}
}
}