AMQP supports two kinds of transactions:
1. Automatic transactions, in which every published message and acknowledgement is processed as a stand-alone transaction.
2. Server local transactions, in which the server will buffer published messages and acknowledgements and commit them on demand from the client.
The Transaction class (“tx”) gives applications access to the second type, namely server transactions. The semantics of this class are:
1. The application asks for server transactions in each channel where it wants these transactions (Select).
2. The application does work (Publish, Ack).
3. The application commits or rolls-back the work (Commit, Roll-back).
4. The application does work, ad infinitum.
Transactions cover published contents and acknowledgements, not deliveries. Thus, a rollback does not requeue or redeliver any messages, and a client is entitled to acknowledge these messages in a following transaction.
The Transaction methods allows publish and ack operations to be batched into atomic units of work. The intention is that all publish and ack requests issued within a transaction will complete successfully or none of them will.
The method StartTransaction starts a new transaction in the server, the client uses this method at least once on a channel before using the Commit or Rollback methods. The event OnAMQPTransactionOk is raised when the server acknowledges the use of transactions.
AMQP.StartTransaction('channel_name');
procedure OnAMQPTransactionOk(Sender: TObject; const aChannel: string; aTransaction: TsgcAMQPTransaction);
begin
case aTransaction of
amqpTransactionSelect:
DoLog('#AMQP_transaction_ok: [' + aChannel + '] select');
amqpTransactionCommit:
DoLog('#AMQP_transaction_ok: [' + aChannel + '] commit');
amqpTransactionRollback:
DoLog('#AMQP_transaction_ok: [' + aChannel + '] rollback');
end;
end;
A Synchronous call can be done just calling the method StartTransactionEx, this method returns true if the request has been processed, otherwise the result will be false.
This method commits all message publications and acknowledgments performed in the current transaction. A new transaction starts immediately after a commit. The event OnAMQPTransactionOk is raised when the server acknowledges the use of transactions.
AMQP.CommitTransaction('channel_name');
A Synchronous call can be done just calling the method CommitTansactionEx, this method returns true if the request has been processed, otherwise the result will be false.
This method abandons all message publications and acknowledgments performed in the current transaction. A new transaction starts immediately after a rollback. Note that unacked messages will not be automatically redelivered by rollback; if that is required an explicit recover call should be issued. The event OnAMQPTransactionOk is raised when the server acknowledges the use of transactions.
AMQP.RollbackTransaction('channel_name');
A Synchronous call can be done just calling the method RollbackTransactionEx, this method returns true if the request has been processed, otherwise the result will be false.