In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
MQTT protocol is frequently used in the industrial Internet of things because of its low latency and high efficiency. The first two documents briefly introduced the MQTT content and MQTT server. Today, this paper describes how to use code to send MQTT messages from a practical point of view.
1. Introduce related dependencies
Org.springframework.bootspring-boot-starter-weborg.springframework.bootspring-boot-starter-integrationorg.springframework.integrationspring-integration-mqtt
two。 Configure MQTT server information in application.yml
Server: port: 9090mqtt: host: tcp://127.0.0.1:1883 clientinid: mqttinId clientoutid: mqttoutid topic: virus qoslevel: 1 # MQTT Certification username: xxx password: yyy # 10s timeout: 10000 # 20s keepalive: 20
3. Configure MQTT message push configuration
@ Configuration@IntegrationComponentScanpublic 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 message push interface
MessagingGateway (defaultRequestChannel = "mqttOutboundChannel") public interface MqttGateway {void sendToMqtt (String data, @ Header (MqttHeaders.TOPIC) String topic);}
5.MQTT message push API
RestControllerpublic class MessageController {@ Autowired MqttGateway mqttGateway; @ RequestMapping ("/ sendMqttMessage") public String sendMqttMessage (String message, String topic) {mqttGateway.sendToMqtt (message, topic); return "ok";}}
test
Then you can test it in POSTMAN, enter the message content and subject, and send the message on the appropriate channel. If you use another message client for testing, you can receive the message
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.