|
NAMENet::STOMP::Client - STOMP object oriented client moduleSYNOPSIS# # simple producer # use Net::STOMP::Client; $stomp = Net::STOMP::Client->new(host => "127.0.0.1", port => 61613); $stomp->connect(login => "guest", passcode => "guest"); $stomp->send(destination => "/queue/test", body => "hello world!"); $stomp->disconnect(); # # consumer with client side acknowledgment # use Net::STOMP::Client; $stomp = Net::STOMP::Client->new(host => "127.0.0.1", port => 61613); $stomp->connect(login => "guest", passcode => "guest"); # declare a callback to be called for each received message frame $stomp->message_callback(sub { my($self, $frame) = @_; $self->ack(frame => $frame); printf("received: %s\n", $frame->body()); return($self); }); # subscribe to the given queue $stomp->subscribe( destination => "/queue/test", id => "testsub", # required in STOMP 1.1 ack => "client", # client side acknowledgment ); # wait for a specified message frame $stomp->wait_for_frames(callback => sub { my($self, $frame) = @_; if ($frame->command() eq "MESSAGE") { # stop waiting for new frames if body is "quit" return(1) if $frame->body() eq "quit"; } # continue to wait for more frames return(0); }); $stomp->unsubscribe(id => "testsub"); $stomp->disconnect(); DESCRIPTIONThis module provides an object oriented client interface to interact with servers supporting STOMP (Streaming Text Orientated Messaging Protocol). It supports the major features of modern messaging brokers: SSL, asynchronous I/O, receipts and transactions.CONSTRUCTORThe new() method can be used to create a Net::STOMP::Client object that will later be used to interact with a server. The following attributes are supported:
Upon object creation, a TCP connection is made to the server but no data (i.e. STOMP frame) is exchanged. DEBUGGINGNet::STOMP::Client uses No::Worries::Log's log_debug() to log debugging information. In addition, to avoid useless data massaging, it also uses a debug string to specify what will be logged using log_debug().The debug string should contain a list of words describing what to log. For instance, "io" logs I/O information while "io connection" logs both I/O and connection information. Here are the supported debug words that can be used:
To enable debugging, you must first configure No::Worries::Log so that it indeed reports debugging messages. This can be done with something like: log_filter("debug"); or, to enable logging only from Net::STOMP::Client modules: log_filter("debug caller=~^Net::STOMP::Client"); See the No::Worries::Log documentation for more information. Then, you have to tell Net::STOMP::Client to indeed log what you want to see. This can be done globally for all connections by setting the global variable $Net::STOMP::Client::Debug: $Net::STOMP::Client::Debug = "connection api"; or per connection via the new() method: $stomp = Net::STOMP::Client->new( uri => "stomp://mybroker:6163", debug => "connection api", ); TIMEOUTSBy default, when sending STOMP frames, the module waits until the frame indeed has been sent (from the socket point of view). In case the server is stuck or unusable, the module can therefore hang.When creating the Net::STOMP::Client object, you can pass a "timeout" attribute to better control how certain operations handle timeouts. This attribute should contain a reference to hash with the following keys:
All values are in seconds. No timeout means wait until the operation succeeds. As a shortcut, the "timeout" attribute can also be a scalar. In this case, only the "connect" and "connected" operations use this value. STOMP METHODSWith a Net::STOMP::Client object, the following methods can be used to interact with the server. They match one-to-one the different commands that a client frame can hold:
All these methods can receive options that will be passed directly as frame headers. For instance: $stomp->subscribe( destination => "/queue/test", id => "testsub", ack => "client", ); Some methods also support additional options:
Finally, all methods support "debug" and "timeout" options that will be given to the send_frame() method called internally to send the crafted frame. OTHER METHODSIn addition to the STOMP methods, the following ones are also available:
CALLBACKSSince STOMP is asynchronous (for instance, "MESSAGE" frames could be sent by the server at any time), Net::STOMP::Client uses callbacks to handle frames. There are in fact two levels of callbacks.First, there are per-command callbacks that will be called each time a frame is handled (via the internal dispatch_frame() method). Net::STOMP::Client implements default callbacks that should be sufficient for all frames except "MESSAGE" frames, which should really be handled by the coder. These callbacks should return undef on error, something else on success. Here is an example with a callback counting the messages received: $stomp->message_callback(sub { my($self, $frame) = @_; $MessageCount++; return($self); }); Here are the methods that can be used to get or set these per-command callbacks:
These callbacks are somehow global and it is good practice not to change them during a session. If you do not need a global message callback, you can supply the dummy: $stomp->message_callback(sub { return(1) }); Then, the wait_for_frames() method takes an optional callback argument holding some code to be called for each received frame, after the per-command callback has been called. This can be seen as a local callback, only valid for the call to wait_for_frames(). This callback must return undef on error, false if more frames are expected or true if wait_for_frames() can now stop waiting for new frames and return. Here are all the options that can be given to wait_for_frames():
The return value of wait_for_frames() can be: false if no suitable frame has been received, the received frame if there is no user callback or the user callback return value otherwise. TRANSACTIONSHere is an example using transactions:# create a unique transaction id $tid = $stomp->uuid(); # begin the transaction $stomp->begin(transaction => $tid); # send two messages as part of this transaction $stomp->send( destination => "/queue/test1", body => "message 1", transaction => $tid, ); $stomp->send( destination => "/queue/test2", body => "message 2", transaction => $tid, ); # commit the transaction $stomp->commit(transaction => $tid); LOW-LEVEL APIIt should be enough to use the high-level API and use, for instance, the send() method to create a "MESSAGE" frame and send it in one go.If you need lower level interaction, you can manipulate frames with the Net::STOMP::Client::Frame module. You can also use:
In these methods, the "timeout" option can either be "undef" (meaning block until it's done) or 0 (meaning do not block at all) or a positive number (meaning block at most this number of seconds). COMPATIBILITYThis module has been successfully tested against ActiveMQ, Apollo, HornetQ and RabbitMQ brokers.See Net::STOMP::Client::Version for the list of supported STOMP protocol versions. SEE ALSOMessaging::Message, Net::STOMP::Client::Auth, Net::STOMP::Client::Connection, Net::STOMP::Client::Frame, Net::STOMP::Client::HeartBeat, Net::STOMP::Client::Peer, Net::STOMP::Client::Receipt, Net::STOMP::Client::Tutorial, Net::STOMP::Client::Version, No::Worries::Log.AUTHORLionel Cons <http://cern.ch/lionel.cons>Copyright (C) CERN 2010-2017
Visit the GSP FreeBSD Man Page Interface. |