Skip to content

Commit fc06c86

Browse files
committed
Improve perl tutorial files.
Fixed a few spelling errors. Removed unnecessary timeout argument from some connections. Removed on_return callback from send.pl. Modified rpc_client to allow parameter passing. Added autoflush, $|++, to allow for automated testing.
1 parent a52e816 commit fc06c86

13 files changed

+60
-55
lines changed

perl/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ To successfully use the examples you will need a running RabbitMQ server.
77

88
## Requirements
99

10-
To run this code you need to intall Net::RabbitFoot.
10+
To run this code you need to install Net::RabbitFoot.
1111

1212
cpan -i Net::RabbitFoot
1313

perl/emit_log.pl

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
use strict;
44
use warnings;
55

6+
$|++;
67
use Net::RabbitFoot;
78

8-
use Data::Dumper;
9-
109
my $conn = Net::RabbitFoot->new()->load_xml_spec()->connect(
1110
host => 'localhost',
1211
port => 5672,

perl/emit_log_direct.pl

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use strict;
44
use warnings;
55

6+
$|++;
67
use Net::RabbitFoot;
78

89
my $conn = Net::RabbitFoot->new()->load_xml_spec()->connect(

perl/emit_log_topic.pl

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
use strict;
44
use warnings;
55

6-
use Net::RabbitFoot;
6+
$|++;
77
use AnyEvent;
8+
use Net::RabbitFoot;
89

910
my $conn = Net::RabbitFoot->new()->load_xml_spec()->connect(
1011
host => 'localhost',

perl/new_task.pl

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use strict;
44
use warnings;
55

6+
$|++;
67
use Net::RabbitFoot;
78

89
my $conn = Net::RabbitFoot->new()->load_xml_spec()->connect(
@@ -11,7 +12,6 @@
1112
user => 'guest',
1213
pass => 'guest',
1314
vhost => '/',
14-
timeout => 1,
1515
);
1616

1717

perl/receive.pl

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
use strict;
44
use warnings;
55

6-
use Net::RabbitFoot;
6+
$|++;
77
use AnyEvent;
8+
use Net::RabbitFoot;
89

910
my $conn = Net::RabbitFoot->new()->load_xml_spec()->connect(
1011
host => 'localhost',
@@ -23,7 +24,7 @@
2324
sub callback {
2425
my $var = shift;
2526
my $body = $var->{body}->{payload};
26-
print " [X] Recevied $body\n";
27+
print " [x] Received $body\n";
2728
}
2829

2930
$ch->consume(

perl/receive_logs.pl

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use strict;
44
use warnings;
55

6+
$|++;
67
use AnyEvent;
78
use Net::RabbitFoot;
89

perl/receive_logs_direct.pl

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use strict;
44
use warnings;
55

6+
$|++;
67
use AnyEvent;
78
use Net::RabbitFoot;
89

perl/receive_logs_topic.pl

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
use strict;
44
use warnings;
55

6-
use Net::RabbitFoot;
6+
$|++;
77
use AnyEvent;
8+
use Net::RabbitFoot;
89

910
my $conn = Net::RabbitFoot->new()->load_xml_spec()->connect(
1011
host => 'localhost',

perl/rpc_client.pl

+43-40
Original file line numberDiff line numberDiff line change
@@ -3,52 +3,55 @@
33
use strict;
44
use warnings;
55

6-
use Net::RabbitFoot;
6+
$|++;
77
use AnyEvent;
8+
use Net::RabbitFoot;
89
use UUID::Tiny;
910

10-
use Data::Dumper;
11-
12-
my $cv = AnyEvent->condvar;
13-
my $corr_id = UUID::Tiny::create_UUID_as_string(UUID::Tiny::UUID_V4);
14-
15-
my $conn = Net::RabbitFoot->new()->load_xml_spec()->connect(
16-
host => 'localhost',
17-
port => 5672,
18-
user => 'guest',
19-
pass => 'guest',
20-
vhost => '/',
21-
);
22-
23-
my $channel = $conn->open_channel();
24-
25-
my $result = $channel->declare_queue(exclusive => 1);
26-
my $callback_queue = $result->{method_frame}->{queue};
27-
28-
sub on_response {
29-
my $var = shift;
30-
my $body = $var->{body}->{payload};
31-
if ($corr_id eq $var->{header}->{correlation_id}) {
32-
$cv->send($body);
11+
sub fibonacci_rpc($) {
12+
my $n = shift;
13+
my $cv = AnyEvent->condvar;
14+
my $corr_id = UUID::Tiny::create_UUID_as_string(UUID::Tiny::UUID_V4);
15+
16+
my $conn = Net::RabbitFoot->new()->load_xml_spec()->connect(
17+
host => 'localhost',
18+
port => 5672,
19+
user => 'guest',
20+
pass => 'guest',
21+
vhost => '/',
22+
);
23+
24+
my $channel = $conn->open_channel();
25+
26+
my $result = $channel->declare_queue(exclusive => 1);
27+
my $callback_queue = $result->{method_frame}->{queue};
28+
29+
sub on_response {
30+
my $var = shift;
31+
my $body = $var->{body}->{payload};
32+
if ($corr_id eq $var->{header}->{correlation_id}) {
33+
$cv->send($body);
34+
}
3335
}
34-
}
3536

36-
$channel->consume(
37-
no_ack => 1,
38-
on_consume => \&on_response,
39-
);
40-
41-
$channel->publish(
42-
exchange => '',
43-
routing_key => 'rpc_queue',
44-
header => {
45-
reply_to => $callback_queue,
46-
correlation_id => $corr_id,
47-
},
48-
body => 30,
49-
);
37+
$channel->consume(
38+
no_ack => 1,
39+
on_consume => \&on_response,
40+
);
41+
42+
$channel->publish(
43+
exchange => '',
44+
routing_key => 'rpc_queue',
45+
header => {
46+
reply_to => $callback_queue,
47+
correlation_id => $corr_id,
48+
},
49+
body => $n,
50+
);
51+
return $cv->recv;
52+
}
5053

5154
print " [x] Requesting fib(30)\n";
52-
my $response = $cv->recv;
55+
my $response = fibonacci_rpc(30);
5356
print " [.] Got $response\n";
5457

perl/rpc_server.pl

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
use strict;
44
use warnings;
55

6-
use Net::RabbitFoot;
6+
$|++;
77
use AnyEvent;
8-
9-
use Data::Dumper;
8+
use Net::RabbitFoot;
109

1110
my $conn = Net::RabbitFoot->new()->load_xml_spec()->connect(
1211
host => 'localhost',

perl/send.pl

+1-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use strict;
44
use warnings;
55

6+
$|++;
67
use Net::RabbitFoot;
78

89
my $conn = Net::RabbitFoot->new()->load_xml_spec()->connect(
@@ -11,7 +12,6 @@
1112
user => 'guest',
1213
pass => 'guest',
1314
vhost => '/',
14-
timeout => 1,
1515
);
1616

1717

@@ -21,9 +21,6 @@
2121
exchange => '',
2222
routing_key => 'hello',
2323
body => 'Hello World!',
24-
on_return => sub {
25-
print "hello World\n";
26-
},
2724
);
2825

2926
print " [x] Sent 'Hello World!'\n";

perl/worker.pl

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use strict;
44
use warnings;
55

6+
$|++;
67
use Net::RabbitFoot;
78

89
my $conn = Net::RabbitFoot->new()->load_xml_spec()->connect(

0 commit comments

Comments
 (0)