com.feingto.iot.client.MqttClient Maven / Gradle / Ivy
package com.feingto.iot.client;
import com.feingto.iot.client.bootstrap.MqttClientBootstrap;
import com.feingto.iot.client.config.NettyProperties;
import com.feingto.iot.client.service.MqttMessageService;
import com.feingto.iot.common.model.mqtt.SendMessage;
import com.feingto.iot.common.model.mqtt.SubscribeMessage;
import com.feingto.iot.common.service.ResponseCallback;
import com.feingto.iot.common.service.mqtt.MessageRequest;
import com.feingto.iot.common.util.MessageId;
import io.netty.channel.Channel;
import io.netty.handler.codec.mqtt.MqttQoS;
import io.netty.handler.codec.mqtt.MqttTopicSubscription;
import lombok.Setter;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
import java.util.Arrays;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* MQTT 客户端
*
* @author longfei
*/
@Slf4j
@Accessors(fluent = true)
public class MqttClient extends MqttMessageService {
private final NettyProperties config;
private final AtomicInteger retryLatch;
private MqttClientBootstrap bootstrap;
private Channel channel;
@Setter
private String[] topic;
@Setter
private int mqttQoS = 0;
public MqttClient(NettyProperties config) {
this.config = config;
retryLatch = new AtomicInteger(config.getMqtt().getRetryMaxAttempts());
}
public MqttClient callback(ResponseCallback callback) {
this.setCallback(callback);
return this;
}
/**
* 连接服务端
*/
public void connect() {
bootstrap = new MqttClientBootstrap(config).addClient(this);
channel = bootstrap.start();
retryLatch.set(config.getMqtt().getRetryMaxAttempts());
if (ArrayUtils.isNotEmpty(topic)) {
subscribe(Arrays.stream(topic)
.map(topic -> new SubscribeMessage()
.mqttQoS(mqttQoS)
.topicName(topic))
.collect(Collectors.toList())
.toArray(new SubscribeMessage[topic.length]));
}
}
/**
* 重新连接
*/
public void reconnect() {
destroy();
if (retryLatch.getAndDecrement() > 0) {
log.warn("Trying to reconnect");
connect();
}
}
/**
* 推送消息至主题
*/
public void publish(String topic, String message) {
publish(topic, message, false, 0);
}
/**
* 推送Qos消息至主题
*/
public void publish(String topic, String message, int qos) {
publish(topic, message, false, qos);
}
/**
* 推送是否保留的消息至主题
*/
public void publish(String topic, String message, boolean retained) {
publish(topic, message, retained, 0);
}
/**
* 推送是否保留的Qos消息至主题
*/
public void publish(String topic, String message, boolean retained, int qos) {
MessageRequest.publish(channel, new SendMessage()
.id(MessageId.messageId())
.topic(topic)
.mqttQoS(MqttQoS.valueOf(qos))
.retain(retained)
.payload(message.getBytes()));
}
/**
* 订阅
*/
public void subscribe(SubscribeMessage... messages) {
MessageRequest.subscribe(channel, Stream.of(messages)
.map(message -> new MqttTopicSubscription(message.topicName(), MqttQoS.valueOf(message.mqttQoS())))
.collect(Collectors.toList()), MessageId.messageId());
}
/**
* 取消订阅
*/
public void unsubscribe(String... topics) {
MessageRequest.unsubscribe(channel, Arrays.asList(topics), MessageId.messageId());
}
/**
* 释放资源
*/
public void destroy() {
if (bootstrap != null) bootstrap.close();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy