Interface Outbox
- All Known Implementing Classes:
Outboxes.DurableQueueBasedOutboxes.DurableQueueBasedOutbox
public interface Outbox
The
The
Instead, you need to use an
The message is added to the
If the transaction fails then both the entity and the message will be rolled back when then
After the
The
This means that the Message consumer, registered with the
Outbox supports the transactional Store and Forward pattern from Enterprise Integration Patterns supporting At-Least-Once delivery guarantee.The
Outbox pattern is used to handle outgoing messages, that are created as a side effect of adding/updating an entity in a database, but where the message infrastructure
(such as a Queue, Kafka, EventBus, etc.) that doesn't share the same underlying transactional resource as the database.Instead, you need to use an
Outbox that can join in the same UnitOfWork/transactional-resource
that the database is using.The message is added to the
Outbox in a transaction/UnitOfWork and afterwards the UnitOfWork is committed.If the transaction fails then both the entity and the message will be rolled back when then
UnitOfWork rolls back.After the
UnitOfWork has been committed, the messages will be asynchronously delivered to the message consumer in a new UnitOfWork.The
Outbox itself supports Message Redelivery in case the Message consumer experiences failures.This means that the Message consumer, registered with the
Outbox, can and will receive Messages more than once and therefore its message handling has to be idempotent.
If you're working with OrderedMessage's then the Outbox consumer must be configured
with OutboxConfig.getMessageConsumptionMode() having value MessageConsumptionMode.SingleGlobalConsumer
in order to be able to guarantee that OrderedMessage's are delivered in OrderedMessage.getOrder() per OrderedMessage.getKey()
across as many OutboxConfig.numberOfParallelMessageConsumers as you wish to use.
-
Method Summary
Modifier and TypeMethodDescriptionStart consuming messages from the Outbox using the provided message consumer.
This is the same as callingsetMessageConsumer(Consumer)followed bystartConsuming()
Only needs to be called if the instance was created without a message consumer such as viaOutboxes.getOrCreateOutbox(OutboxConfig)longGet the number of message in the outbox that haven't been sent yetbooleanHas the instance been created with a Message consumer or hasconsume(Consumer)been calledbooleanIs the provided Message consumer consuming messages from theOutboxname()The name of the outboxsendMessage(Message message) Send a message asynchronously.
This message will be stored durably (without any duplication check) in connection with the currently activeUnitOfWork(or a newUnitOfWorkwill be created in case no there isn't an activeUnitOfWork).
The message will be delivered asynchronously to the message consumerdefault OutboxsendMessage(Object payload) Send a message (without meta-data) asynchronously.
This message will be stored durably (without any duplication check) in connection with the currently activeUnitOfWork(or a newUnitOfWorkwill be created in case no there isn't an activeUnitOfWork).
The message will be delivered asynchronously to the message consumerdefault OutboxsendMessage(Object payload, MessageMetaData metaData) Send a message (with meta-data) asynchronously.
This message will be stored durably (without any duplication check) in connection with the currently activeUnitOfWork(or a newUnitOfWorkwill be created in case no there isn't an activeUnitOfWork).
The message will be delivered asynchronously to the message consumersetMessageConsumer(Consumer<Message> messageConsumer) Set the message consumer.Start consuming messages from theOutbox.Stop consuming messages from theOutbox.
-
Method Details
-
consume
Start consuming messages from the Outbox using the provided message consumer.
This is the same as callingsetMessageConsumer(Consumer)followed bystartConsuming()
Only needs to be called if the instance was created without a message consumer such as viaOutboxes.getOrCreateOutbox(OutboxConfig)If an
OrderedMessageis delivered via anOutboxusing aFencedLock(such as theOutboxes.durableQueueBasedOutboxes(DurableQueues, FencedLockManager)) to coordinate message consumption, then you can find theFencedLock.getCurrentToken()of the consumer in theMessage.getMetaData()under keyMessageMetaData.FENCED_LOCK_TOKEN- Parameters:
messageConsumer- the message consumer. SeePatternMatchingMessageHandler- Returns:
- this outbox instance
-
setMessageConsumer
Set the message consumer. To start consuming callstartConsuming()- Parameters:
messageConsumer- the message consumer. SeePatternMatchingMessageHandler- Returns:
- this outbox instance
-
startConsuming
Outbox startConsuming()Start consuming messages from theOutbox. Requires callingsetMessageConsumer(Consumer)first
If anOrderedMessageis delivered via anOutboxusing aFencedLock(such asOutboxes.durableQueueBasedOutboxes(DurableQueues, FencedLockManager)to coordinate message consumption, then you can find theFencedLock.getCurrentToken()of the consumer in theMessage.getMetaData()under keyMessageMetaData.FENCED_LOCK_TOKEN- Returns:
- this outbox instance
-
stopConsuming
Outbox stopConsuming()Stop consuming messages from theOutbox. Calling this method will remove the message consumer and to resume message consumption you need to callconsume(Consumer)- Returns:
- this outbox instance
-
hasAMessageConsumer
boolean hasAMessageConsumer()Has the instance been created with a Message consumer or hasconsume(Consumer)been called- Returns:
- Has the instance been created with a Message consumer or has
consume(Consumer)been called
-
isConsumingMessages
boolean isConsumingMessages()Is the provided Message consumer consuming messages from theOutbox- Returns:
- Is the provided Message consumer consuming messages from the
Outbox
-
name
OutboxName name()The name of the outbox- Returns:
- the name of the outbox
-
sendMessage
Send a message (without meta-data) asynchronously.
This message will be stored durably (without any duplication check) in connection with the currently activeUnitOfWork(or a newUnitOfWorkwill be created in case no there isn't an activeUnitOfWork).
The message will be delivered asynchronously to the message consumer- Parameters:
payload- the message payload- Returns:
- this outbox instance
-
sendMessage
Send a message (with meta-data) asynchronously.
This message will be stored durably (without any duplication check) in connection with the currently activeUnitOfWork(or a newUnitOfWorkwill be created in case no there isn't an activeUnitOfWork).
The message will be delivered asynchronously to the message consumer- Parameters:
payload- the message payloadmetaData- the message meta-data- Returns:
- this outbox instance
-
sendMessage
Send a message asynchronously.
This message will be stored durably (without any duplication check) in connection with the currently activeUnitOfWork(or a newUnitOfWorkwill be created in case no there isn't an activeUnitOfWork).
The message will be delivered asynchronously to the message consumer- Parameters:
message- the message- Returns:
- this outbox instance
- See Also:
-
getNumberOfOutgoingMessages
long getNumberOfOutgoingMessages()Get the number of message in the outbox that haven't been sent yet- Returns:
- Get the number of message in the outbox that haven't been sent yet
-