Using STOMP Transactions with Best STOMP¶
STOMP transactions are a powerful feature that allows grouping multiple operations into a single unit. These transactions ensure that all operations within them either succeed or fail together. Best STOMP's implementation of STOMP transactions offers a reliable way to handle grouped messaging tasks in Unity.
Understanding STOMP Transactions¶
STOMP transactions are used to execute a series of messaging operations as a single atomic operation. This is particularly useful in scenarios where the consistency of a set of messages is critical.
When to Use Transactions¶
- Batch Message Processing: When multiple messages need to be processed together.
- Consistency Guarantee: To ensure that either all messages are processed successfully or none are, maintaining data consistency.
- Error Handling: Transactions allow for easier error handling in complex messaging scenarios.
The Transaction Class¶
The Transaction class in Best STOMP represents a STOMP transaction. It includes methods to begin, commit, or abort transactions.
Key Properties and Methods¶
- Id: Unique identifier of the transaction.
- Session: The STOMP client session associated with the transaction.
- Begin(callback)/BeginAsync(): Starts the transaction with an optional acknowledgment callback.
- Commit(callback)/CommitAsync(): Commits all operations within the transaction.
- Abort(callback)/AbortAsync(): Aborts the transaction, discarding all operations.
Implementing Transactions in Best STOMP¶
Creating a Transaction¶
To create a transaction, use the CreateTransaction method of the Client object:
Creating a Transaction | |
---|---|
Beginning a Transaction¶
Start the transaction using the Begin
method. Optionally, provide a callback to handle acknowledgment:
Sending Messages in a Transaction¶
Use CreateMessageBuilder
to construct messages. Specify the transaction using WithTransaction
:
Committing a Transaction¶
Commit the transaction to apply all operations:
Aborting a Transaction¶
If needed, abort the transaction to discard all operations:
Full Example¶
Here's a complete example demonstrating the use of transactions in Best STOMP:
Example
And running it should produce an output like this:
Messages are sent by the broker only after the client is committed the transaction!
Why Use Transactions?¶
- Atomic Operations: Ensures that all operations in a transaction are completed successfully or none at all.
- Error Recovery: Simplifies error handling by allowing rollback of operations in case of failure.
- Data Integrity: Maintains the consistency of message processing and delivery.
STOMP transactions in Best STOMP are essential for scenarios where the integrity of a series of messaging operations is crucial. They provide a structured way to manage complex messaging tasks, ensuring consistency and reliability in your Unity applications.