MQTT協(xié)議因低延遲、效率高在工業(yè)物聯(lián)網(wǎng)領域使用的頻率特別高,前面兩篇文檔分別對MQTT內容和MQTT服務器做了簡單介紹,今天本文從實戰(zhàn)的角度闡述如何用代碼實現(xiàn)發(fā)送MQTT消息。
創(chuàng)新互聯(lián)公司是一家專業(yè)提供相山企業(yè)網(wǎng)站建設,專注與網(wǎng)站設計、成都網(wǎng)站建設、H5技術、小程序制作等業(yè)務。10年已為相山眾多企業(yè)、政府機構等服務。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站制作公司優(yōu)惠進行中。
1.引入相關的依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-integration</artifactId> </dependency> <dependency> <groupId>org.springframework.integration</groupId> <artifactId>spring-integration-mqtt</artifactId> </dependency>
2.在application.yml配置MQTT服務器信息
server: port: 9090 mqtt: host: tcp://127.0.0.1:1883 clientinid: mqttinId clientoutid: mqttoutid topic: virus qoslevel: 1 #MQTT 認證 username: xxx password: yyy # 10s timeout: 10000 #20s keepalive: 20
3.配置MQTT消息推送配置
@Configuration @IntegrationComponentScan public class MqttSenderConfig { @Value("${mqtt.username}") private String username; @Value("${mqtt.password}") private String password; @Value("${mqtt.host}") private String hostUrl; @Value("${mqtt.clientinid}") private String clientId; @Value("${mqtt.topic}") private String defaultTopic; @Value("${mqtt.timeout}") private int completionTimeout; @Bean public MqttConnectOptions getMqttConnectOptions(){ MqttConnectOptions mqttConnectOptions=new MqttConnectOptions(); mqttConnectOptions.setCleanSession(true); mqttConnectOptions.setConnectionTimeout(10); mqttConnectOptions.setKeepAliveInterval(90); mqttConnectOptions.setAutomaticReconnect(true); mqttConnectOptions.setUserName(username); mqttConnectOptions.setPassword(password.toCharArray()); mqttConnectOptions.setServerURIs(new String[]{hostUrl}); mqttConnectOptions.setKeepAliveInterval(2); return mqttConnectOptions; } @Bean public MqttPahoClientFactory mqttClientFactory() { DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory(); factory.setConnectionOptions(getMqttConnectOptions()); return factory; } @Bean @ServiceActivator(inputChannel = "mqttOutboundChannel") public MessageHandler mqttOutbound() { MqttPahoMessageHandler messageHandler = new MqttPahoMessageHandler(clientId, mqttClientFactory()); messageHandler.setAsync(true); messageHandler.setDefaultTopic(defaultTopic); return messageHandler; } @Bean public MessageChannel mqttOutboundChannel() { return new DirectChannel(); } }
4.MQTT消息推送接口
@MessagingGateway(defaultRequestChannel = "mqttOutboundChannel") public interface MqttGateway { void sendToMqtt(String data, @Header(MqttHeaders.TOPIC) String topic); }
5.MQTT消息推送API
@RestController public class MessageController { @Autowired MqttGateway mqttGateway; @RequestMapping("/sendMqttMessage") public String sendMqttMessage(String message, String topic) { mqttGateway.sendToMqtt(message, topic); return "ok"; } }
測試
接下來就可以在POSTMAN中進行測試了,輸入消息內容和主題,就可以在相應的頻道發(fā)送消息了。如果使用其它的消息客戶端進行測試的話,可以接受到消息
網(wǎng)頁題目:如何用代碼實現(xiàn)發(fā)送MQTT消息
URL標題:http://m.2m8n56k.cn/article32/pjedpc.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供移動網(wǎng)站建設、網(wǎng)站改版、網(wǎng)站營銷、網(wǎng)站收錄、App開發(fā)、網(wǎng)站設計公司
聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:[email protected]。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)