简体   繁体   English

我们可以使用Spring Integration在mosquitto中批量处理10个消息负载的组吗

[英]can we batch up groups of 10 message load in mosquitto using spring integration

this is how i have defined my mqtt connection using spring integration.i am not sure whether this is possible bt can we setup a mqtt subscriber works after getting a 10 load of messages. 这是我使用spring集成定义mqtt连接的方式。我不确定是否有可能,我们能否在获取10条消息后设置mqtt订阅服务器工作。 right now subscriber works after publishing a message as it should. 现在,订阅者可以在发布消息后正常工作。

    @Autowired
    ConnectorConfig config;


    @Bean
    public MqttPahoClientFactory mqttClientFactory() {
        DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory();
        factory.setServerURIs(config.getUrl());
        factory.setUserName(config.getUser());
        factory.setPassword(config.getPass());
        return factory;
    }

    @Bean
    public MessageProducer inbound() {
        MqttPahoMessageDrivenChannelAdapter adapter =
                new MqttPahoMessageDrivenChannelAdapter(config.getClientid(), mqttClientFactory(), "ALERT", "READING");

        adapter.setCompletionTimeout(5000);
        adapter.setConverter(new DefaultPahoMessageConverter());
        adapter.setQos(1);
        adapter.setOutputChannel(mqttRouterChannel());
        return adapter;
    }

   /**this is router**/
   @MessageEndpoint
   public class MessageRouter {

    private final Logger logger = LoggerFactory.getLogger(MessageRouter.class);


    static final String  ALERT = "ALERT";
    static final String  READING = "READING";

    @Router(inputChannel = "mqttRouterChannel")
    public String route(@Header("mqtt_topic") String topic){
        String route = null;
        switch (topic){
            case ALERT:
                logger.info("alert message received");
                route = "alertTransformerChannel";
                break;
            case READING:
                logger.info("reading message received");
                route = "readingTransformerChannel";
                break;
        }
        return route;
    }
 }

i need to batch up groups of 10 messages at a time 我需要一次批处理10条消息

That is not a MqttPahoMessageDrivenChannelAdapter responsibility. 这不是MqttPahoMessageDrivenChannelAdapter责任。

We use there MqttCallback with this semantic: 我们使用带有以下语义的MqttCallback

 * @param topic name of the topic on the message was published to
 * @param message the actual message.
 * @throws Exception if a terminal error has occurred, and the client should be
 * shut down.
 */
public void messageArrived(String topic, MqttMessage message) throws Exception;

So, we can't batch them there on this Channel Adapter by nature of the Paho client. 因此,根据Paho客户端的性质,我们无法在此Channel Adapter上将它们批处理在那里。

What we can suggest you from the Spring Integration perspective is an Aggregator EIP implementation. 从Spring Integration角度我们可以建议您的是Aggregator EIP实现。

In your case you should add @ServiceActivator for the AggregatorFactoryBean @Bean before that mqttRouterChannel , before sending to the router. 在您的情况下,应在发送到路由器之前,在该mqttRouterChannel之前为AggregatorFactoryBean @Bean添加@ServiceActivator

That maybe as simple as: 那也许很简单:

@Bean
@ServiceActivator(inputChannel = "mqttAggregatorChannel")
AggregatorFactoryBean mqttAggregator() {
    AggregatorFactoryBean aggregator = new AggregatorFactoryBean();
    aggregator.setProcessorBean(new DefaultAggregatingMessageGroupProcessor());
    aggregator.setCorrelationStrategy(m -> 1);
    aggregator.setReleaseStrategy(new MessageCountReleaseStrategy(10));
    aggregator.setExpireGroupsUponCompletion(true);
    aggregator.setSendPartialResultOnExpiry(true);
    aggregator.setGroupTimeoutExpression(new ValueExpression<>(1000));
    aggregator.setOutputChannelName("mqttRouterChannel");
    return aggregator;
}

See more information in the Reference Manual . 请参阅《 参考手册》中的更多信息。

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

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