使用相同的连接进行发布并使用Spring Integration Java DSL订阅mqtt主题

时间:2016-11-03 08:56:07

标签: java spring integration mqtt dsl

我有以下配置:

@Configuration
public class MqttConfig {

    @Autowired
    private MqttClientProperties mqttClientProperties;

    @Autowired
    ActionHandlerManager actionHandlerManager;

    @Autowired
    private ActionDispatchManager actionDispatchManager;

    @Autowired
    @Qualifier("messagePackObjectMapper")
    private ObjectMapper messagePackObjectMapper;

    @Bean
    public IntegrationFlow getMqttInFlow() {
        return IntegrationFlows.from(getMqttInBound())
                .handle(actionDispatchManager)
                .get();
    }

    @Bean
    public MessageProducerSupport getMqttInBound() {
        String[] allTopics = getActionHandlers().parallelStream()
                .flatMap(actionHandler -> actionHandler.getTopics().stream())
                .map(topic -> topic.replaceAll("\\{(.*?)\\}", "+"))
                .toArray(String[]::new);

        int[] allQos = getActionHandlers().parallelStream()
                .flatMap(actionHandler -> actionHandler.getQos().stream())
                .mapToInt(Integer::intValue)
                .toArray();

        MqttPahoMessageDrivenChannelAdapter messageDrivenChannelAdapter = new MqttPahoMessageDrivenChannelAdapter("exhibitManager",
                getMqttClientFactory(), allTopics);

        messageDrivenChannelAdapter.setCompletionTimeout(5000);
        messageDrivenChannelAdapter.setConverter(getMessageConverter());
        messageDrivenChannelAdapter.setQos(allQos);

        return messageDrivenChannelAdapter;
    }


    @Bean
    @ServiceActivator(inputChannel = "mqttOutBoundChannel")
    public MessageHandler getMqttOutBound() {
        MqttPahoMessageHandler mqttMessageHandler = new MqttPahoMessageHandler("exhibitManager", getMqttClientFactory());
        mqttMessageHandler.setAsync(true);
        mqttMessageHandler.setDefaultTopic("/test");

        return mqttMessageHandler;
    }

    @MessagingGateway(defaultRequestChannel = "mqttOutBoundChannel")
    public interface MqttGateway {
        void sendMessage(byte[] message);
    }

    @Bean
    public MqttPahoClientFactory getMqttClientFactory() {
        DefaultMqttPahoClientFactory mqttClientFactory = new DefaultMqttPahoClientFactory();
        mqttClientFactory.setServerURIs(String.format("tcp://%s:%s", mqttClientProperties.getHost(), mqttClientProperties.getPort()));
        mqttClientFactory.setWill(new DefaultMqttPahoClientFactory.Will("disconnect", "test".getBytes(), 0, false));
        mqttClientFactory.setCleanSession(false);

        return mqttClientFactory;
    }

    @Bean
    public MqttMessageConverter getMessageConverter() {
        DefaultPahoMessageConverter messageConverter = new DefaultPahoMessageConverter();
        messageConverter.setPayloadAsBytes(true);

        return messageConverter;
    }

    @Bean
    public List<ActionHandler> getActionHandlers() {
        return actionHandlerManager.getActionHandlers()
                .collect(Collectors.toList());
    }
}

使用此配置,客户端会断开连接并重新连接多次。作为具有相同clientId的发布者和订阅者连接到主题的最佳配置是什么?

0 个答案:

没有答案