-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
Copy pathreceive_logs_topic.pl
55 lines (41 loc) · 1.04 KB
/
receive_logs_topic.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
#!/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 => 'topic_logs',
type => 'topic',
);
my $result = $channel->declare_queue(exclusive => 1);
my $queue_name = $result->{method_frame}->{queue};
my @binding_keys = @ARGV or die "Usage: $0 [binding_key]...\n";
for my $key (@binding_keys) {
$channel->bind_queue(
exchange => 'topic_logs',
queue => $queue_name,
routing_key => $key,
);
}
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;