fix: wait ten seconds before mqtt command retry

This commit is contained in:
yuhaiming
2026-07-17 16:38:17 +08:00
parent 96941abc0d
commit 3728f9a009
3 changed files with 42 additions and 2 deletions

View File

@@ -47,7 +47,7 @@ mqtt:
command-ack: command-ack:
enabled: true enabled: true
max-retry-count: 3 max-retry-count: 3
retry-interval-ms: 30000 retry-interval-ms: 10000
scan-interval-ms: 5000 scan-interval-ms: 5000
pending-ttl-seconds: 86400 pending-ttl-seconds: 86400
ack-ttl-seconds: 86400 ack-ttl-seconds: 86400

View File

@@ -55,7 +55,7 @@ public class MqttProperties {
public static class CommandAck { public static class CommandAck {
private boolean enabled = true; private boolean enabled = true;
private int maxRetryCount = 3; private int maxRetryCount = 3;
private long retryIntervalMs = 5000; private long retryIntervalMs = 10000;
private long scanIntervalMs = 5000; private long scanIntervalMs = 5000;
private long pendingTtlSeconds = 86400; private long pendingTtlSeconds = 86400;
private long ackTtlSeconds = 86400; private long ackTtlSeconds = 86400;

View File

@@ -1,15 +1,39 @@
package org.dromara.mqtt; package org.dromara.mqtt;
import cn.hutool.extra.spring.SpringUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.dromara.app.domain.mqtt.DeviceCommand;
import org.dromara.mqtt.config.properties.MqttProperties; import org.dromara.mqtt.config.properties.MqttProperties;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.context.support.GenericApplicationContext;
import java.util.function.Supplier;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.springframework.test.util.ReflectionTestUtils.invokeMethod; import static org.springframework.test.util.ReflectionTestUtils.invokeMethod;
@Tag("dev") @Tag("dev")
class DeviceMqttCommandPublisherTest { class DeviceMqttCommandPublisherTest {
private static GenericApplicationContext applicationContext;
@BeforeAll
static void initializeJsonUtils() {
applicationContext = new GenericApplicationContext();
applicationContext.registerBean(ObjectMapper.class, (Supplier<ObjectMapper>) ObjectMapper::new);
applicationContext.refresh();
new SpringUtil().setApplicationContext(applicationContext);
}
@AfterAll
static void closeApplicationContext() {
applicationContext.close();
}
@Test @Test
void buildCommandTopicDefaultsToDeviceNo() { void buildCommandTopicDefaultsToDeviceNo() {
MqttProperties properties = new MqttProperties(); MqttProperties properties = new MqttProperties();
@@ -23,4 +47,20 @@ class DeviceMqttCommandPublisherTest {
assertThat(topic).isEqualTo("/d01/subscriber/cmd"); assertThat(topic).isEqualTo("/d01/subscriber/cmd");
} }
@Test
void sendSchedulesFirstRetryTenSecondsAfterInitialPublish() {
DeviceMqttCommandPublisher publisher = new DeviceMqttCommandPublisher(
mock(MqttClientManager.class),
mock(MqttCommandAckService.class),
new MqttProperties()
);
DeviceCommand command = new DeviceCommand();
command.setDeviceNo("D01");
command.setCommandType("switch");
publisher.send(command);
assertThat(command.getNextRetryAt() - command.getLastSentAt()).isEqualTo(10000);
}
} }