簡體   English   中英

與DSL的Spring集成:間隔10分鍾后,文件出站通道適配器可以創建文件嗎?

[英]Spring Integration with DSL: Can File Outbound Channel Adapter create file after say 10 mins of interval

我有一個要求,我的應用程序應該從MQ讀取消息並使用文件出站通道適配器進行寫入。 我希望每個輸出文件都應包含每10分鍾間隔的消息。 是否存在任何默認實現,或執行此操作的任何指針。

    public @Bean IntegrationFlow defaultJmsFlow()
{
    return IntegrationFlows.from(

            //read JMS topic
            Jms.messageDrivenChannelAdapter(this.connectionFactory).destination(this.config.getInputQueueName()).errorChannel(errorChannel()).configureListenerContainer(c ->
            {
                final DefaultMessageListenerContainer container = c.get();
                container.setSessionTransacted(true);
                container.setMaxMessagesPerTask(-1);

            }).get())

            .channel(messageProcessingChannel()).get();
}

public @Bean MessageChannel messageProcessingChannel()
{
    return MessageChannels.queue().get();
}

public @Bean IntegrationFlow messageProcessingFlow() {
    return IntegrationFlows.from(messageProcessingChannel())

            .handle(Files.outboundAdapter(new File(config.getWorkingDir()))
                    .fileNameGenerator(fileNameGenerator())
                    .fileExistsMode(FileExistsMode.APPEND).appendNewLine(true))
            .get();
}

首先,您可以使用類似QueueChannel ,在這10分鍾內將終結QueueChannel上的poller用於具有fixedDelayFileWritingMessageHandler 但是,請記住,在輪詢器工作之前,消息將被存儲在內存中。 因此,一旦應用程序崩潰,消息就會丟失。

另一方面,可以使用具有類似poller配置的JmsDestinationPollingSource 但是,通過這種方式,您需要使用maxMessagesPerPoll(-1)對其進行配置,以使其在單個輪詢任務期間從MQ提取盡可能多的消息-每10分鍾一次。

aggregator及其groupTimeout選項可以實現另一個變體。 這樣,直到經過10分鍾的間隔,聚合器才會輸出消息。 但是再次提醒:默認情況下,存儲區在內存中。 當我們已經有一個MQ並且我們確實可以輪詢時,我不會再引入一個持久性存儲來滿足周期性的需求。 因此,我將使用JmsDestinationPollingSource變體。

UPDATE

您能為我提供如何在文件出站適配器中設置固定延遲的信息嗎?

由於您處理QueueChannel ,因此需要為“固定延遲”配置一個PollingConsumer端點。 這個確實屬於該頻道的訂戶。 實際上,這是一個.handle(Files.outboundAdapter)部分。 只有您缺少的一點是Poller是端點的一個選項,而不是MessageHandler 考慮使用重載的handle()變體:

.handle(Files.outboundAdapter(new File(config.getWorkingDir()))
                .fileNameGenerator(fileNameGenerator())
                .fileExistsMode(FileExistsMode.APPEND).appendNewLine(true),
       e -> e.poller(p -> p.fixedDelay(10000)))

JMSDestinationPollingSource的示例示例

    @Bean
    public IntegrationFlow jmsInboundFlow() {
        return IntegrationFlows
                .from(Jms.inboundAdapter(cachingConnectionFactory())
                        .destination("jmsInbound"),
                        e -> e.poller(p -> p.fixedDelay(10000)))
                .<String, String>transform(String::toUpperCase)
                .channel(jmsOutboundInboundReplyChannel())
                .get();
    }

暫無
暫無

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

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