|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import lombok.extern.slf4j.Slf4j; | 
|---|
|  |  |  | import org.eclipse.paho.client.mqttv3.MqttClient; | 
|---|
|  |  |  | import org.springframework.beans.factory.annotation.Value; | 
|---|
|  |  |  | import org.springframework.web.bind.annotation.GetMapping; | 
|---|
|  |  |  | import org.springframework.web.bind.annotation.PathVariable; | 
|---|
|  |  |  | import org.springframework.web.bind.annotation.RequestMapping; | 
|---|
|  |  |  | 
|---|
|  |  |  | @RestController | 
|---|
|  |  |  | @RequestMapping(path = "mqtt") | 
|---|
|  |  |  | public class TestController { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Value("${mqtt.broker}") | 
|---|
|  |  |  | private String broker; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Value("${mqtt.username}") | 
|---|
|  |  |  | private String username; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Value("${mqtt.password}") | 
|---|
|  |  |  | private String password; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Value("${mqtt.topic}") | 
|---|
|  |  |  | private String topic; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Value("${mqtt.qos}") | 
|---|
|  |  |  | private Integer qos; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @GetMapping("/mqtt/{msg}") | 
|---|
|  |  |  | public String testSendMqttMsg(@PathVariable("msg") String msg){ | 
|---|
|  |  |  | log.info("消息内容:{}.", msg); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | MqttClient mqttClient = MqttClientConnectorPool.connectMQTT(); | 
|---|
|  |  |  | MqttClient mqttClient = MqttClientConnectorPool.connectMQTT(broker, username, password); | 
|---|
|  |  |  | MqttMsgSender sender = new MqttMsgSender(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | String content = "{" + " \"message\": \"" + msg + "\"," + " \"val\": 100.00" + "}"; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | String topic = "workOrder"; | 
|---|
|  |  |  | int qos = 1; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (null != mqttClient){ | 
|---|
|  |  |  | sender.sendMessage(mqttClient, topic, content, qos); | 
|---|