简体   繁体   English

SI订阅了多个mqtt主题

[英]SI subscription to multiple mqtt topics

I'm trying to learn how to handle MQTT Messages in Spring-Integration . 我正在尝试学习如何在Spring-Integration中处理MQTT消息 Have created a converter, that subscribes with a single MqttPahoMessageDrivenChannelAdapter per MQTT Topic for consuming and converting the messages. 已创建一个转换器,每个MQTT主题都订阅一个MqttPahoMessageDrivenChannelAdapter来转换和使用消息。

The problem is our data provider is planning to "speed-up" publishing messages on his side. 问题在于我们的数据提供商正计划“加快”他一方的发布消息。 So instead of having a few(<=10) topics each of which has messages with about 150 fields it is planned to publish each of those fields to the separate MQTT topic. 因此,计划将每个字段都发布到单独的MQTT主题,而不是只包含几个主题(<= 10),每个主题都具有包含约150个字段的消息。

This means my converter would have to consume ca. 这意味着我的转换器将不得不消耗约。 1000 mqtt topics, but I do not know whether: 1000个mqtt主题,但我不知道是否:

  1. Is spring-integration still a good choice for it. 弹簧集成仍然是一个不错的选择。 Cause afaik. 造成afaik。 the mentioned adapter uses the PAHO MqttClient that will consume the messages from all of the topics it is subscribed to in one single thread and creating 1000 instances of those adapters is an overkill. 提到的适配器使用PAHO MqttClient,它将在一个线程中使用它所订阅的所有主题的消息,而创建这些适配器的1000个实例实在是太过分了。
  2. If we stick further to spring-integration and use the provided components, would it be a good idea to create a single inbound adapter for all of the fields, that previously were in messages of one topic but moving the conversion away from the adapter bean to a separate bean ( that does the conversion) connected with an executer-channel to the adapter and thus executing the conversion of those fields on some threadpool in parallel. 如果我们进一步坚持弹簧集成并使用提供的组件,那么为所有字段创建单个入站适配器是一个好主意,以前这些字段属于一个主题的消息,但是将转换从适配器bean转移到了与执行者通道连接到适配器的单独的bean(执行转换),从而并行地在某个线程池上执行这些字段的转换。

Thanks in advance for your answers! 预先感谢您的回答!

I think your idea makes sense. 我认为您的想法很有道理。

For that purpose you need to implement a passthrough MqttMessageConverter and provide an MqttMessage as a payload and topic as a header: 为此,您需要实现直通 MqttMessageConverter并提供MqttMessage作为payload ,并提供topic作为标头:

public class PassThroughMqttMessageConverter implements MqttMessageConverter {

    @Override
    public Message<?> toMessage(String topic, MqttMessage mqttMessage) {
        return MessageBuilder.withPayload(mqttMessage)
                .setHeader(MqttHeaders.RECEIVED_TOPIC, topic)
                .build();
    }

    @Override
    public Object fromMessage(Message<?> message, Class<?> targetClass) {
        return null;
    }

    @Override
    public Message<?> toMessage(Object payload, MessageHeaders headers) {
        return null;
    }

}

So, you really will be able to perform a target conversion downstream, after a mentioned ExecutorChannel in the custom transformer . 因此,在自定义transformer ExecutorChannel中提到了ExecutorChannel之后,您实际上将能够在下游执行目标转换。

You also may consider to implement a custom MqttPahoClientFactory (an extension of the DefaultMqttPahoClientFactory may work as well) and provide a custom ScheduledExecutorService to be injected into the MqttClient you are going create in the getClientInstance() . 您还可以考虑实现自定义MqttPahoClientFactoryDefaultMqttPahoClientFactory的扩展也可以工作),并提供一个自定义ScheduledExecutorService注入到要在getClientInstance()创建的MqttClient中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM