parent
dd64b29e8b
commit
f6b5f12eef
@ -0,0 +1,31 @@
|
||||
package cc.yunxi.config;
|
||||
|
||||
import cc.yunxi.config.props.MqttProperties;
|
||||
import cc.yunxi.utils.CustomerMqttClient;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.eclipse.paho.client.mqttv3.MqttException;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
|
||||
@Slf4j
|
||||
//@Configuration
|
||||
@EnableConfigurationProperties(MqttProperties.class)
|
||||
public class MqttConfig {
|
||||
|
||||
@Resource
|
||||
private MqttProperties properties;
|
||||
|
||||
@Bean
|
||||
public CustomerMqttClient customerMqttClient() throws MqttException {
|
||||
CustomerMqttClient client = new CustomerMqttClient();
|
||||
client.setMqttProperties(properties);
|
||||
client.connect();
|
||||
log.info("===========> MQTT连接成功 <===========");
|
||||
return client;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,21 @@
|
||||
package cc.yunxi.config.props;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@Data
|
||||
@ConfigurationProperties(prefix = "mqtt")
|
||||
public class MqttProperties {
|
||||
private String host;
|
||||
private String clientId;
|
||||
private String username;
|
||||
private String password;
|
||||
private Map<String, Integer> topics;
|
||||
private Boolean cleanSession;
|
||||
private Integer timeout;
|
||||
private Integer qos;
|
||||
private Integer keepAlive;
|
||||
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package cc.yunxi.utils;
|
||||
|
||||
import org.eclipse.paho.client.mqttv3.MqttMessage;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class TopicHandler {
|
||||
|
||||
public void handleTopic(String topic, MqttMessage message) {
|
||||
// 处理订阅的主题和消息
|
||||
System.out.println("Received message on topic: " + topic + ", message: " + message.getPayload());
|
||||
}
|
||||
}
|
Loading…
Reference in new issue