Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring数据流InboundChannelAdapter具有不同返回类型的不同行为_Spring_Spring Data_Spring Cloud Stream - Fatal编程技术网

Spring数据流InboundChannelAdapter具有不同返回类型的不同行为

Spring数据流InboundChannelAdapter具有不同返回类型的不同行为,spring,spring-data,spring-cloud-stream,Spring,Spring Data,Spring Cloud Stream,我使用的是spring cloud stream,面临一个问题,当我使用返回类型为MessageSource的InboundChannelAdapter时,它的行为就像一个单例类,每1秒运行一次,并将相同的数据发送给消费者。此外,当应用程序启动时,记录器仅打印一次 @InboundChannelAdapter(value = Source.OUTPUT, poller = @Poller(fixedDelay = "1000", maxMessagesPerPoll = "1")) p

我使用的是spring cloud stream,面临一个问题,当我使用返回类型为MessageSource的InboundChannelAdapter时,它的行为就像一个单例类,每1秒运行一次,并将相同的数据发送给消费者。此外,当应用程序启动时,记录器仅打印一次

 @InboundChannelAdapter(value = Source.OUTPUT, poller = @Poller(fixedDelay = "1000", maxMessagesPerPoll = "1"))
    public MessageSource<String> uuidSource() {
        UuidCaller uuidCaller = new UuidCaller(atomicLong.addAndGet(1), new Date(), UUID.randomUUID().toString());
        logger.info("buid request:"+uuidCaller);
        return () ->  MessageBuilder.withPayload(uuidCaller.toString()).build();
    }
它将消费者发送到更新的数据,并每秒打印更新日志


所以我的问题是,为什么不同的返回类型会有不同的行为?

当它是一个消息源时,它还必须用“@Bean”注释。因此,UUID只创建一次。当它是POJO方法时,会在每次轮询时创建它

如果将UUID移动到lambda中,它们的工作方式相同

编辑

@Bean
@InboundChannelAdapter(value = Source.OUTPUT, poller = @Poller(fixedDelay = "1000", maxMessagesPerPoll = "1"))
public MessageSource<String> uuidSource() {
    return () -> {
        UuidCaller uuidCaller = new UuidCaller(atomicLong.addAndGet(1), new Date(), UUID.randomUUID().toString());
        logger.info("buid request:"+uuidCaller);
        return MessageBuilder.withPayload(uuidCaller.toString()).build();
    };
}
@Bean
@InboundChannelAdapter(value=Source.OUTPUT,poller=@poller(fixedDelay=“1000”,maxMessagesPerPoll=“1”))
public MessageSource uuidSource(){
返回()->{
UuidCaller UuidCaller=新的UuidCaller(atomicLong.addAndGet(1),new Date(),UUID.randomUUID().toString());
logger.info(“生成请求:+uuidcall”);
返回MessageBuilder.withPayload(uuidCaller.toString()).build();
};
}

Hello Gray,感谢您的回复,我在MessageSource上添加了@Bean,但它仍然是每1秒发送一份UuidCaller的副本(作为singleton),而不打印任何内容(日志)。你能写下如何将UUID移动到lambda中(例如代码)吗。非常感谢。谢谢,先生。我是春天队的超级粉丝。谢谢你宝贵的回复。
@Bean
@InboundChannelAdapter(value = Source.OUTPUT, poller = @Poller(fixedDelay = "1000", maxMessagesPerPoll = "1"))
public MessageSource<String> uuidSource() {
    return () -> {
        UuidCaller uuidCaller = new UuidCaller(atomicLong.addAndGet(1), new Date(), UUID.randomUUID().toString());
        logger.info("buid request:"+uuidCaller);
        return MessageBuilder.withPayload(uuidCaller.toString()).build();
    };
}