Net::Stomp::Producer::Transactional - subclass of Net::Stomp::Producer with transaction-like behaviour
version 2.005
my $p = Net::Stomp::Producer::Transactional->new({ servers => [ { hostname => 'localhost', port => 61613, } ], }); $p->txn_begin(); $p->send('/queue/somewhere', { type => 'my_message' }, 'body contents'); # nothing sent yet # some time later $p->txn_commit(); # all messages are sent now
Also:
$p->txn_do(sub{ # do something... $p->send(@msg1); # do something else... $p->send(@msg2); }); # all messages are sent now, unless an exception was thrown
A subclass of Net::Stomp::Producer, this class adds some transaction-like behaviour.
If you call "txn_begin", the messages sent through this object will be kept in memory instead of being actually sent to the STOMP connection. They will be sent when you call "txn_commit",
There is also a "txn_do" method, which takes a coderef and executes it between a "txn_begin" and a "txn_commit". If the coderef throws an exception, the messages are forgotten.
Please remember that this has nothing to do with STOMP transactions, nor with the "transactional_sending" in Net::Stomp::Producer attribute. We could, in future, re-implement this to delegate transactional behaviour to the broker via STOMP's BEGIN and COMMIT frames. At the moment we do everything client-side.
BEGIN
COMMIT
in_transaction
If true, we are inside a "transaction". You can change this with "txn_begin", "txn_commit" and "txn_rollback".
txn_begin
Start a transaction, so that subsequent calls to send or transform_and_send won't really send messages to the connection, but keep them in memory.
send
transform_and_send
You can call this method multiple times; the transaction will end (and messages will be sent) when you call "txn_commit" as many times as you called txn_begin.
Calling "txn_rollback" will destroy the messages sent since the most recent txn_begin. In other words, transactions are properly re-entrant.
txn_commit
Commit the current transaction. If this was the outer-most transaction, send all buffered messages.
If you call this method outside of a transaction, you'll get a Net::Stomp::Producer::Exceptions::Transactional exception.
txn_rollback
Roll back the current transaction, destroying all messages "sent" inside it.
If not "in_transaction", send the message normally; otherwise, add it to the in-memory buffer. See the base method for more details.
txn_do
$p->txn_do(sub { $p->send(@something); });
This method executes the given coderef between a "txn_begin" and a "txn_commit".
If the coderef throws an exception, "txn_rollback" will be called, and the exception re-thrown.
This method is re-entrant:
$p->txn_do(sub { $p->send(@msg1); eval { $p->txn_do(sub { $p->send(@msg2); die "boom\n"; }); }; $p->send(@msg3); });
The first and thind messages will be sent, the second one will not.
Gianni Ceccarelli <gianni.ceccarelli@net-a-porter.com>
This software is copyright (c) 2012 by Net-a-porter.com.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install Net::Stomp::Producer, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Net::Stomp::Producer
CPAN shell
perl -MCPAN -e shell install Net::Stomp::Producer
For more information on module installation, please visit the detailed CPAN module installation guide.