Outbox pattern with Spring Integration

192 views Asked by At

We want to implement an Outbox pattern by using spring integration. Starting from this example, we came up with this simpeler solution:

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()));
}

This solution will take a message from "domain.event.sender", store it in the database (in case of an error) and then POLL this db to send a message to a MessageBus (via messagePublisherHandler).

We want to have a solution that will ALSO process this message immediately after transaction synchronisation, without that we have to wait for the poller to finish.

2

There are 2 answers

0
user969039 On BEST ANSWER

It was a stupid answer. I just needed to change the fixedDelay to 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()));
}
0
Artem Bilan On

It would be great if you show how you process a business data before sending it into this domain.event.sender channel for storing into so-called outbox table.

However it is just pretty simple to make this domain.event.sender as a PublishSubscribeChannel and have another subscriber which would do the logic you are asking. If you don't configure this channel for a TaskExecutor, both subscribers will process the same message sequentially and withing the same business transaction.

See more info in docs: https://docs.spring.io/spring-integration/reference/channel/implementations.html#channel-implementations-publishsubscribechannel