File Coverage

lib/AMQP/Publisher.pm
Criterion Covered Total %
statement 4 6 66.6
branch n/a
condition n/a
subroutine 2 2 100.0
pod 1 1 100.0
total 7 9 77.7


line stmt bran cond sub pod time code
1             package AMQP::Publisher;
2             our $VERSION = '0.01';
3              
4 1     1   661 use Mojo::Base 'AMQP';
  1         3  
  1         13  
5 1     1 1 576 use AnyEvent::RabbitMQ;
  0            
  0            
6             use Sys::Hostname;
7              
8             has 'debug' => 1;
9             has 'host' => 'localhost';
10             has 'port' => 5672;
11             has 'user' => 'guest';
12             has 'password' => 'guest';
13             has 'vhost' => '/';
14             has 'timeout' => 1;
15             has 'heartbeat' => 30;
16             has 'exchange' => 'log';
17             has 'type' => 'topic';
18             has 'key' => '#';
19             has 'rabbit';
20             has 'connection';
21             has 'channel';
22             has 'status';
23             has 'on_connect';
24              
25             sub attach {
26             my $self = shift;
27             $self->status(AnyEvent->condvar);
28             $self->rabbit(AnyEvent::RabbitMQ->new);
29             $self->rabbit->load_xml_spec();
30             $self->rabbit->connect(
31             host => $self->host,
32             port => $self->port,
33             user => $self->user,
34             pass => $self->password,
35             vhost => $self->vhost,
36             timeout => $self->timeout,
37             tune => { heartbeat => $self->heartbeat },
38             on_success => sub {
39             say "Connected to amqp://" . $self->host . ":" . $self->port . $self->vhost if $self->debug;
40             $self->connection(shift);
41             $self->connection->open_channel(
42             on_failure => $self->status,
43             on_close => sub {
44             say "Channel closed" if $self->debug;
45             $self->status->send;
46             },
47             on_success => sub {
48             say "Opened channel" if $self->debug;
49             $self->channel(shift);
50             $self->on_connect->($self);
51             },
52             );
53             },
54             on_failure => $self->status,
55             on_read_failure => sub {
56             say "Failed to read" if $self->debug;
57             $self->status->send;
58             },
59             on_return => sub {
60             say "Failed to send" if $self->debug;
61             $self->status->send;
62             },
63             on_close => sub {
64             say "Connection closed" if $self->debug;
65             $self->status->send;
66             }
67             );
68             $self->status->recv;
69             }
70              
71             sub send {
72             my ($self,$message) = @_;
73             $self->channel->send($message);
74             }
75              
76             1;
77              
78             __END__