使用 Spring 集成的发件箱模式

问题描述 投票:0回答:2

我们想通过使用 spring 集成来实现发件箱模式。 从这个例子开始,我们想出了这个更简单的解决方案:

protected IntegrationFlowDefinition<?> buildFlow() {
    return from("domain.event.sender")
            .channel(c -> c.queue(this.jdbcMessageStore, "outbox"))
            .handle(
                    this.messagePublisherHandler,
                    c -> c.poller(poller -> poller.fixedDelay(1000).transactional()));
}

此解决方案将从“domain.event.sender”获取消息,将其存储在数据库中(如果出现错误),然后轮询该数据库以将消息发送到 MessageBus(通过 messagePublisherHandler)。

我们希望有一个解决方案,在事务同步后立即处理此消息,而无需等待轮询器完成。

java microservices spring-integration outbox-pattern
2个回答
1
投票

如果您能够展示如何在将业务数据发送到此

domain.event.sender
通道以存储到所谓的发件箱表中之前对其进行处理,那就太好了。

但是,将其

domain.event.sender
制作为
PublishSubscribeChannel
并拥有另一个订阅者来执行您所要求的逻辑非常简单。如果您没有为
TaskExecutor
配置此通道,则两个订阅者将在同一业务事务中按顺序处理相同的消息。

在文档中查看更多信息:https://docs.spring.io/spring-integration/reference/channel/implementations.html#channel-implementations-publishsubscribechannel


0
投票

这是一个愚蠢的答案。我只需要将

fixedDelay
更改为
0
:

protected IntegrationFlowDefinition<?> buildFlow() {
    return from("domain.event.sender")
            .channel(c -> c.queue(this.jdbcMessageStore, "outbox"))
            .handle(
                    this.messagePublisherHandler,
                    c -> c.poller(poller -> poller.fixedDelay(0).transactional()));
}
© www.soinside.com 2019 - 2024. All rights reserved.