提交 53aada4a 编写于 作者: 浅梦2013's avatar 浅梦2013

完善 stater,添加遗嘱消息配置。

上级 b7086017
......@@ -114,6 +114,10 @@ public final class MqttWillMessage {
}
public MqttWillMessage build() {
// 遗嘱标志被设置为 false,遗嘱 QoS 也必须设置为 0。
if (!this.retain && MqttQoS.AT_MOST_ONCE != this.qos) {
throw new IllegalArgumentException("WillMessage retain is false and QoS must be 0");
}
return new MqttWillMessage(this.topic, this.message, this.retain, this.qos, this.willProperties);
}
}
......
......@@ -18,11 +18,15 @@ package net.dreamlu.iot.mqtt.spring.client;
import net.dreamlu.iot.mqtt.core.client.MqttClient;
import net.dreamlu.iot.mqtt.core.client.MqttClientCreator;
import net.dreamlu.iot.mqtt.core.client.MqttWillMessage;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;
import java.nio.charset.StandardCharsets;
/**
* mqtt client 配置
......@@ -62,6 +66,18 @@ public class MqttClientConfiguration {
if (reInterval1 != null && reInterval1 > 0) {
clientCreator.reInterval(reInterval1);
}
// 构造遗嘱消息
MqttClientProperties.WillMessage willMessage = properties.getWillMessage();
if (willMessage != null && StringUtils.hasText(willMessage.getTopic())) {
MqttWillMessage.Builder builder = MqttWillMessage.builder();
builder.topic(willMessage.getTopic())
.qos(willMessage.getQos())
.retain(willMessage.isRetain());
if (StringUtils.hasText(willMessage.getMessage())) {
builder.message(willMessage.getMessage().getBytes(StandardCharsets.UTF_8));
}
clientCreator.willMessage(builder.build());
}
// 自定义处理
customizers.ifAvailable((customizer) -> customizer.customize(clientCreator));
return clientCreator;
......
......@@ -20,6 +20,7 @@ import lombok.Getter;
import lombok.Setter;
import net.dreamlu.iot.mqtt.codec.ByteBufferAllocator;
import net.dreamlu.iot.mqtt.codec.MqttConstant;
import net.dreamlu.iot.mqtt.codec.MqttQoS;
import net.dreamlu.iot.mqtt.codec.MqttVersion;
import org.springframework.boot.context.properties.ConfigurationProperties;
......@@ -101,5 +102,30 @@ public class MqttClientProperties {
* ByteBuffer Allocator,支持堆内存和堆外内存,默认为:堆内存
*/
private ByteBufferAllocator bufferAllocator = ByteBufferAllocator.HEAP;
/**
* 遗嘱消息
*/
private WillMessage willMessage;
@Getter
@Setter
public static class WillMessage {
/**
* 遗嘱消息 topic
*/
private String topic;
/**
* 遗嘱消息 qos,默认: qos0
*/
private MqttQoS qos = MqttQoS.AT_MOST_ONCE;
/**
* 遗嘱消息 payload
*/
private String message;
/**
* 遗嘱消息保留标识符,默认: false
*/
private boolean retain = false;
}
}
......@@ -78,7 +78,8 @@ public class MqttServerConfiguration {
.heartbeatTimeout(properties.getHeartbeatTimeout())
.readBufferSize(properties.getReadBufferSize())
.maxBytesInMessage(properties.getMaxBytesInMessage())
.bufferAllocator(properties.getBufferAllocator());
.bufferAllocator(properties.getBufferAllocator())
.maxClientIdLength(properties.getMaxClientIdLength());
if (properties.isDebug()) {
serverCreator.debug();
}
......
......@@ -72,6 +72,10 @@ public class MqttServerProperties {
* debug
*/
private boolean debug = false;
/**
* mqtt 3.1 会校验此参数
*/
private int maxClientIdLength = MqttConstant.DEFAULT_MAX_CLIENT_ID_LENGTH;
/**
* ssl 配置
*/
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册