123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- package com.zy.omp.config.mqtt;
- import com.zy.omp.utils.CodeGenerator;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.integration.annotation.ServiceActivator;
- import org.springframework.integration.channel.DirectChannel;
- import org.springframework.integration.mqtt.core.MqttPahoClientFactory;
- import org.springframework.integration.mqtt.outbound.MqttPahoMessageHandler;
- import org.springframework.messaging.MessageChannel;
- import org.springframework.messaging.MessageHandler;
- /**
- * MQTT 生产者配置
- *
- * @author yang xiao kun
- * create on 2021/1/19
- */
- @Configuration
- public class MqttProducerCfg {
- @Autowired
- private MqttPahoClientFactory mqttClientFactory;
- /**
- * MQTT信息通道(生产者)
- */
- @Bean(name = "mqttOutboundChannel")
- public MessageChannel mqttOutboundChannel() {
- return new DirectChannel();
- }
- /**
- * MQTT消息处理器(生产者)
- */
- @Bean
- @ServiceActivator(inputChannel = "mqttOutboundChannel")
- public MessageHandler mqttOutbound() {
- MqttPahoMessageHandler messageHandler = new MqttPahoMessageHandler(CodeGenerator.shortUuid() + "_Producer", mqttClientFactory);
- // 设置异步发送,默认是false(发送时阻塞)
- messageHandler.setAsync(true);
- // 设置默认的服务质量
- messageHandler.setDefaultQos(0);
- messageHandler.setDefaultRetained(false);
- return messageHandler;
- }
- }
|