我们可以使用spring集成批处理mosquitto中的10个消息加载组

时间:2017-08-02 12:17:09

标签: spring spring-integration mqtt spring-integration-mqtt

这就是我如何使用spring integration定义我的mqtt连接。我不确定这是否可行bt我们可以设置一个mqtt用户在获得10个消息后工作。现在订阅者按照应该发布消息后工作。

    @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;
    }
 }

1 个答案:

答案 0 :(得分:1)

  

我需要一次批量处理10条消息组

这不是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; 使用这个语义:

@ServiceActivator

因此,我们无法根据Paho客户端在此渠道适配器上批量处理它们。

我们可以从Spring Integration的角度建议您使用Aggregator EIP实现。

在您发送到路由器之前,您应该在AggregatorFactoryBean之前为@Bean mqttRouterChannel添加@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; }

这可能很简单:

@mixin sharedStyles{
  //shared nested styles go here
}

.parentA{
  margin-top:74px;
  @include sharedStyles;
}

.parentB{
  margin-top: 50px;
  @include sharedStyles;
}

请参阅Reference Manual

中的详细信息