Net::Stomp::MooseHelpers::CanConnect - role for classes that connect via Net::Stomp
version 2.1
package MyThing; use Moose; with 'Net::Stomp::MooseHelpers::CanConnect'; use Try::Tiny; sub foo { my ($self) = @_; SERVER_LOOP: while (1) { my $exception; try { $self->connect(); # do something } catch { $exception = $_; }; if ($exception) { if (blessed $exception && $exception->isa('Net::Stomp::MooseHelpers::Exceptions::Stomp')) { warn "connection died, trying next server\n"; $self->clear_connection; next SERVER_LOOP; } die "unhandled exception $exception"; } } }
This role provides your class with a flexible way to connect to a STOMP server. It supports connecting to one of many server in a round-robin fashion.
connection
The connection to the STOMP server. It's built using the "connection_builder" (passing hostname and port), rotating servers via "next_server". It's usually a Net::Stomp object.
hostname
port
is_connected
True if a call to /connect succeded. Net::Stomp::MooseHelpers::ReconnectOnFailure resets this when reconnecting; you should not care much about it.
/connect
connection_builder
Coderef that, given a hashref of options, returns a connection. The default builder just passes the hashref to the constructor of Net::Stomp.
servers
A ServerConfigList, that is, an arrayref of hashrefs, each of which describes how to connect to a single server. Defaults to [ { hostname => 'localhost', port => 61613 } ].
[ { hostname => 'localhost', port => 61613 } ]
connect_headers
Global setting for connection headers (passed to "connect" in Net::Stomp). Can be overridden by the connect_headers slot in each element of "servers". Defaults to the empty hashref.
next_server
Rotates "servers", returning the element that was just moved from the front to the back.
current_server
Returns whatever the last call to "next_server" returned, i.e. the last element of "servers".
connect
Call the connect method on "connection", passing the generic "connect_headers" and the per-server connect headers (from "current_server", slot connect_headers). Throws a Net::Stomp::MooseHelpers::Exceptions::Stomp if anything goes wrong.
If the "connection" attribute is set, and "is_connected", returns without doing anything.
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::MooseHelpers, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Net::Stomp::MooseHelpers
CPAN shell
perl -MCPAN -e shell install Net::Stomp::MooseHelpers
For more information on module installation, please visit the detailed CPAN module installation guide.