adamh-amqp 0.6.3.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. data/README +128 -0
  2. data/Rakefile +15 -0
  3. data/amqp.gemspec +83 -0
  4. data/amqp.todo +32 -0
  5. data/doc/EXAMPLE_01_PINGPONG +2 -0
  6. data/doc/EXAMPLE_02_CLOCK +2 -0
  7. data/doc/EXAMPLE_03_STOCKS +2 -0
  8. data/doc/EXAMPLE_04_MULTICLOCK +2 -0
  9. data/doc/EXAMPLE_05_ACK +2 -0
  10. data/doc/EXAMPLE_05_POP +2 -0
  11. data/doc/EXAMPLE_06_HASHTABLE +2 -0
  12. data/examples/amqp/simple.rb +79 -0
  13. data/examples/mq/ack.rb +45 -0
  14. data/examples/mq/clock.rb +56 -0
  15. data/examples/mq/hashtable.rb +52 -0
  16. data/examples/mq/internal.rb +49 -0
  17. data/examples/mq/logger.rb +88 -0
  18. data/examples/mq/multiclock.rb +49 -0
  19. data/examples/mq/pingpong.rb +45 -0
  20. data/examples/mq/pop.rb +43 -0
  21. data/examples/mq/primes-simple.rb +19 -0
  22. data/examples/mq/primes.rb +99 -0
  23. data/examples/mq/stocks.rb +58 -0
  24. data/lib/amqp.rb +115 -0
  25. data/lib/amqp/buffer.rb +395 -0
  26. data/lib/amqp/client.rb +210 -0
  27. data/lib/amqp/frame.rb +124 -0
  28. data/lib/amqp/protocol.rb +212 -0
  29. data/lib/amqp/server.rb +99 -0
  30. data/lib/amqp/spec.rb +832 -0
  31. data/lib/ext/blankslate.rb +7 -0
  32. data/lib/ext/em.rb +51 -0
  33. data/lib/ext/emfork.rb +69 -0
  34. data/lib/mq.rb +823 -0
  35. data/lib/mq/exchange.rb +302 -0
  36. data/lib/mq/header.rb +33 -0
  37. data/lib/mq/logger.rb +89 -0
  38. data/lib/mq/queue.rb +433 -0
  39. data/lib/mq/rpc.rb +100 -0
  40. data/old/README +30 -0
  41. data/old/Rakefile +12 -0
  42. data/old/amqp-0.8.json +606 -0
  43. data/old/amqp_spec.rb +796 -0
  44. data/old/amqpc.rb +695 -0
  45. data/old/codegen.rb +148 -0
  46. data/protocol/amqp-0.8.json +617 -0
  47. data/protocol/amqp-0.8.xml +3908 -0
  48. data/protocol/codegen.rb +173 -0
  49. data/protocol/doc.txt +281 -0
  50. data/research/api.rb +88 -0
  51. data/research/primes-forked.rb +63 -0
  52. data/research/primes-processes.rb +135 -0
  53. data/research/primes-threaded.rb +49 -0
  54. metadata +121 -0
@@ -0,0 +1,302 @@
1
+ class MQ
2
+ # An Exchange acts as an ingress point for all published messages. An
3
+ # exchange may also be described as a router or a matcher. Every
4
+ # published message is received by an exchange which, depending on its
5
+ # type (described below), determines how to deliver the message.
6
+ #
7
+ # It determines the next delivery hop by examining the bindings associated
8
+ # with the exchange.
9
+ #
10
+ # There are three (3) supported Exchange types: direct, fanout and topic.
11
+ #
12
+ # As part of the standard, the server _must_ predeclare the direct exchange
13
+ # 'amq.direct' and the fanout exchange 'amq.fanout' (all exchange names
14
+ # starting with 'amq.' are reserved). Attempts to declare an exchange using
15
+ # 'amq.' as the name will raise an MQ:Error and fail. In practice these
16
+ # default exchanges are never used directly by client code.
17
+ #
18
+ # These predececlared exchanges are used when the client code declares
19
+ # an exchange without a name. In these cases the library will use
20
+ # the default exchange for publishing the messages.
21
+ #
22
+ class Exchange
23
+ include AMQP
24
+
25
+ # Defines, intializes and returns an Exchange to act as an ingress
26
+ # point for all published messages.
27
+ #
28
+ # There are three (3) supported Exchange types: direct, fanout and topic.
29
+ #
30
+ # As part of the standard, the server _must_ predeclare the direct exchange
31
+ # 'amq.direct' and the fanout exchange 'amq.fanout' (all exchange names
32
+ # starting with 'amq.' are reserved). Attempts to declare an exchange using
33
+ # 'amq.' as the name will raise an MQ:Error and fail. In practice these
34
+ # default exchanges are never used directly by client code.
35
+ #
36
+ # == Direct
37
+ # A direct exchange is useful for 1:1 communication between a publisher and
38
+ # subscriber. Messages are routed to the queue with a binding that shares
39
+ # the same name as the exchange. Alternately, the messages are routed to
40
+ # the bound queue that shares the same name as the routing key used for
41
+ # defining the exchange. This exchange type does not honor the :key option
42
+ # when defining a new instance with a name. It _will_ honor the :key option
43
+ # if the exchange name is the empty string. This is because an exchange
44
+ # defined with the empty string uses the default pre-declared exchange
45
+ # called 'amq.direct'. In this case it needs to use :key to do its matching.
46
+ #
47
+ # # exchange is named 'foo'
48
+ # exchange = MQ::Exchange.new(MQ.new, :direct, 'foo')
49
+ #
50
+ # # or, the exchange can use the default name (amq.direct) and perform
51
+ # # routing comparisons using the :key
52
+ # exchange = MQ::Exchange.new(MQ.new, :direct, "", :key => 'foo')
53
+ # exchange.publish('some data') # will be delivered to queue bound to 'foo'
54
+ #
55
+ # queue = MQ::Queue.new(MQ.new, 'foo')
56
+ # # can receive data since the queue name and the exchange key match exactly
57
+ # queue.pop { |data| puts "received data [#{data}]" }
58
+ #
59
+ # == Fanout
60
+ # A fanout exchange is useful for 1:N communication where one publisher
61
+ # feeds multiple subscribers. Like direct exchanges, messages published
62
+ # to a fanout exchange are delivered to queues whose name matches the
63
+ # exchange name (or are bound to that exchange name). Each queue gets
64
+ # its own copy of the message.
65
+ #
66
+ # Like the direct exchange type, this exchange type does not honor the
67
+ # :key option when defining a new instance with a name. It _will_ honor
68
+ # the :key option if the exchange name is the empty string. Fanout exchanges
69
+ # defined with the empty string as the name use the default 'amq.fanout'.
70
+ # In this case it needs to use :key to do its matching.
71
+ #
72
+ # EM.run do
73
+ # clock = MQ::Exchange.new(MQ.new, :fanout, 'clock')
74
+ # EM.add_periodic_timer(1) do
75
+ # puts "\npublishing #{time = Time.now}"
76
+ # clock.publish(Marshal.dump(time))
77
+ # end
78
+ #
79
+ # # one way of defining a queue
80
+ # amq = MQ::Queue.new(MQ.new, 'every second')
81
+ # amq.bind(MQ.fanout('clock')).subscribe do |time|
82
+ # puts "every second received #{Marshal.load(time)}"
83
+ # end
84
+ #
85
+ # # defining a queue using the convenience method
86
+ # # note the string passed to #bind
87
+ # MQ.queue('every 5 seconds').bind('clock').subscribe do |time|
88
+ # time = Marshal.load(time)
89
+ # puts "every 5 seconds received #{time}" if time.strftime('%S').to_i%5 == 0
90
+ # end
91
+ # end
92
+ #
93
+ # == Topic
94
+ # A topic exchange allows for messages to be published to an exchange
95
+ # tagged with a specific routing key. The Exchange uses the routing key
96
+ # to determine which queues to deliver the message. Wildcard matching
97
+ # is allowed. The topic must be declared using dot notation to separate
98
+ # each subtopic.
99
+ #
100
+ # This is the only exchange type to honor the :key parameter.
101
+ #
102
+ # As part of the AMQP standard, each server _should_ predeclare a topic
103
+ # exchange called 'amq.topic' (this is not required by the standard).
104
+ #
105
+ # The classic example is delivering market data. When publishing market
106
+ # data for stocks, we may subdivide the stream based on 2
107
+ # characteristics: nation code and trading symbol. The topic tree for
108
+ # Apple Computer would look like:
109
+ # 'stock.us.aapl'
110
+ # For a foreign stock, it may look like:
111
+ # 'stock.de.dax'
112
+ #
113
+ # When publishing data to the exchange, bound queues subscribing to the
114
+ # exchange indicate which data interests them by passing a routing key
115
+ # for matching against the published routing key.
116
+ #
117
+ # EM.run do
118
+ # exch = MQ::Exchange.new(MQ.new, :topic, "stocks")
119
+ # keys = ['stock.us.aapl', 'stock.de.dax']
120
+ #
121
+ # EM.add_periodic_timer(1) do # every second
122
+ # puts
123
+ # exch.publish(10+rand(10), :routing_key => keys[rand(2)])
124
+ # end
125
+ #
126
+ # # match against one dot-separated item
127
+ # MQ.queue('us stocks').bind(exch, :key => 'stock.us.*').subscribe do |price|
128
+ # puts "us stock price [#{price}]"
129
+ # end
130
+ #
131
+ # # match against multiple dot-separated items
132
+ # MQ.queue('all stocks').bind(exch, :key => 'stock.#').subscribe do |price|
133
+ # puts "all stocks: price [#{price}]"
134
+ # end
135
+ #
136
+ # # require exact match
137
+ # MQ.queue('only dax').bind(exch, :key => 'stock.de.dax').subscribe do |price|
138
+ # puts "dax price [#{price}]"
139
+ # end
140
+ # end
141
+ #
142
+ # For matching, the '*' (asterisk) wildcard matches against one
143
+ # dot-separated item only. The '#' wildcard (hash or pound symbol)
144
+ # matches against 0 or more dot-separated items. If none of these
145
+ # symbols are used, the exchange performs a comparison looking for an
146
+ # exact match.
147
+ #
148
+ # == Options
149
+ # * :passive => true | false (default false)
150
+ # If set, the server will not create the exchange if it does not
151
+ # already exist. The client can use this to check whether an exchange
152
+ # exists without modifying the server state.
153
+ #
154
+ # * :durable => true | false (default false)
155
+ # If set when creating a new exchange, the exchange will be marked as
156
+ # durable. Durable exchanges remain active when a server restarts.
157
+ # Non-durable exchanges (transient exchanges) are purged if/when a
158
+ # server restarts.
159
+ #
160
+ # A transient exchange (the default) is stored in memory-only
161
+ # therefore it is a good choice for high-performance and low-latency
162
+ # message publishing.
163
+ #
164
+ # Durable exchanges cause all messages to be written to non-volatile
165
+ # backing store (i.e. disk) prior to routing to any bound queues.
166
+ #
167
+ # * :auto_delete => true | false (default false)
168
+ # If set, the exchange is deleted when all queues have finished
169
+ # using it. The server waits for a short period of time before
170
+ # determining the exchange is unused to give time to the client code
171
+ # to bind a queue to it.
172
+ #
173
+ # If the exchange has been previously declared, this option is ignored
174
+ # on subsequent declarations.
175
+ #
176
+ # * :internal => true | false (default false)
177
+ # If set, the exchange may not be used directly by publishers, but
178
+ # only when bound to other exchanges. Internal exchanges are used to
179
+ # construct wiring that is not visible to applications.
180
+ #
181
+ # * :nowait => true | false (default true)
182
+ # If set, the server will not respond to the method. The client should
183
+ # not wait for a reply method. If the server could not complete the
184
+ # method it will raise a channel or connection exception.
185
+ #
186
+ # == Exceptions
187
+ # Doing any of these activities are illegal and will raise MQ:Error.
188
+ # * redeclare an already-declared exchange to a different type
189
+ # * :passive => true and the exchange does not exist (NOT_FOUND)
190
+ #
191
+ def initialize mq, type, name, opts = {}
192
+ @mq = mq
193
+ @type, @name, @opts = type, name, opts
194
+ @mq.exchanges[@name = name] ||= self
195
+ @key = opts[:key]
196
+
197
+ @mq.callback{
198
+ @mq.send Protocol::Exchange::Declare.new({ :exchange => name,
199
+ :type => type,
200
+ :nowait => true }.merge(opts))
201
+ } unless name == "amq.#{type}" or name == ''
202
+ end
203
+ attr_reader :name, :type, :key
204
+
205
+ # This method publishes a staged file message to a specific exchange.
206
+ # The file message will be routed to queues as defined by the exchange
207
+ # configuration and distributed to any active consumers when the
208
+ # transaction, if any, is committed.
209
+ #
210
+ # exchange = MQ.direct('name', :key => 'foo.bar')
211
+ # exchange.publish("some data")
212
+ #
213
+ # The method takes several hash key options which modify the behavior or
214
+ # lifecycle of the message.
215
+ #
216
+ # * :routing_key => 'string'
217
+ #
218
+ # Specifies the routing key for the message. The routing key is
219
+ # used for routing messages depending on the exchange configuration.
220
+ #
221
+ # * :mandatory => true | false (default false)
222
+ #
223
+ # This flag tells the server how to react if the message cannot be
224
+ # routed to a queue. If this flag is set, the server will return an
225
+ # unroutable message with a Return method. If this flag is zero, the
226
+ # server silently drops the message.
227
+ #
228
+ # * :immediate => true | false (default false)
229
+ #
230
+ # This flag tells the server how to react if the message cannot be
231
+ # routed to a queue consumer immediately. If this flag is set, the
232
+ # server will return an undeliverable message with a Return method.
233
+ # If this flag is zero, the server will queue the message, but with
234
+ # no guarantee that it will ever be consumed.
235
+ #
236
+ # * :persistent
237
+ # True or False. When true, this message will remain in the queue until
238
+ # it is consumed (if the queue is durable). When false, the message is
239
+ # lost if the server restarts and the queue is recreated.
240
+ #
241
+ # For high-performance and low-latency, set :persistent => false so the
242
+ # message stays in memory and is never persisted to non-volatile (slow)
243
+ # storage.
244
+ #
245
+ def publish data, opts = {}
246
+ @mq.callback{
247
+ out = []
248
+
249
+ out << Protocol::Basic::Publish.new({ :exchange => name,
250
+ :routing_key => opts.delete(:key) || @key }.merge(opts))
251
+
252
+ data = data.to_s
253
+
254
+ out << Protocol::Header.new(Protocol::Basic,
255
+ data.length, { :content_type => 'application/octet-stream',
256
+ :delivery_mode => (opts.delete(:persistent) ? 2 : 1),
257
+ :priority => 0 }.merge(opts))
258
+
259
+ out << Frame::Body.new(data)
260
+
261
+ @mq.send *out
262
+ }
263
+ self
264
+ end
265
+
266
+ # This method deletes an exchange. When an exchange is deleted all queue
267
+ # bindings on the exchange are cancelled.
268
+ #
269
+ # Further attempts to publish messages to a deleted exchange will raise
270
+ # an MQ::Error due to a channel close exception.
271
+ #
272
+ # exchange = MQ.direct('name', :key => 'foo.bar')
273
+ # exchange.delete
274
+ #
275
+ # == Options
276
+ # * :nowait => true | false (default true)
277
+ # If set, the server will not respond to the method. The client should
278
+ # not wait for a reply method. If the server could not complete the
279
+ # method it will raise a channel or connection exception.
280
+ #
281
+ # exchange.delete(:nowait => false)
282
+ #
283
+ # * :if_unused => true | false (default false)
284
+ # If set, the server will only delete the exchange if it has no queue
285
+ # bindings. If the exchange has queue bindings the server does not
286
+ # delete it but raises a channel exception instead (MQ:Error).
287
+ #
288
+ def delete opts = {}
289
+ @mq.callback{
290
+ @mq.send Protocol::Exchange::Delete.new({ :exchange => name,
291
+ :nowait => true }.merge(opts))
292
+ @mq.exchanges.delete name
293
+ }
294
+ nil
295
+ end
296
+
297
+ def reset
298
+ @deferred_status = nil
299
+ initialize @mq, @type, @name, @opts
300
+ end
301
+ end
302
+ end
@@ -0,0 +1,33 @@
1
+ class MQ
2
+ class Header
3
+ include AMQP
4
+
5
+ def initialize(mq, header_obj)
6
+ @mq = mq
7
+ @header = header_obj
8
+ end
9
+
10
+ # Acknowledges the receipt of this message with the server.
11
+ def ack
12
+ @mq.callback{
13
+ @mq.send Protocol::Basic::Ack.new(:delivery_tag => properties[:delivery_tag])
14
+ }
15
+ end
16
+
17
+ # Reject this message (XXX currently unimplemented in rabbitmq)
18
+ # * :requeue => true | false (default false)
19
+ def reject opts = {}
20
+ @mq.callback{
21
+ @mq.send Protocol::Basic::Reject.new(opts.merge(:delivery_tag => properties[:delivery_tag]))
22
+ }
23
+ end
24
+
25
+ def method_missing meth, *args, &blk
26
+ @header.send meth, *args, &blk
27
+ end
28
+
29
+ def inspect
30
+ @header.inspect
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,89 @@
1
+ class MQ
2
+ class Logger
3
+ def initialize *args, &block
4
+ opts = args.pop if args.last.is_a? Hash
5
+ opts ||= {}
6
+
7
+ printer(block) if block
8
+
9
+ @prop = opts
10
+ @tags = ([:timestamp] + args).uniq
11
+ end
12
+
13
+ attr_reader :prop
14
+ alias :base :prop
15
+
16
+ def log severity, *args
17
+ opts = args.pop if args.last.is_a? Hash and args.size != 1
18
+ opts ||= {}
19
+ opts = @prop.clone.update(opts)
20
+
21
+ data = args.shift
22
+
23
+ data = {:type => :exception,
24
+ :name => data.class.to_s.intern,
25
+ :backtrace => data.backtrace,
26
+ :message => data.message} if data.is_a? Exception
27
+
28
+ (@tags + args).each do |tag|
29
+ tag = tag.to_sym
30
+ case tag
31
+ when :timestamp
32
+ opts.update :timestamp => Time.now
33
+ when :hostname
34
+ @hostname ||= { :hostname => `hostname`.strip }
35
+ opts.update @hostname
36
+ when :process
37
+ @process_id ||= { :process_id => Process.pid,
38
+ :process_name => $0,
39
+ :process_parent_id => Process.ppid,
40
+ :thread_id => Thread.current.object_id }
41
+ opts.update :process => @process_id
42
+ else
43
+ (opts[:tags] ||= []) << tag
44
+ end
45
+ end
46
+
47
+ opts.update(:severity => severity,
48
+ :msg => data)
49
+
50
+ print(opts)
51
+ unless Logger.disabled?
52
+ MQ.fanout('logging', :durable => true).publish Marshal.dump(opts)
53
+ end
54
+
55
+ opts
56
+ end
57
+ alias :method_missing :log
58
+
59
+ def print data = nil, &block
60
+ if block
61
+ @printer = block
62
+ elsif data.is_a? Proc
63
+ @printer = data
64
+ elsif data
65
+ (pr = @printer || self.class.printer) and pr.call(data)
66
+ else
67
+ @printer
68
+ end
69
+ end
70
+ alias :printer :print
71
+
72
+ def self.printer &block
73
+ @printer = block if block
74
+ @printer
75
+ end
76
+
77
+ def self.disabled?
78
+ !!@disabled
79
+ end
80
+
81
+ def self.enable
82
+ @disabled = false
83
+ end
84
+
85
+ def self.disable
86
+ @disabled = true
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,433 @@
1
+ class MQ
2
+ class Queue
3
+ include AMQP
4
+
5
+ # Queues store and forward messages. Queues can be configured in the server
6
+ # or created at runtime. Queues must be attached to at least one exchange
7
+ # in order to receive messages from publishers.
8
+ #
9
+ # Like an Exchange, queue names starting with 'amq.' are reserved for
10
+ # internal use. Attempts to create queue names in violation of this
11
+ # reservation will raise MQ:Error (ACCESS_REFUSED).
12
+ #
13
+ # When a queue is created without a name, the server will generate a
14
+ # unique name internally (not currently supported in this library).
15
+ #
16
+ # == Options
17
+ # * :passive => true | false (default false)
18
+ # If set, the server will not create the exchange if it does not
19
+ # already exist. The client can use this to check whether an exchange
20
+ # exists without modifying the server state.
21
+ #
22
+ # * :durable => true | false (default false)
23
+ # If set when creating a new queue, the queue will be marked as
24
+ # durable. Durable queues remain active when a server restarts.
25
+ # Non-durable queues (transient queues) are purged if/when a
26
+ # server restarts. Note that durable queues do not necessarily
27
+ # hold persistent messages, although it does not make sense to
28
+ # send persistent messages to a transient queue (though it is
29
+ # allowed).
30
+ #
31
+ # If the queue has already been declared, any redeclaration will
32
+ # ignore this setting. A queue may only be declared durable the
33
+ # first time when it is created.
34
+ #
35
+ # * :exclusive => true | false (default false)
36
+ # Exclusive queues may only be consumed from by the current connection.
37
+ # Setting the 'exclusive' flag always implies 'auto-delete'. Only a
38
+ # single consumer is allowed to remove messages from this queue.
39
+ #
40
+ # The default is a shared queue. Multiple clients may consume messages
41
+ # from this queue.
42
+ #
43
+ # Attempting to redeclare an already-declared queue as :exclusive => true
44
+ # will raise MQ:Error.
45
+ #
46
+ # * :auto_delete = true | false (default false)
47
+ # If set, the queue is deleted when all consumers have finished
48
+ # using it. Last consumer can be cancelled either explicitly or because
49
+ # its channel is closed. If there was no consumer ever on the queue, it
50
+ # won't be deleted.
51
+ #
52
+ # The server waits for a short period of time before
53
+ # determining the queue is unused to give time to the client code
54
+ # to bind an exchange to it.
55
+ #
56
+ # If the queue has been previously declared, this option is ignored
57
+ # on subsequent declarations.
58
+ #
59
+ # * :nowait => true | false (default true)
60
+ # If set, the server will not respond to the method. The client should
61
+ # not wait for a reply method. If the server could not complete the
62
+ # method it will raise a channel or connection exception.
63
+ #
64
+ def initialize mq, name, opts = {}
65
+ @mq = mq
66
+ @opts = opts
67
+ @bindings ||= {}
68
+ @mq.queues[@name = name] ||= self
69
+ @mq.callback{
70
+ @mq.send Protocol::Queue::Declare.new({ :queue => name,
71
+ :nowait => true }.merge(opts))
72
+ }
73
+ end
74
+ attr_reader :name
75
+
76
+ # This method binds a queue to an exchange. Until a queue is
77
+ # bound it will not receive any messages. In a classic messaging
78
+ # model, store-and-forward queues are bound to a dest exchange
79
+ # and subscription queues are bound to a dest_wild exchange.
80
+ #
81
+ # A valid exchange name (or reference) must be passed as the first
82
+ # parameter. Both of these are valid:
83
+ # exch = MQ.direct('foo exchange')
84
+ # queue = MQ.queue('bar queue')
85
+ # queue.bind('foo.exchange') # OR
86
+ # queue.bind(exch)
87
+ #
88
+ # It is not valid to call #bind without the +exchange+ parameter.
89
+ #
90
+ # It is unnecessary to call #bind when the exchange name and queue
91
+ # name match exactly (for +direct+ and +fanout+ exchanges only).
92
+ # There is an implicit bind which will deliver the messages from
93
+ # the exchange to the queue.
94
+ #
95
+ # == Options
96
+ # * :key => 'some string'
97
+ # Specifies the routing key for the binding. The routing key is
98
+ # used for routing messages depending on the exchange configuration.
99
+ # Not all exchanges use a routing key - refer to the specific
100
+ # exchange documentation. If the routing key is empty and the queue
101
+ # name is empty, the routing key will be the current queue for the
102
+ # channel, which is the last declared queue.
103
+ #
104
+ # * :nowait => true | false (default true)
105
+ # If set, the server will not respond to the method. The client should
106
+ # not wait for a reply method. If the server could not complete the
107
+ # method it will raise a channel or connection exception.
108
+ #
109
+ def bind exchange, opts = {}
110
+ exchange = exchange.respond_to?(:name) ? exchange.name : exchange
111
+ @bindings[exchange] = opts
112
+
113
+ @mq.callback{
114
+ @mq.send Protocol::Queue::Bind.new({ :queue => name,
115
+ :exchange => exchange,
116
+ :routing_key => opts.delete(:key),
117
+ :nowait => true }.merge(opts))
118
+ }
119
+ self
120
+ end
121
+
122
+ # Remove the binding between the queue and exchange. The queue will
123
+ # not receive any more messages until it is bound to another
124
+ # exchange.
125
+ #
126
+ # Due to the asynchronous nature of the protocol, it is possible for
127
+ # "in flight" messages to be received after this call completes.
128
+ # Those messages will be serviced by the last block used in a
129
+ # #subscribe or #pop call.
130
+ #
131
+ # * :nowait => true | false (default true)
132
+ # If set, the server will not respond to the method. The client should
133
+ # not wait for a reply method. If the server could not complete the
134
+ # method it will raise a channel or connection exception.
135
+ #
136
+ def unbind exchange, opts = {}
137
+ exchange = exchange.respond_to?(:name) ? exchange.name : exchange
138
+ @bindings.delete exchange
139
+
140
+ @mq.callback{
141
+ @mq.send Protocol::Queue::Unbind.new({ :queue => name,
142
+ :exchange => exchange,
143
+ :routing_key => opts.delete(:key),
144
+ :nowait => true }.merge(opts))
145
+ }
146
+ self
147
+ end
148
+
149
+ # This method deletes a queue. When a queue is deleted any pending
150
+ # messages are sent to a dead-letter queue if this is defined in the
151
+ # server configuration, and all consumers on the queue are cancelled.
152
+ #
153
+ # == Options
154
+ # * :if_unused => true | false (default false)
155
+ # If set, the server will only delete the queue if it has no
156
+ # consumers. If the queue has consumers the server does does not
157
+ # delete it but raises a channel exception instead.
158
+ #
159
+ # * :if_empty => true | false (default false)
160
+ # If set, the server will only delete the queue if it has no
161
+ # messages. If the queue is not empty the server raises a channel
162
+ # exception.
163
+ #
164
+ # * :nowait => true | false (default true)
165
+ # If set, the server will not respond to the method. The client should
166
+ # not wait for a reply method. If the server could not complete the
167
+ # method it will raise a channel or connection exception.
168
+ #
169
+ def delete opts = {}
170
+ @mq.callback{
171
+ @mq.send Protocol::Queue::Delete.new({ :queue => name,
172
+ :nowait => true }.merge(opts))
173
+ }
174
+ @mq.queues.delete @name
175
+ nil
176
+ end
177
+
178
+ # This method provides a direct access to the messages in a queue
179
+ # using a synchronous dialogue that is designed for specific types of
180
+ # application where synchronous functionality is more important than
181
+ # performance.
182
+ #
183
+ # The provided block is passed a single message each time pop is called.
184
+ #
185
+ # EM.run do
186
+ # exchange = MQ.direct("foo queue")
187
+ # EM.add_periodic_timer(1) do
188
+ # exchange.publish("random number #{rand(1000)}")
189
+ # end
190
+ #
191
+ # # note that #bind is never called; it is implicit because
192
+ # # the exchange and queue names match
193
+ # queue = MQ.queue('foo queue')
194
+ # queue.pop { |body| puts "received payload [#{body}]" }
195
+ #
196
+ # EM.add_periodic_timer(1) { queue.pop }
197
+ # end
198
+ #
199
+ # If the block takes 2 parameters, both the +header+ and the +body+ will
200
+ # be passed in for processing. The header object is defined by
201
+ # AMQP::Protocol::Header.
202
+ #
203
+ # EM.run do
204
+ # exchange = MQ.direct("foo queue")
205
+ # EM.add_periodic_timer(1) do
206
+ # exchange.publish("random number #{rand(1000)}")
207
+ # end
208
+ #
209
+ # queue = MQ.queue('foo queue')
210
+ # queue.pop do |header, body|
211
+ # p header
212
+ # puts "received payload [#{body}]"
213
+ # end
214
+ #
215
+ # EM.add_periodic_timer(1) { queue.pop }
216
+ # end
217
+ #
218
+ # == Options
219
+ # * :ack => true | false (default false)
220
+ # If this field is set to false the server does not expect acknowledgments
221
+ # for messages. That is, when a message is delivered to the client
222
+ # the server automatically and silently acknowledges it on behalf
223
+ # of the client. This functionality increases performance but at
224
+ # the cost of reliability. Messages can get lost if a client dies
225
+ # before it can deliver them to the application.
226
+ #
227
+ # * :nowait => true | false (default true)
228
+ # If set, the server will not respond to the method. The client should
229
+ # not wait for a reply method. If the server could not complete the
230
+ # method it will raise a channel or connection exception.
231
+ #
232
+ def pop opts = {}, &blk
233
+ if blk
234
+ @on_pop = blk
235
+ @on_pop_opts = opts
236
+ end
237
+
238
+ @mq.callback{
239
+ @mq.get_queue{ |q|
240
+ q.push(self)
241
+ @mq.send Protocol::Basic::Get.new({ :queue => name,
242
+ :consumer_tag => name,
243
+ :no_ack => !opts.delete(:ack),
244
+ :nowait => true }.merge(opts))
245
+ }
246
+ }
247
+
248
+ self
249
+ end
250
+
251
+ # Subscribes to asynchronous message delivery.
252
+ #
253
+ # The provided block is passed a single message each time the
254
+ # exchange matches a message to this queue.
255
+ #
256
+ # EM.run do
257
+ # exchange = MQ.direct("foo queue")
258
+ # EM.add_periodic_timer(1) do
259
+ # exchange.publish("random number #{rand(1000)}")
260
+ # end
261
+ #
262
+ # queue = MQ.queue('foo queue')
263
+ # queue.subscribe { |body| puts "received payload [#{body}]" }
264
+ # end
265
+ #
266
+ # If the block takes 2 parameters, both the +header+ and the +body+ will
267
+ # be passed in for processing. The header object is defined by
268
+ # AMQP::Protocol::Header.
269
+ #
270
+ # EM.run do
271
+ # exchange = MQ.direct("foo queue")
272
+ # EM.add_periodic_timer(1) do
273
+ # exchange.publish("random number #{rand(1000)}")
274
+ # end
275
+ #
276
+ # # note that #bind is never called; it is implicit because
277
+ # # the exchange and queue names match
278
+ # queue = MQ.queue('foo queue')
279
+ # queue.subscribe do |header, body|
280
+ # p header
281
+ # puts "received payload [#{body}]"
282
+ # end
283
+ # end
284
+ #
285
+ # == Options
286
+ # * :ack => true | false (default false)
287
+ # If this field is set to false the server does not expect acknowledgments
288
+ # for messages. That is, when a message is delivered to the client
289
+ # the server automatically and silently acknowledges it on behalf
290
+ # of the client. This functionality increases performance but at
291
+ # the cost of reliability. Messages can get lost if a client dies
292
+ # before it can deliver them to the application.
293
+ #
294
+ # * :nowait => true | false (default true)
295
+ # If set, the server will not respond to the method. The client should
296
+ # not wait for a reply method. If the server could not complete the
297
+ # method it will raise a channel or connection exception.
298
+ #
299
+ def subscribe opts = {}, &blk
300
+ @consumer_tag = "#{name}-#{Kernel.rand(999_999_999_999)}"
301
+ @mq.consumers[@consumer_tag] = self
302
+
303
+ raise Error, 'already subscribed to the queue' if subscribed?
304
+
305
+ @on_msg = blk
306
+ @on_msg_opts = opts
307
+
308
+ @mq.callback{
309
+ @mq.send Protocol::Basic::Consume.new({ :queue => name,
310
+ :consumer_tag => @consumer_tag,
311
+ :no_ack => !opts.delete(:ack),
312
+ :nowait => true }.merge(opts))
313
+ }
314
+ self
315
+ end
316
+
317
+ # Removes the subscription from the queue and cancels the consumer.
318
+ # New messages will not be received by the queue. This call is similar
319
+ # in result to calling #unbind.
320
+ #
321
+ # Due to the asynchronous nature of the protocol, it is possible for
322
+ # "in flight" messages to be received after this call completes.
323
+ # Those messages will be serviced by the last block used in a
324
+ # #subscribe or #pop call.
325
+ #
326
+ # Additionally, if the queue was created with _autodelete_ set to
327
+ # true, the server will delete the queue after its wait period
328
+ # has expired unless the queue is bound to an active exchange.
329
+ #
330
+ # The method accepts a block which will be executed when the
331
+ # unsubscription request is acknowledged as complete by the server.
332
+ #
333
+ # * :nowait => true | false (default true)
334
+ # If set, the server will not respond to the method. The client should
335
+ # not wait for a reply method. If the server could not complete the
336
+ # method it will raise a channel or connection exception.
337
+ #
338
+ def unsubscribe opts = {}, &blk
339
+ @on_msg = nil
340
+ @on_cancel = blk
341
+ @mq.callback{
342
+ @mq.send Protocol::Basic::Cancel.new({ :consumer_tag => @consumer_tag }.merge(opts))
343
+ }
344
+ self
345
+ end
346
+
347
+ def publish data, opts = {}
348
+ exchange.publish(data, opts)
349
+ end
350
+
351
+ # Boolean check to see if the current queue has already been subscribed
352
+ # to an exchange.
353
+ #
354
+ # Attempts to #subscribe multiple times to any exchange will raise an
355
+ # Exception. Only a single block at a time can be associated with any
356
+ # one queue for processing incoming messages.
357
+ #
358
+ def subscribed?
359
+ !!@on_msg
360
+ end
361
+
362
+ # Passes the message to the block passed to pop or subscribe.
363
+ #
364
+ # Performs an arity check on the block's parameters. If arity == 1,
365
+ # pass only the message body. If arity != 1, pass the headers and
366
+ # the body to the block.
367
+ #
368
+ # See AMQP::Protocol::Header for the hash properties available from
369
+ # the headers parameter. See #pop or #subscribe for a code example.
370
+ #
371
+ def receive headers, body
372
+ headers = MQ::Header.new(@mq, headers)
373
+
374
+ if cb = (@on_msg || @on_pop)
375
+ cb.call *(cb.arity == 1 ? [body] : [headers, body])
376
+ end
377
+ end
378
+
379
+ # Get the number of messages and consumers on a queue.
380
+ #
381
+ # MQ.queue('name').status{ |num_messages, num_consumers|
382
+ # puts num_messages
383
+ # }
384
+ #
385
+ def status opts = {}, &blk
386
+ @on_status = blk
387
+ @mq.callback{
388
+ @mq.send Protocol::Queue::Declare.new({ :queue => name,
389
+ :passive => true }.merge(opts))
390
+ }
391
+ self
392
+ end
393
+
394
+ def recieve_status declare_ok
395
+ if @on_status
396
+ m, c = declare_ok.message_count, declare_ok.consumer_count
397
+ @on_status.call *(@on_status.arity == 1 ? [m] : [m, c])
398
+ @on_status = nil
399
+ end
400
+ end
401
+
402
+ def cancelled
403
+ @on_cancel.call if @on_cancel
404
+ @on_cancel = @on_msg = nil
405
+ @mq.consumers.delete @consumer_tag
406
+ @consumer_tag = nil
407
+ end
408
+
409
+ def reset
410
+ @deferred_status = nil
411
+ initialize @mq, @name, @opts
412
+
413
+ binds = @bindings
414
+ @bindings = {}
415
+ binds.each{|ex,opts| bind(ex, opts) }
416
+
417
+ if blk = @on_msg
418
+ @on_msg = nil
419
+ subscribe @on_msg_opts, &blk
420
+ end
421
+
422
+ if @on_pop
423
+ pop @on_pop_opts, &@on_pop
424
+ end
425
+ end
426
+
427
+ private
428
+
429
+ def exchange
430
+ @exchange ||= Exchange.new(@mq, :direct, '', :key => name)
431
+ end
432
+ end
433
+ end