Interface Outbox
-
- All Known Implementing Classes:
Outboxes.DurableQueueBasedOutboxes.DurableQueueBasedOutbox
public interface OutboxTheOutboxsupports the transactional Store and Forward pattern from Enterprise Integration Patterns supporting At-Least-Once delivery guarantee.
TheOutboxpattern 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 anOutboxthat can join in the sameUnitOfWork/transactional-resource that the database is using.
The message is added to theOutboxin a transaction/UnitOfWorkand afterwards theUnitOfWorkis committed.
If the transaction fails then both the entity and the message will be rolled back when thenUnitOfWorkrolls back.
After theUnitOfWorkhas been committed, the messages will be asynchronously delivered to the message consumer in a newUnitOfWork.
TheOutboxitself supports Message Redelivery in case the Message consumer experiences failures.
This means that the Message consumer, registered with theOutbox, 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 theOutboxconsumer must be configured withOutboxConfig.getMessageConsumptionMode()having valueMessageConsumptionMode.SingleGlobalConsumerin order to be able to guarantee thatOrderedMessage's are delivered inOrderedMessage.getOrder()perOrderedMessage.getKey()across as manyOutboxConfig.numberOfParallelMessageConsumersas you wish to use.
-
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description Outboxconsume(Consumer<Message> messageConsumer)Start consuming messages from the Outbox using the provided message consumer.
Only needs to be called if the instance was created without a message consumerlonggetNumberOfOutgoingMessages()Get the number of message in the outbox that haven't been sent yetbooleanhasAMessageConsumer()Has the instance been created with a Message consumer or hasconsume(Consumer)been calledbooleanisConsumingMessages()Is the provided Message consumer consuming messages from theOutboxOutboxNamename()The name of the outboxvoidsendMessage(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 voidsendMessage(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 voidsendMessage(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 consumervoidstopConsuming()Stop consuming messages from theOutbox.
-
-
-
Method Detail
-
consume
Outbox consume(Consumer<Message> messageConsumer)
Start consuming messages from the Outbox using the provided message consumer.
Only needs to be called if the instance was created without a message consumerIf 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
-
stopConsuming
void stopConsuming()
Stop consuming messages from theOutbox. Calling this method will remove the message consumer and to resume message consumption you need to callconsume(Consumer)
-
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
default void sendMessage(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 consumer- Parameters:
payload- the message payload
-
sendMessage
default void sendMessage(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 consumer- Parameters:
payload- the message payloadmetaData- the message meta-data
-
sendMessage
void sendMessage(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 consumer- Parameters:
message- the message- See Also:
OrderedMessage
-
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
-
-