簡體   English   中英

如何暫停Spring雲數據流Source class向kafka發送數據?

[英]How to pause the Spring cloud data flow Source class from sending data to kafka?

我正在開發 spring 雲數據流應用程序,以下是代碼片段

    @Bean
    @InboundChannelAdapter(channel = TbeSource.PR1, poller = @Poller(fixedDelay = "2000"))
    public MessageSource<Product> getProductSource(ProductBuilder dataAccess) {

        return new MessageSource<Product>() {
            @SneakyThrows
            @Override
            public Message<Product> receive() {
                System.out.println("calling method");
                return MessageBuilder.withPayload(dataAccess.getNext()).build();
            }
        };
    }

在上面的代碼中, getNext()方法將從數據庫中獲取數據並返回 object,所以如果數據被完全讀取,那么它將返回 null

我們無法將 null 返回到此消息源。

那么是否有任何選項可用於在我們需要時暫停和恢復此源連接 class?

有沒有人面對/克服過這種情況?

首先,您可以使用Supplier<Product>而不是MessageSource ,您的代碼將如下所示:

return () -> dataAccess.getNext();

null結果在此處有效,在這種情況下不會發出任何消息,也不會出現錯誤,因為框架正確處理null結果。

當方法調用的結果是@InboundChannelAdapter時,您仍然可以在該null上擁有空閑功能。 因此,您需要查看SimpleActiveIdleMessageSourceAdvice 有關更多信息,請參閱文檔: https://docs.spring.io/spring-integration/docs/5.3.4.RELEASE/reference/html/core.html#simpleactiveidlereceivemessageadvice

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM