springboot集成mqtt的實(shí)踐開發(fā)
序
MQTT(Message Queuing Telemetry Transport)是基于二進(jìn)制消息的發(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的詳細(xì)圖文教程
- SpringBoot實(shí)現(xiàn)MQTT消息發(fā)送和接收方式
- springboot集成mqtt超級詳細(xì)步驟
- SpringBoot集成MQTT實(shí)現(xiàn)交互服務(wù)通信
- springboot使用EMQX(MQTT協(xié)議)的實(shí)現(xiàn)
- SpringBoot集成mqtt的多模塊項(xiàng)目配置詳解
- SpringBoot2.0集成MQTT消息推送功能實(shí)現(xiàn)
- SpringBoot項(xiàng)目接入MQTT的詳細(xì)指南
- Springboot對接mqtt的項(xiàng)目實(shí)踐
相關(guān)文章
關(guān)于Java三大特性之多態(tài)的總結(jié)
這篇文章主要介紹了關(guān)于Java三大特性之多態(tài)的總結(jié),內(nèi)容詳細(xì),涉及多態(tài)的定義,存在條件,好處,分類及實(shí)現(xiàn)方式等相關(guān)內(nèi)容,具有一定參考價值,需要的朋友可以了解下。2017-11-11
Java 實(shí)現(xiàn)漢字轉(zhuǎn)換為拼音的實(shí)例
這篇文章主要介紹了Java 實(shí)現(xiàn)漢字轉(zhuǎn)換為拼音的實(shí)例的相關(guān)資料,需要的朋友可以參考下2016-12-12
Java與Oracle實(shí)現(xiàn)事務(wù)(JDBC事務(wù))實(shí)例詳解
這篇文章主要介紹了Java與Oracle實(shí)現(xiàn)事務(wù)(JDBC事務(wù))實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-05-05
JavaWeb入門:ServletContext詳解和應(yīng)用
這篇文章主要介紹了Java ServletContext對象用法解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2021-07-07
Java多線程和并發(fā)基礎(chǔ)面試題(問答形式)
多線程和并發(fā)問題是Java技術(shù)面試中面試官比較喜歡問的問題之一。在這里,從面試的角度列出了大部分重要的問題,感興趣的小伙伴們可以參考一下2016-06-06

