![](/img/trans.png)
[英]JPA outbound channel adapter config in Spring Integration Java DSL
[英]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
用於具有fixedDelay
的FileWritingMessageHandler
。 但是,請記住,在輪詢器工作之前,消息將被存儲在內存中。 因此,一旦應用程序崩潰,消息就會丟失。
另一方面,可以使用具有類似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.