springboot集成mqtt的實踐開發(fā)
序
MQTT(Message Queuing Telemetry Transport)是基于二進制消息的發(fā)布/訂閱編程模式的消息協(xié)議,非常適合需要低功耗和網(wǎng)絡(luò)帶寬有限的IoT場景。這里簡單介紹一下如何在springboot中集成。
maven
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-integration</artifactId> </dependency> <dependency> <groupId>org.springframework.integration</groupId> <artifactId>spring-integration-stream</artifactId> </dependency> <dependency> <groupId>org.springframework.integration</groupId> <artifactId>spring-integration-mqtt</artifactId> </dependency>
配置client factory
@Bean public MqttPahoClientFactory mqttClientFactory() { DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory(); factory.setServerURIs("tcp://demo:1883"); // factory.setUserName("guest"); // factory.setPassword("guest"); return factory; }
配置consumer
@Bean public IntegrationFlow mqttInFlow() { return IntegrationFlows.from(mqttInbound()) .transform(p -> p + ", received from MQTT") .handle(logger()) .get(); } private LoggingHandler logger() { LoggingHandler loggingHandler = new LoggingHandler("INFO"); loggingHandler.setLoggerName("siSample"); return loggingHandler; } @Bean public MessageProducerSupport mqttInbound() { MqttPahoMessageDrivenChannelAdapter adapter = new MqttPahoMessageDrivenChannelAdapter("siSampleConsumer", mqttClientFactory(), "siSampleTopic"); adapter.setCompletionTimeout(5000); adapter.setConverter(new DefaultPahoMessageConverter()); adapter.setQos(1); return adapter; }
配置producer
@Bean public IntegrationFlow mqttOutFlow() { //console input // return IntegrationFlows.from(CharacterStreamReadingMessageSource.stdin(), // e -> e.poller(Pollers.fixedDelay(1000))) // .transform(p -> p + " sent to MQTT") // .handle(mqttOutbound()) // .get(); return IntegrationFlows.from(outChannel()) .handle(mqttOutbound()) .get(); } @Bean public MessageChannel outChannel() { return new DirectChannel(); } @Bean public MessageHandler mqttOutbound() { MqttPahoMessageHandler messageHandler = new MqttPahoMessageHandler("siSamplePublisher", mqttClientFactory()); messageHandler.setAsync(true); messageHandler.setDefaultTopic("siSampleTopic"); return messageHandler; }
配置MessagingGateway
@MessagingGateway(defaultRequestChannel = "outChannel") public interface MsgWriter { void write(String note); }
這樣就大功告成了
doc
spring-integration-samples-mqtt
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- SpringBoot整合MQTT并實現(xiàn)異步線程調(diào)用的問題
- SpringBoot集成mqtt的多模塊項目配置詳解
- springboot 實現(xiàn)mqtt物聯(lián)網(wǎng)的示例代碼
- SpringBoot+MQTT+apollo實現(xiàn)訂閱發(fā)布功能的示例
- SpringBoot+netty-socketio實現(xiàn)服務(wù)器端消息推送
- SpringBoot+WebSocket+Netty實現(xiàn)消息推送的示例代碼
- SpringBoot實現(xiàn)釘釘機器人消息推送的示例代碼
- springboot整合mqtt實現(xiàn)消息訂閱和推送功能
相關(guān)文章
Java關(guān)于BeabUtils.copyproperties的用法
這篇文章主要介紹了Java關(guān)于BeabUtils.copyproperties的用法,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08Java獲取e.printStackTrace()打印的信息方式
這篇文章主要介紹了Java獲取e.printStackTrace()打印的信息方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08MyBatis中的XML實現(xiàn)和動態(tài)SQL實現(xiàn)示例詳解
這篇文章主要介紹了MyBatis中的XML實現(xiàn)和動態(tài)SQL實現(xiàn),我們可以將XML中重復(fù)出現(xiàn)的內(nèi)容提取出來放到sql標簽中,當需要用到sql標簽中的內(nèi)容時,用include標簽將sql標簽中的內(nèi)容引進來即可,感興趣的朋友跟隨小編一起看看吧2024-02-02Java實現(xiàn)去掉字符串重復(fù)字母的方法示例
這篇文章主要介紹了Java實現(xiàn)去掉字符串重復(fù)字母的方法,涉及java針對字符串的遍歷、判斷、運算等相關(guān)操作技巧,需要的朋友可以參考下2017-12-12解決使用mybatis-plus時,生成的SQL大寫變小寫加下劃線問題
這篇文章主要介紹了解決使用mybatis-plus時,生成的SQL大寫變小寫加下劃線問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12