How to use code to send MQTT messages
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