为什么不从QueueChannel轮询所有消息?

时间:2018-11-06 17:24:13

标签: spring-integration spring-integration-dsl

我用QueueChannel创建了一个capacity=500,并在那里发送了1000条消息。并非全部打印;最后一个是567。为什么会这样?

代码如下:

@SpringBootApplication
@IntegrationComponentScan
public class QueueChannelResearch {

    @Bean
    public IntegrationFlow lambdaFlow() {
        return f -> f.channel(c -> c.queue(500))

            .handle(System.out::println);
    }

    public static void main(String[] args) {
        ConfigurableApplicationContext ctx = SpringApplication.run(QueueChannelResearch.class, args);

        MessageChannel inputChannel = ctx.getBean("lambdaFlow.input", MessageChannel.class);
        for (int i = 0; i < 1000; i++) {
            inputChannel.send(MessageBuilder.withPayload("w" + i)
                .build());
        }

        ctx.close();

    }

}

以下是输出:

GenericMessage [payload=w1, headers={id=d97946f2-1cf6-d681-fa88-08a4e708e61e, timestamp=1541524850590}]
...
GenericMessage [payload=w567, headers={id=83ab8720-f1c1-a4b1-b2ac-2a24a93bd00c, timestamp=1541524850590}]
GenericMessage [payload=w566, headers={id=d97946f2-1cf6-d681-fa88-08a4e708e61e, timestamp=1541524850590}]
GenericMessage [payload=w567, headers={id=83ab8720-f1c1-a4b1-b2ac-2a24a93bd00c, timestamp=1541524850590}]

1 个答案:

答案 0 :(得分:2)

由于消息是从单独的调度线程中从队列中轮询的,因此您确实需要等到收到所有消息之后。

由于您的应用程序中没有任何挂钩来跟踪消息,因此我只能建议在Thread.sleep(10000)之前添加一个ctx.close()

或者您可以在控制台中添加一个挂钩以等待用户输入:

    System.out.println("Hit 'Enter' to terminate");
    System.in.read();
    ctx.close();

或者只是不关闭ctx并依靠JVM终止。

相关问题