forked from rabbitmq/rabbitmq-tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpc_client.pl
70 lines (59 loc) · 1.58 KB
/
rpc_client.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/perl
use strict;
use warnings;
$|++;
use AnyEvent;
use Net::RabbitFoot;
use UUID::Tiny;
sub fibonacci_rpc($) {
my $n = shift;
my $cv = AnyEvent->condvar;
my $corr_id = UUID::Tiny::create_UUID_as_string(UUID::Tiny::UUID_V4);
my $conn = Net::RabbitFoot->new()->load_xml_spec()->connect(
host => 'localhost',
port => 5672,
user => 'guest',
pass => 'guest',
vhost => '/',
);
my $channel = $conn->open_channel();
my $result = $channel->declare_queue(exclusive => 1);
my $callback_queue = $result->{method_frame}->{queue};
sub on_response_cb {
my %a = (
condvar => undef,
correlation_id => undef,
@_
);
return sub {
my $var = shift;
my $body = $var->{body}->{payload};
if ($a{correlation_id} eq $var->{header}->{correlation_id}) {
$a{condvar}->send($body);
}
};
}
$channel->consume(
no_ack => 1,
on_consume => on_response_cb(
condvar => $cv,
correlation_id => $corr_id,
),
);
$channel->publish(
exchange => '',
routing_key => 'rpc_queue',
header => {
reply_to => $callback_queue,
correlation_id => $corr_id,
},
body => $n,
);
return $cv->recv;
}
print " [x] Requesting fib(30)\n";
my $response = fibonacci_rpc(30);
print " [.] Got $response\n";
print " [x] Requesting fib(32)\n";
$response = fibonacci_rpc(32);
print " [.] Got $response\n";