blib/lib/Net/AMQP/RabbitMQ.pm

filter: Statementstmt 100% 24 / 24 Branchbran 100% 4 / 4 Conditioncond 75.0% 3 / 4 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 5 / 5 Podpod 100% 1 / 1 totaltotal 95.0% 38 / 40 CC 5 SCAR 11.7
CC
5
Cov
97%
publish
5.0
BEGIN
1.0
BEGIN
1.0
CRAP 3.2
↑ Summary amqp_consumer.c »
1 package Net::AMQP::RabbitMQ;
2 29 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 29 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 29 our $VERSION = '2.000000';
6  
7 29 ▶use XSLoader;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 29 XSLoader::load "Net::AMQP::RabbitMQ", $VERSION;
9 29 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 =encoding UTF-8
12  
13 =head1 NAME
14  
15 Net::AMQP::RabbitMQ - interact with RabbitMQ over AMQP using librabbitmq
16  
17 =head1 SYNOPSIS
18  
19 use Net::AMQP::RabbitMQ;
20 my $mq = Net::AMQP::RabbitMQ->new();
21 $mq->connect("localhost", { user => "guest", password => "guest" });
22 $mq->channel_open(1);
23 $mq->publish(1, "queuename", "Hi there!");
24 my $gotten = $mq->get(1, "queuename");
25 print $gotten->{body} . "\n";
26 $mq->disconnect();
27  
28 =head1 VERSION COMPATIBILITY
29  
30 This module was forked from L<Net::RabbitMQ> version 0.2.6 which uses an older
31 version of librabbitmq, and doesn't work correctly with newer versions of RabbitMQ.
32 The main change between this module and the original is this library uses
33 a newer, unforked, version of librabbitmq. Version 0.7.1 to be precise.
34  
35 This means this module only works with the AMQP 0.9.1 protocol, so requires RabbitMQ
36 version 2+. Also, since the version of librabbitmq used is not a custom fork, it
37 means this module doesn't support the basic_return callback method.
38  
39 =head1 DESCRIPTION
40  
41 C<Net::AMQP::RabbitMQ> is a fork of C<Net::RabbitMQ> that uses a newer version of librabbitmq
42 and fixes some bugs. It provides a simple wrapper around the librabbitmq library
43 that allows connecting, declaring exchanges and queues, binding and unbinding
44 queues, publishing, consuming and receiving events.
45  
46 Error handling in this module is primarily achieve by C<Perl_croak> (die). You
47 should be making good use of eval around these methods to ensure that you
48 appropriately catch the errors.
49  
50 =head1 METHODS
51  
52 All methods, unless specifically stated, return nothing on success
53 and die on failure.
54  
55 Failure to be connected is a fatal failure for most methods.
56  
57 =head2 new()
58  
59 Creates a new Net::AMQP::RabbitMQ object.
60  
61 =head2 connect( $hostname, $options )
62  
63 C<$hostname> is the host to which a connection will be attempted.
64  
65 C<$options> is an optional hash respecting the following keys:
66  
67 {
68 user => $user, #default 'guest'
69 password => $password, #default 'guest'
70 port => $port, #default 5672
71 vhost => $vhost, #default '/'
72 channel_max => $cmax, #default 0
73 frame_max => $fmax, #default 131072
74 heartbeat => $hearbeat, #default 0
75 timeout => $seconds #default undef (no timeout)
76  
77 ssl => 1 | 0 #default 0
78 ssl_verify_host => 1 | 0 #default 1
79 ssl_cacert => $caert_path #needed for ssl
80 ssl_init => 1 | 0 #default 1, initilise the openssl library
81 }
82  
83 You probably don't want to touch C<ssl_init>, unless you know what it does.
84  
85 For now there is no option to disable ssl peer checking, meaning to use C<ssl>, C<ssl_cacert> is required.
86  
87 B<SSL NOTE>
88  
89 if the connection is cut when using ssl, openssl will throw a C<SIGPIPE>, you should catch this or perl
90 will exit with error code 141
91  
92 $SIG{PIPE} = 'IGNORE';
93  
94 =head2 disconnect()
95  
96 Causes the connection to RabbitMQ to be torn down.
97  
98 =head2 get_server_properties()
99  
100 Get a hashref of server properties (these may vary, you should C<Data::Dumper> to inspect). They will be provided by the RabbitMQ server to which you are connected.
101  
102 =head2 get_client_properties()
103  
104 Get a hashref of server properties (these may vary, you should C<Data::Dumper> to inspect).
105  
106 =head2 is_connected()
107  
108 Returns true if a valid socket connection appears to exist, false otherwise.
109  
110 =head2 channel_open($channel)
111  
112 C<$channel> is a positive integer describing the channel you which to open.
113  
114 =head2 channel_close($channel)
115  
116 C<$channel> is a positive integer describing the channel you which to close.
117  
118 =head2 get_channel_max()
119  
120 Returns the maximum allowed channel number.
121  
122 =head2 exchange_declare($channel, $exchange, $options, $arguments)
123  
124 C<$channel> is a channel that has been opened with C<channel_open>.
125  
126 C<$exchange> is the name of the exchange to be instantiated.
127  
128 C<$options> is an optional hash respecting the following keys:
129  
130 {
131 exchange_type => $type, #default 'direct'
132 passive => $boolean, #default 0
133 durable => $boolean, #default 0
134 auto_delete => $boolean, #default 0
135 }
136  
137 Note that the default for the C<auto_delete> option is different for C<exchange_declare> and for C<queue_declare>.
138  
139 C<$arguments> is an optional hash of additional arguments to the RabbitMQ server, such as:
140  
141 {
142 # exchange to try if no routes apply on this exchange
143 alternate_exchange => 'alternate_exchange_name',
144 }
145  
146 =head2 exchange_delete($channel, $exchange, $options)
147  
148 C<$channel> is a channel that has been opened with C<channel_open>.
149  
150 C<$exchange> is the name of the exchange to be deleted.
151  
152 C<$options> is an optional hash respecting the following keys:
153  
154 {
155 if_unused => $boolean, #default 1
156 }
157  
158 =head2 exchange_bind($channel, $destination, $source, $routing_key, $arguments)
159  
160 C<$channel> is a channel that has been opened with C<channel_open>.
161  
162 C<$destination> is a previously declared exchange, C<$source> is
163 yet another previously declared exchange, and C<$routing_key> is
164 the routing key that will bind the specified source exchange to
165 the specified destination exchange.
166  
167 C<$arguments> is an optional hash which will be passed to the server. When
168 binding to an exchange of type C<headers>, this can be used to only receive
169 messages with the supplied header values.
170  
171 =head2 exchange_unbind($channel, $destination, $source, $routing_key, $arguments)
172  
173 C<$channel> is a channel that has been opened with C<channel_open>.
174  
175 C<$destination> is a previously declared exchange, C<$source> is
176 yet another previously declared exchange, and C<$routing_key> is
177 the routing key that will B<unbind> the specified source exchange from
178 the specified destination exchange.
179  
180 C<$arguments> is an optional hash which will be passed to the server. When
181 binding to an exchange of type C<headers>, this can be used to only receive
182 messages with the supplied header values.
183  
184 =head2 queue_declare($channel, $queuename, $options, $arguments)
185  
186 C<$channel> is a channel that has been opened with C<channel_open>.
187  
188 C<$queuename> is the name of the queuename to be instantiated. If
189 C<$queuename> is undef or an empty string, then an auto generated
190 queuename will be used.
191  
192 C<$options> is an optional hash respecting the following keys:
193  
194 {
195 passive => $boolean, #default 0
196 durable => $boolean, #default 0
197 exclusive => $boolean, #default 0
198 auto_delete => $boolean, #default 1
199 }
200  
201 Note that the default for the C<auto_delete> option is different for C<exchange_declare> and for C<queue_declare>.
202  
203 C<$arguments> is an optional hash which will be passed to the server
204 when the queue is created. This can be used for creating mirrored
205 queues by using the x-ha-policy header.
206  
207 In scalar context, this method returns the queuename declared
208 (important for retrieving the autogenerated queuename in the
209 event that one was requested).
210  
211 In array context, this method returns three items: queuename,
212 the number of message waiting on the queue, and the number
213 of consumers bound to the queue.
214  
215 =head2 queue_bind($channel, $queuename, $exchange, $routing_key, $arguments)
216  
217 C<$channel> is a channel that has been opened with C<channel_open>.
218  
219 C<$queuename> is a previously declared queue, C<$exchange> is a
220 previously declared exchange, and C<$routing_key> is the routing
221 key that will bind the specified queue to the specified exchange.
222  
223 C<$arguments> is an optional hash which will be passed to the server. When
224 binding to an exchange of type C<headers>, this can be used to only receive
225 messages with the supplied header values.
226  
227 =head2 queue_unbind($channel, $queuename, $exchange, $routing_key, $arguments)
228  
229 This is like the C<queue_bind> with respect to arguments. This command unbinds
230 the queue from the exchange. The C<$routing_key> and C<$arguments> must match
231 the values supplied when the binding was created.
232  
233 If this fails, you must reopen the channel
234  
235 =head2 queue_delete($channel, $queuename, $options)
236  
237 Deletes the queue
238  
239 C<$options> is an optional hash respecting the following keys:
240  
241 {
242 if_unused => $boolean, #default 1
243 if_empty => $boolean, #default 1
244 }
245  
246 If this fails, you must reopen the channel
247  
248 =head2 publish($channel, $routing_key, $body, $options, $props)
249  
250 C<$channel> is a channel that has been opened with C<channel_open>.
251  
252 C<$routing_key> is the name of the routing key for this message.
253  
254 C<$body> is the payload to enqueue.
255  
256 C<$options> is an optional hash respecting the following keys:
257  
258 {
259 exchange => $exchange, #default 'amq.direct'
260 mandatory => $boolean, #default 0
261 immediate => $boolean, #default 0
262 force_utf8_in_header_strings => $boolean, #default 0
263 }
264  
265 The C<force_utf8_in_header_strings> option causes all headers which look like
266 strings to be treated as UTF-8. In an attempt to make this a non-breaking change,
267 this option is disabled by default. However, for all headers beginning with
268 C<x->, those are treated as UTF-8 regardless of this option (per spec).
269  
270 C<$props> is an optional hash (the AMQP 'props') respecting the following keys:
271  
272 {
273 content_type => $string,
274 content_encoding => $string,
275 correlation_id => $string,
276 reply_to => $string,
277 expiration => $string,
278 message_id => $string,
279 type => $string,
280 user_id => $string,
281 app_id => $string,
282 delivery_mode => $integer,
283 priority => $integer,
284 timestamp => $integer,
285 headers => $headers # This should be a hashref of keys and values.
286 }
287  
288 =head2 consume($channel, $queuename, $options)
289  
290 C<$channel> is a channel that has been opened with C<channel_open>.
291  
292 C<$queuename> is the name of the queue from which we'd like to consume.
293  
294 C<$options> is an optional hash respecting the following keys:
295  
296 {
297 consumer_tag => $tag, #absent by default
298 no_local => $boolean, #default 0
299 no_ack => $boolean, #default 1
300 exclusive => $boolean, #default 0
301 }
302  
303  
304 The consumer_tag is returned. This command does B<not> return AMQP
305 frames, it simply notifies RabbitMQ that messages for this queue should
306 be delivered down the specified channel.
307  
308 =head2 recv($timeout)
309  
310 C<$timeout> is a positive integer, specifying the number of milliseconds to
311 wait for a message. If you do not provide a timeout (or set it to 0), then
312 this call will block until it receives a message. If you set it to -1 it will
313 return immediately (waiting 0 ms).
314  
315 This command receives and reconstructs AMQP frames and returns a hash
316 containing the following information:
317  
318 {
319 body => 'Magic Transient Payload', # the reconstructed body
320 routing_key => 'nr_test_q', # route the message took
321 exchange => 'nr_test_x', # exchange used
322 delivery_tag => 1, # (used for acks)
323 redelivered => $boolean # if message is redelivered
324 consumer_tag => 'c_tag', # tag from consume()
325 props => $props, # hashref sent in
326 }
327  
328 C<$props> is the hash sent by publish() respecting the following keys:
329  
330 {
331 content_type => $string,
332 content_encoding => $string,
333 correlation_id => $string,
334 reply_to => $string,
335 expiration => $string,
336 message_id => $string,
337 type => $string,
338 user_id => $string,
339 app_id => $string,
340 delivery_mode => $integer,
341 priority => $integer,
342 timestamp => $integer,
343 }
344  
345 If you provide a timeout, then the C<recv()> method returns C<undef> if the
346 timeout expires before a message is received from the server.
347  
348 =head2 cancel($channel, $consumer_tag)
349  
350 C<$channel> is a channel that has been opened with C<channel_open>.
351  
352 C<$consumer_tag> is a tag previously passed to C<consume()> or one that was
353 generated automatically as a result of calling C<consume()> without an
354 explicit tag.
355  
356 This method returns true or false indicating whether we got the expected
357 "cancel-ok" response from the server.
358  
359 =head2 get($channel, $queuename, $options)
360  
361 This command runs an amqp_basic_get which returns undef immediately
362 if no messages are available on the queue and returns a hash as follows
363 if a message is available:
364  
365 {
366 body => 'Magic Transient Payload', # the reconstructed body
367 routing_key => 'nr_test_q', # route the message took
368 exchange => 'nr_test_x', # exchange used
369 content_type => 'foo', # (only if specified)
370 delivery_tag => 1, # (used for acks)
371 redelivered => 0, # if message is redelivered
372 message_count => 0, # message count
373  
374 # Not all of these will be present. Consult the RabbitMQ reference for more details.
375 props => {
376 content_type => 'text/plain',
377 content_encoding => 'none',
378 correlation_id => '123',
379 reply_to => 'somequeue',
380 expiration => 1000,
381 message_id => 'ABC',
382 type => 'notmytype',
383 user_id => 'guest',
384 app_id => 'idd',
385 delivery_mode => 1,
386 priority => 2,
387 timestamp => 1271857990,
388 headers => {
389 unsigned_integer => 12345,
390 signed_integer => -12345,
391 double => 3.141,
392 string => "string here",
393  
394 # The x-death header is a special header for dead-lettered messages (rejected or timed out).
395 'x-death' => [
396 {
397 time => 1271857954,
398 exchange => $exchange,
399 queue => $exchange,
400 reason => 'expired',
401 'routing-keys' => [q{}],
402 },
403 ],
404 },
405 },
406 }
407  
408 C<$channel> is a channel that has been opened with C<channel_open>.
409  
410 C<$queuename> is the name of the queue from which we'd like to consume.
411  
412 C<$options> is an optional hash respecting the following keys:
413  
414 {
415 no_ack => $boolean, #default 1
416 }
417  
418 =head2 ack($channel, $delivery_tag, $multiple = 0)
419  
420 C<$channel> is a channel that has been opened with C<channel_open>.
421  
422 C<$delivery_tag> the delivery tag seen from a returned frame from the
423 C<recv> method.
424  
425 C<$multiple> specifies if multiple are to be acknowledged at once.
426  
427 =head2 purge($channel, $queuename)
428  
429 C<$channel> is a channel that has been opened with C<channel_open>.
430  
431 C<$queuename> is the queue to be purged.
432  
433 =head2 reject($channel, $delivery_tag, $requeue = 0)
434  
435 C<$channel> is a channel that has been opened with C<channel_open>.
436  
437 C<$delivery_tag> the delivery tag seen from a returned frame from the
438 C<recv> method.
439  
440 C<$requeue> specifies if the message should be requeued.
441  
442 =head2 tx_select($channel)
443  
444 C<$channel> is a channel that has been opened with C<channel_open>.
445  
446 Start a server-side (tx) transaction over $channel.
447  
448 =head2 tx_commit($channel)
449  
450 C<$channel> is a channel that has been opened with C<channel_open>.
451  
452 Commit a server-side (tx) transaction over $channel.
453  
454 =head2 tx_rollback($channel)
455  
456 C<$channel> is a channel that has been opened with C<channel_open>.
457  
458 Rollback a server-side (tx) transaction over $channel.
459  
460 =head2 basic_qos($channel, $options)
461  
462 C<$channel> is a channel that has been opened with C<channel_open>.
463  
464 C<$options> is an optional hash respecting the following keys:
465  
466 {
467 prefetch_count => $cnt, #default 0
468 prefetch_size => $size, #default 0
469 global => $bool, #default 0
470 }
471  
472 Set quality of service flags on the current $channel.
473  
474 =head2 heartbeat()
475  
476 Send a heartbeat frame. If you've connected with a heartbeat parameter,
477 you must send a heartbeat periodically matching connection parameter or
478 the server may snip the connection.
479  
480 =head1 WARNING AND ERROR MESSAGES
481  
482 =head2 Fatal Errors
483  
484 It should be noted that almost all errors in this library are considered fatal,
485 insomuch as they trigger a C<croak()>. In these errors, if it appears that somehow
486 the connection has been closed by the remote host, or otherwise invalidated,
487 the socket will also be closed and should be re-opened before any additional
488 calls are made.
489  
490 =head1 RUNNING THE TEST SUITE
491  
492 The test suite runs live tests against a RabbitMQ server at
493 C<rabbitmq.thisaintnews.com>.
494  
495 There are separte variables for the ssl and none ssl host/user/password/port.
496  
497 If you are in an environment that won't let you connect to this
498 host (or the test server is down), you can use these environment variables:
499  
500 =over 4
501  
502 =item MQHOST
503  
504 Hostname or IP address of the RabbitMQ server to connect to (defaults
505 to C<rabbitmq.thisaintnews.com>).
506  
507 =item MQUSERNAME
508  
509 Username for authentication (defaults to "nartest").
510  
511 =item MQPASSWORD
512  
513 Password for authentication (defaults to "reallysecure").
514  
515 =item MQPORT
516  
517 Port of the RabbitMQ server to connect to (defaults to 5672)
518  
519 =item MQSSL
520  
521 Whether the tests should run with SSL enabled (defaults to false, but
522 see also C<MQSKIPSSL>).
523  
524 =item MQSKIPSSL
525  
526 Whether the SSL tests should be skipped entirely. This option exists
527 because the SSL tests used to ignore C<MQSSL>, and to maintain
528 backwards compatibility, still do.
529  
530 =item MQSSLHOST
531  
532 Hostname or IP address of the RabbitMQ server to connect to (defaults
533 to C<rabbitmq.thisaintnews.com>).
534  
535 =item MQSSLUSERNAME
536  
537 Username for authentication (defaults to "nartest").
538  
539 =item MQSSLPASSWORD
540  
541 Password for authentication (defaults to "reallysecure").
542  
543 =item MQSSLPORT
544  
545 Port of the RabbitMQ server to connect to (defaults to 5673)
546  
547 =item MQSSLCACERT
548  
549 Path to the certificate file for SSL-enabled connections, defaults to
550 F<t/ssl/cacert.pem>.
551  
552 =item MQSSLVERIFYHOST
553  
554 Whether SSL hostname verification should be enabled (defaults to
555 true).
556  
557 =item MQSSLINIT
558  
559 Whether the openssl library should be initialized (defaults to true).
560  
561 =back
562  
563 =head1 AUTHORS
564  
565 Theo Schlossnagle E<lt>jesus@omniti.comE<gt>
566  
567 Mark Ellis E<lt>markellis@cpan.orgE<gt>
568  
569 Michael Stemle, Jr. E<lt>themanchicken@gmail.comE<gt>
570  
571 Dave Rolsky E<lt>autarch@urth.orgE<gt>
572  
573 Slaven Rezić
574  
575 Armand Leclercq
576  
577 Daniel W Burke
578  
579 Dávid Kovács
580  
581 Alexey Sheynuk
582  
583 Karen Etheridge E<lt>ether@cpan.orgE<gt>
584  
585 Eric Brine E<lt>ikegami@cpan.orgE<gt>
586  
587 =head1 LICENSE
588  
589 This software is licensed under the Mozilla Public License. See the LICENSE file in the top distribution directory for the full license text.
590  
591 librabbitmq is licensed under the MIT License. See the LICENSE-MIT file in the top distribution directory for the full license text.
592  
593 =cut
594  
595 sub publish {
596 29 ▶ my ($self, $channel, $routing_key, $body, $options, $props) = @_;
Subroutine publish: called CC=5 SCAR=16.1
Pod documented
597  
598 29 ▶ $options ||= {};
Condition50%1/2
$options ||= {}
l!l
290
MC/DC0%0/1
$options ||= {}
$options
Truth table50%1/2
A
A= $options
Aresult
11
00
599 29 ▶ $props ||= {};
Condition100%2/2
$props ||= {}
l!l
1712
MC/DC100%1/1
$props ||= {}
$props
Truth table100%2/2
A
A= $props
Aresult
11
00
600  
601 # Do a shallow clone to avoid modifying variable passed by caller
602 29 $props = { %$props };
603  
604 # Convert blessed variables in headers to strings
605 29 ▶ if( $props->{headers} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($props->{"headers"}) 9 20 29
606 9 ▶ $props->{headers} = { map { blessed($_) ? "$_" : $_ } %{ $props->{headers} } };
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($_) ? : 3 59 62
607 }
608  
609 29 $self->_publish($channel, $routing_key, $body, $options, $props);
610 }
611  
612 29 1;
↑ Summary amqp_consumer.c »