forked from rabbitmq/rabbitmq-tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
receive_logs_direct.pl
55 lines (43 loc) · 1.06 KB
/
receive_logs_direct.pl
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
#!/usr/bin/perl
use strict;
use warnings;
$|++;
use AnyEvent;
use Net::RabbitFoot;
my $conn = Net::RabbitFoot->new()->load_xml_spec()->connect(
host => 'localhost',
port => 5672,
user => 'guest',
pass => 'guest',
vhost => '/',
);
my $channel = $conn->open_channel();
$channel->declare_exchange(
exchange => 'direct_logs',
type => 'direct',
);
my $result = $channel->declare_queue(
exclusive => 1,
);
my $queue_name = $result->{method_frame}->{queue};
my @severities = @ARGV or die "Usage: $0 [info] [warning] [error]\n";
foreach my $severity (@severities) {
$channel->bind_queue(
exchange => 'direct_logs',
queue => $queue_name,
routing_key => $severity,
);
}
print " [*] Waiting for logs. To exit press CTRL-C\n";
sub callback {
my $var = shift;
my $body = $var->{body}->{payload};
my $routing_key = $var->{deliver}->{method_frame}->{routing_key};
print " [x] $routing_key: $body\n";
}
$channel->consume(
on_consume => \&callback,
no_ack => 1,
);
# Wait forever
AnyEvent->condvar->recv;