NAME
Net::Stomp::MooseHelpers::CanConnect - role for classes that connect via Net::Stomp
VERSION
version 2.3
SYNOPSIS
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";
}
}
}
DESCRIPTION
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.
ATTRIBUTES
connection
The connection to the STOMP server. It's built using the "connection_builder" (passing hostname
, port
, and SSL flag and options), rotating servers via "next_server". It's usually a Net::Stomp object.
is_connected
True if a call to /connect
succeded. Net::Stomp::MooseHelpers::ReconnectOnFailure resets this when reconnecting; you should not care much about it.
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 } ]
.
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.
METHODS
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.
AUTHOR
Gianni Ceccarelli <gianni.ceccarelli@net-a-porter.com>
COPYRIGHT AND LICENSE
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.