File Coverage

blib/lib/Crixa/Message.pm
Criterion Covered Total %
statement 18 23 78.2
branch 0 2 0.0
condition 0 3 0.0
subroutine 6 8 75.0
pod 1 2 50.0
total 25 38 65.7


line stmt bran cond sub pod time code
1             package Crixa::Message;
2              
3 1     1   4 use strict;
  1         2  
  1         34  
4 1     1   4 use warnings;
  1         1  
  1         22  
5 1     1   4 use namespace::autoclean;
  1         1  
  1         6  
6              
7             our $VERSION = '0.13';
8              
9 1     1   75 use Moose;
  1         2  
  1         6  
10              
11 1     1   5298 use Math::Int64 0.34;
  1         1946  
  1         6  
12 1     1   99 use Moose::Util::TypeConstraints;
  1         17  
  1         11  
13              
14             has channel => (
15             isa => 'Crixa::Channel',
16             is => 'ro',
17             required => 1,
18             );
19              
20             has body => (
21             is => 'ro',
22             isa => 'Str',
23             required => 1,
24             );
25              
26             {
27             my @properties = qw(
28             content_type
29             content_encoding
30             correlation_id
31             reply_to
32             expiration
33             message_id
34             type
35             user_id
36             app_id
37             priority
38             delivery_mode
39             timestamp
40             headers
41             );
42              
43             has _properties => (
44             traits => ['Hash'],
45             is => 'bare',
46             isa => 'HashRef',
47             init_arg => 'props',
48             required => 1,
49             handles => {
50             map { ( $_ => [ 'get', $_ ], 'has_' . $_ => [ 'exists', $_ ], ) }
51             @properties
52             },
53             );
54             }
55              
56             has redelivered => (
57             is => 'ro',
58             isa => 'Bool',
59             required => 1,
60             );
61              
62             has routing_key => (
63             is => 'ro',
64             isa => 'Str',
65             required => 1,
66             );
67              
68             has exchange => (
69             is => 'ro',
70             isa => 'Str',
71             required => 1,
72             );
73              
74             # This allows both standard integers and integer objects (e.g., Math::UInt64)
75             # as long as they stringify correctly.
76             my $non_negative_int = subtype(
77             as 'Defined',
78             where {},
79             inline_as {
80             $_[0]->parent()->_inline_check( $_[1] )
81             . " && $_[1] =~ /^[0-9]+\\z/ ";
82             }
83             );
84              
85             has delivery_tag => (
86             is => 'ro',
87             isa => $non_negative_int,
88             required => 1,
89             );
90              
91             has message_count => (
92             is => 'ro',
93             isa => 'Int',
94             predicate => '_has_message_count',
95             );
96              
97             has consumer_tag => (
98             is => 'ro',
99             isa => 'Str',
100             predicate => '_has_consumer_tag',
101             );
102              
103             sub BUILD {
104 0     0 0   my $self = shift;
105              
106 0 0 0       die 'A Crixa::Message must have a message_count or consumer_tag'
107             unless $self->_has_message_count() || $self->_has_consumer_tag();
108              
109 0           return;
110             }
111              
112             sub ack {
113 0     0 1   my $self = shift;
114 0           $self->channel->ack( $self->delivery_tag );
115             }
116              
117             __PACKAGE__->meta->make_immutable;
118              
119             1;
120              
121             # ABSTRACT: A Crixa Message
122              
123             __END__
124              
125             =pod
126              
127             =head1 NAME
128              
129             Crixa::Message - A Crixa Message
130              
131             =head1 VERSION
132              
133             version 0.13
134              
135             =head1 DESCRIPTION
136              
137             This class represents a single queue. With RabbitMQ, messages are published to
138             exchanges, which then routes the message to one or more queues. You then
139             consume those messages from the queue.
140              
141             =encoding UTF-8
142              
143             =head1 METHODS
144              
145             This class provides the following methods:
146              
147             =head2 $message->ack
148              
149             This send an acknowledgement for the message on the channel that was used to
150             receive the message.
151              
152             =head2 $message->body
153              
154             This returns the message's body. If the message does not have any
155             content-encoding set _or_ the message contains an encoding with the string
156             "utf-8" (case insensitive), then the message is returned as character
157             data. Otherwise it is returned as binary data.
158              
159             =head2 Property methods
160              
161             There are a number of properties that can be associated with a message. This
162             class provides reader and predicate methods for all properties of the form C<<
163             $message->foo >> and C<< $message->has_foo >>. None of the properties are
164             required.
165              
166             The properties supported are:
167              
168             =over 4
169              
170             =item * content_type
171              
172             =item * content_encoding
173              
174             =item * correlation_id
175              
176             =item * reply_to
177              
178             =item * expiration
179              
180             =item * message_id
181              
182             =item * type
183              
184             =item * user_id
185              
186             =item * app_id
187              
188             =item * priority
189              
190             =item * delivery_mode
191              
192             =item * timestamp
193              
194             =item * headers
195              
196             =back
197              
198             See the C<publish> method in the L<Crixa::Exchange> docs for more details.
199              
200             =head2 $message->redelivered
201              
202             A boolean indicating whether or not the message has already been delivered at
203             least once. RabbitMQ guarantees that each message will be delivered I<at least
204             once>, and it is not uncommon for a message to be redelivered before the first
205             consumer to receive it has had a chance to acknowledge it.
206              
207             =head2 $message->message_count
208              
209             The number of messages left in the queue at the time this message was
210             delivered.
211              
212             Note that this is only set for messages which are not received via the C<<
213             Crixa::Queue->consume() >> method.
214              
215             =head2 $message->routing_key
216              
217             The routing path on which the message was received.
218              
219             =head2 $message->exchange
220              
221             The exchange to which this message was published
222              
223             =head2 $message->delivery_tag
224              
225             The delivery tag for a given message. This is used when the C<<
226             $message->ack() >> method is called.
227              
228             =head2 $message->consumer_tag
229              
230             The tag for the consumer associated with the message, if one exists.
231              
232             Note that this is only set for messages which are received via the C<<
233             Crixa::Queue->consume() >> method.
234              
235             =head2 Crixa::Message->new(...)
236              
237             There is no reason to call this method directly. It will be called by a
238             L<Crixa::Queue> object to inflate messages into objects.
239              
240             =head1 AUTHORS
241              
242             =over 4
243              
244             =item *
245              
246             Chris Prather <chris@prather.org>
247              
248             =item *
249              
250             Dave Rolsky <autarch@urth.org>
251              
252             =back
253              
254             =head1 COPYRIGHT AND LICENSE
255              
256             This software is copyright (c) 2012 - 2015 by Chris Prather.
257              
258             This is free software; you can redistribute it and/or modify it under
259             the same terms as the Perl 5 programming language system itself.
260              
261             =cut