1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
package Message::Passing::AMQP::ConnectionManager;
use Moo;
use Types::Standard qw( Bool Str Int );
use Scalar::Util qw/ weaken /;
use AnyEvent;
use AnyEvent::RabbitMQ;
use Carp qw/ croak /;
use namespace::autoclean;
with qw/
Message::Passing::Role::ConnectionManager
Message::Passing::Role::HasHostnameAndPort
Message::Passing::Role::HasUsernameAndPassword
/;
sub _default_port { 5672 }
has vhost => (
is => 'ro',
isa => Str,
required => 1,
);
has tls => (
is => 'ro',
isa => Bool,
default => sub { 0 },
);
has verbose => (
is => 'ro',
isa => Bool,
default => sub { 0 },
);
my $has_loaded;
sub _build_connection {
my $self = shift;
weaken($self);
my $client = AnyEvent::RabbitMQ->new(
verbose => $self->verbose,
);
$client->load_xml_spec unless $has_loaded++;
$client->connect(
host => $self->hostname,
port => $self->port,
user => $self->username,
pass => $self->password,
vhost => $self->vhost,
tls => $self->tls,
timeout => $self->timeout,
on_success => sub {
$self->_set_connected(1);
},
on_failure => sub {
my ($error) = @_;
warn("CONNECT ERROR $error");
$self->_set_connected(0);
},
on_close => sub {
warn("CLOSED");
$self->_set_connected(0);
},
);
return $client;
}
1;
=head1 NAME
Message::Passing::AMQP::ConnectionManager - Implements the Message::Passing::Role::HasAConnection interface.
=head1 ATTRIBUTES
=head2 vhost
Passed to L<AnyEvent::RabbitMQ>->new->connect.
=head2 timeout
Passed to L<AnyEvent::RabbitMQ>->new->connect.
=head2 tls
Passed to L<AnyEvent::RabbitMQ>->new->connect.
=head2 verbose
Passed to L<AnyEvent::RabbitMQ>->new.
=head1 AUTHOR, COPYRIGHT AND LICENSE
See L<Message::Passing::AMQP>.
=cut
|