| 1 | package POE::Component::Client::AMQP::Queue; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | =head1 NAME | ||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | POE::Component::Client::AMQP::Queue - AMQP Queue object | ||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | =head1 DESCRIPTION | ||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | Create using the L<POE::Component::Client::AMQP::Channel> objects' C<queue()> command. Calling C<queue()> also retrieves previously created Queue objects. Alternatively, you can use the C<create()> class method below to create it directly, but doing so will not send the Queue.Declare call to the AMQP server. | ||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | =cut | ||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | 1 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | 1 | ▶ | use POE; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | 1 | ▶ | use Params::Validate qw(validate_with); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | 1 | ▶ | use base qw(Class::Accessor); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 19 | 1 | __PACKAGE__->mk_accessors(qw(name channel is_created)); | |||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | 1 | our $VERSION = 0.03; | |||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | =head1 CLASS METHODS | ||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | =head2 create (...) | ||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | =over 4 | ||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | Pass two named args 'name' (optional) and 'channel'. | ||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | =back | ||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | =cut | ||||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | 0 | ▶ | sub create { | ||||||||||||||||||
|
Subroutine
create: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 36 | 0 | my $class = shift; | |||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | 0 | my %self = validate_with( | |||||||||||||||||||
| 39 | params => \@_, | ||||||||||||||||||||
| 40 | spec => { | ||||||||||||||||||||
| 41 | name => 0, | ||||||||||||||||||||
| 42 | channel => 1, | ||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | is_created => { default => 0 }, | ||||||||||||||||||||
| 45 | on_created => { default => [] }, | ||||||||||||||||||||
| 46 | }, | ||||||||||||||||||||
| 47 | allow_extra => 1, | ||||||||||||||||||||
| 48 | ); | ||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | 0 | return bless \%self, $class; | |||||||||||||||||||
| 51 | } | ||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | =head1 OBJECT METHODS | ||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | =head2 name | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | =over 4 | ||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | Returns the queue name | ||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | =back | ||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | =head2 channel | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | =over 4 | ||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | Returns the L<POE::Component::Client::AMQP::Channel> parent object. | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | =back | ||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | =head2 is_created | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | =over 4 | ||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | Returns a boolean, indicating wether the queue has been created on the AMQP server yet or not. | ||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | =back | ||||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | =head2 do_when_created (...) | ||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | =over 4 | ||||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | See L<POE::Component::Client::AMQP::do_when_startup()>; similar behavior. | ||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | =back | ||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | =cut | ||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | 0 | ▶ | sub do_when_created { | ||||||||||||||||||
|
Subroutine
do_when_created: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 90 | 0 | my ($self, $subref) = @_; | |||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | 0 | ▶ | if ($self->{is_created}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 93 | 0 | $subref->(); | |||||||||||||||||||
| 94 | } | ||||||||||||||||||||
| 95 | else { | ||||||||||||||||||||
| 96 | 0 | push @{ $self->{on_created} }, $subref; | |||||||||||||||||||
| 97 | } | ||||||||||||||||||||
| 98 | } | ||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | 0 | ▶ | sub created { | ||||||||||||||||||
|
Subroutine
created: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 101 | 0 | my $self = shift; | |||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | 0 | $self->{is_created} = 1; | |||||||||||||||||||
| 104 | 0 | foreach my $callback (@{ $self->{on_created} }) { | |||||||||||||||||||
| 105 | 0 | $callback->(); | |||||||||||||||||||
| 106 | } | ||||||||||||||||||||
| 107 | } | ||||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | =head2 subscribe ($subref, \%opts) | ||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | =over 4 | ||||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | Sends a L<Net::AMQP::Protocol::Basic::Consume> frame to the server, storing the $subref as a callback function for when content is received. | ||||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | Optionally provide %opts which will override defaults for the Basic.Consume call. | ||||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | The argument signature of the callback is like so: | ||||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | my $do_ack = $subref->($message, $meta) | ||||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | =over 4 | ||||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | =item I<$do_ack> | ||||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | If in the %opts hash you choose 'no_ack => 0', then messages have to be explicitly ack'ed once handled. If your callback returns true in this condition, an ack message will automatically be sent for you. | ||||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | =item I<$message> | ||||||||||||||||||||
| 128 | |||||||||||||||||||||
| 129 | Opaque payload of the content body. | ||||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | =item I<$meta> | ||||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | Hashref with keys as follows: | ||||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | =over 4 | ||||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | =item I<method_frame> | ||||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | L<Net::AMQP::Protocol::Base> delivering method object. | ||||||||||||||||||||
| 140 | |||||||||||||||||||||
| 141 | =item I<header_frame> | ||||||||||||||||||||
| 142 | |||||||||||||||||||||
| 143 | L<Net::AMQP::Protocol::Base> delivering ContentHeader object. | ||||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | =item I<weight>, I<body_size> | ||||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | Copied from the header_frame object. | ||||||||||||||||||||
| 148 | |||||||||||||||||||||
| 149 | =item I<payload> | ||||||||||||||||||||
| 150 | |||||||||||||||||||||
| 151 | Same as the $message argument above. | ||||||||||||||||||||
| 152 | |||||||||||||||||||||
| 153 | =item I<body_frames> | ||||||||||||||||||||
| 154 | |||||||||||||||||||||
| 155 | Array of all the L<Net::AMQP::Frame::Body> frames that comprise the payload. | ||||||||||||||||||||
| 156 | |||||||||||||||||||||
| 157 | =item I<queue> | ||||||||||||||||||||
| 158 | |||||||||||||||||||||
| 159 | The name of this queue object. | ||||||||||||||||||||
| 160 | |||||||||||||||||||||
| 161 | =item I<opts> | ||||||||||||||||||||
| 162 | |||||||||||||||||||||
| 163 | The options used to create the Basic.Consume call (merge of default values and %opts, above) | ||||||||||||||||||||
| 164 | |||||||||||||||||||||
| 165 | =back | ||||||||||||||||||||
| 166 | |||||||||||||||||||||
| 167 | =back | ||||||||||||||||||||
| 168 | |||||||||||||||||||||
| 169 | =back | ||||||||||||||||||||
| 170 | |||||||||||||||||||||
| 171 | =cut | ||||||||||||||||||||
| 172 | |||||||||||||||||||||
| 173 | 0 | ▶ | sub subscribe { | ||||||||||||||||||
|
Subroutine
subscribe: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 174 | 0 | my ($self, $callback, $user_opts) = @_; | |||||||||||||||||||
| 175 | 0 | ▶ | $user_opts ||= {}; | ||||||||||||||||||
|
Condition0%0/2
$user_opts ||= {}
MC/DC0%0/1
$user_opts ||= {}
$user_opts
Truth table0%0/2
A
A= $user_opts
| |||||||||||||||||||||
| 176 | |||||||||||||||||||||
| 177 | 0 | ▶ | $self->do_when_created(sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 178 | my %opts = ( | ||||||||||||||||||||
| 179 | ticket => 0, | ||||||||||||||||||||
| 180 | queue => $self->{name}, | ||||||||||||||||||||
| 181 | #consumer_tag => '', # auto-generated | ||||||||||||||||||||
| 182 | #no_local => 0, | ||||||||||||||||||||
| 183 | 0 | no_ack => 1, | |||||||||||||||||||
| 184 | #exclusive => 0, | ||||||||||||||||||||
| 185 | #nowait => 0, # do not send the ConsumeOk response | ||||||||||||||||||||
| 186 | %$user_opts, | ||||||||||||||||||||
| 187 | ); | ||||||||||||||||||||
| 188 | |||||||||||||||||||||
| 189 | # TODO: if user sets $opts{nowait}, we can't do the synchronous_callback or even know the consumer_tag. | ||||||||||||||||||||
| 190 | |||||||||||||||||||||
| 191 | $poe_kernel->post($self->{channel}{Alias}, server_send => | ||||||||||||||||||||
| 192 | Net::AMQP::Frame::Method->new( | ||||||||||||||||||||
| 193 | 0 | ▶ | synchronous_callback => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 194 | 0 | my $response_frame = shift; | |||||||||||||||||||
| 195 | 0 | my $consumer_tag = $response_frame->method_frame->consumer_tag; | |||||||||||||||||||
| 196 | 0 | ▶ | if (! $consumer_tag) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 197 | 0 | print STDERR "Didn't receive a consumer tag to Basic.Consume request\n"; | |||||||||||||||||||
| 198 | 0 | return; | |||||||||||||||||||
| 199 | } | ||||||||||||||||||||
| 200 | $self->{channel}{consumers}{$consumer_tag} = { | ||||||||||||||||||||
| 201 | queue => $self->{name}, | ||||||||||||||||||||
| 202 | 0 | callback => $callback, | |||||||||||||||||||
| 203 | opts => \%opts, | ||||||||||||||||||||
| 204 | }; | ||||||||||||||||||||
| 205 | }, | ||||||||||||||||||||
| 206 | 0 | method_frame => Net::AMQP::Protocol::Basic::Consume->new(%opts) | |||||||||||||||||||
| 207 | ), | ||||||||||||||||||||
| 208 | ); | ||||||||||||||||||||
| 209 | 0 | }); | |||||||||||||||||||
| 210 | |||||||||||||||||||||
| 211 | 0 | return $self; | |||||||||||||||||||
| 212 | } | ||||||||||||||||||||
| 213 | |||||||||||||||||||||
| 214 | =head2 publish ($message, \%opts) | ||||||||||||||||||||
| 215 | |||||||||||||||||||||
| 216 | =over 4 | ||||||||||||||||||||
| 217 | |||||||||||||||||||||
| 218 | Sends a message to the queue. In other words, sends a L<Net::AMQP::Protocol::Basic::Publish> followed by a L<Net::AMQP::Protocol::Basic::ContentHeader> and L<Net::AMQP::Frame::Body> containing the body of the message. | ||||||||||||||||||||
| 219 | |||||||||||||||||||||
| 220 | Optionally pass %opts, which can override any option in the L<Net::AMQP::Protocol::Basic::Publish> ('ticket', 'exchange', 'routing_key', 'mandatory', 'immediate'), L<Net::AMQP::Frame::Header> ('weight') or L<Net::AMQP::Protocol::Basic::ContentHeader> ('content_type', 'content_encoding', 'headers', 'delivery_mode', 'priority', 'correlation_id', 'reply_to', 'expiration', 'message_id', 'timestamp', 'type', 'user_id', 'app_id', 'cluster_id') objects. See the related documentation for an explaination of each. | ||||||||||||||||||||
| 221 | |||||||||||||||||||||
| 222 | =back | ||||||||||||||||||||
| 223 | |||||||||||||||||||||
| 224 | =cut | ||||||||||||||||||||
| 225 | |||||||||||||||||||||
| 226 | 0 | ▶ | sub publish { | ||||||||||||||||||
|
Subroutine
publish: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 227 | 0 | my ($self, $message, $user_opts) = @_; | |||||||||||||||||||
| 228 | 0 | ▶ | $user_opts ||= {}; | ||||||||||||||||||
|
Condition0%0/2
$user_opts ||= {}
MC/DC0%0/1
$user_opts ||= {}
$user_opts
Truth table0%0/2
A
A= $user_opts
| |||||||||||||||||||||
| 229 | |||||||||||||||||||||
| 230 | 0 | ▶ | $self->do_when_created(sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 231 | my %opts = ( | ||||||||||||||||||||
| 232 | routing_key => $self->{name}, # route to self | ||||||||||||||||||||
| 233 | 0 | content_type => 'application/octet-stream', | |||||||||||||||||||
| 234 | %$user_opts, | ||||||||||||||||||||
| 235 | ); | ||||||||||||||||||||
| 236 | |||||||||||||||||||||
| 237 | $poe_kernel->post($self->{channel}{Alias}, server_send => | ||||||||||||||||||||
| 238 | 0 | $self->{channel}{server}->compose_basic_publish($message, %opts) | |||||||||||||||||||
| 239 | ); | ||||||||||||||||||||
| 240 | 0 | }); | |||||||||||||||||||
| 241 | |||||||||||||||||||||
| 242 | 0 | return $self; | |||||||||||||||||||
| 243 | } | ||||||||||||||||||||
| 244 | |||||||||||||||||||||
| 245 | =head2 bind (%opts) | ||||||||||||||||||||
| 246 | |||||||||||||||||||||
| 247 | =over 4 | ||||||||||||||||||||
| 248 | |||||||||||||||||||||
| 249 | Shortcut to send a Queue.Bind call with this queue name. Pass the same args you'd pass to a L<Net::AMQP::Protocol::Queue::Bind> object creation. | ||||||||||||||||||||
| 250 | |||||||||||||||||||||
| 251 | =back | ||||||||||||||||||||
| 252 | |||||||||||||||||||||
| 253 | =cut | ||||||||||||||||||||
| 254 | |||||||||||||||||||||
| 255 | 0 | ▶ | sub bind { | ||||||||||||||||||
|
Subroutine
bind: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 256 | 0 | my ($self, %opts) = @_; | |||||||||||||||||||
| 257 | |||||||||||||||||||||
| 258 | 0 | ▶ | $self->do_when_created(sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 259 | 0 | ▶ | $opts{queue} ||= $self->{name}; | ||||||||||||||||||
|
Condition0%0/3
$opts{"queue"} ||= $self->{"name"}
MC/DC0%0/2
$opts{"queue"} ||= $self->{"name"}
$opts{"queue"}
$self->{"name"}
Truth table0%0/3
A ||= B
A= $opts{"queue"}B= $self->{"name"}
| |||||||||||||||||||||
| 260 | 0 | $poe_kernel->post($self->{channel}{Alias}, server_send => | |||||||||||||||||||
| 261 | Net::AMQP::Protocol::Queue::Bind->new(%opts) | ||||||||||||||||||||
| 262 | ); | ||||||||||||||||||||
| 263 | 0 | }); | |||||||||||||||||||
| 264 | |||||||||||||||||||||
| 265 | 0 | return $self; | |||||||||||||||||||
| 266 | } | ||||||||||||||||||||
| 267 | |||||||||||||||||||||
| 268 | =head1 SEE ALSO | ||||||||||||||||||||
| 269 | |||||||||||||||||||||
| 270 | L<POE::Component::Client::AMQP::Channel> | ||||||||||||||||||||
| 271 | |||||||||||||||||||||
| 272 | =head1 COPYRIGHT | ||||||||||||||||||||
| 273 | |||||||||||||||||||||
| 274 | Copyright (c) 2009 Eric Waters and XMission LLC (http://www.xmission.com/). All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||||||||||||||||||||
| 275 | |||||||||||||||||||||
| 276 | The full text of the license can be found in the LICENSE file included with this module. | ||||||||||||||||||||
| 277 | |||||||||||||||||||||
| 278 | =head1 AUTHOR | ||||||||||||||||||||
| 279 | |||||||||||||||||||||
| 280 | Eric Waters <ewaters@gmail.com> | ||||||||||||||||||||
| 281 | |||||||||||||||||||||
| 282 | =cut | ||||||||||||||||||||
| 283 | |||||||||||||||||||||
| 284 | 1 | 1; | |||||||||||||||||||