/
basic.ex
424 lines (376 loc) · 19.9 KB
/
basic.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
defmodule AMQP.Basic do
@moduledoc """
Functions to publish, consume and acknowledge messages.
"""
import AMQP.Core
alias AMQP.{Channel, Utils}
@type error :: {:error, reason :: :blocked | :closing}
@type exchange :: String.t
@type queue :: String.t
@type routing_key :: String.t
@type payload :: String.t
@type delivery_tag :: integer
@type consumer_tag :: String.t
@doc """
Publishes a message to an Exchange.
This method publishes a message to a specific exchange. The message will be routed
to queues as defined by the exchange configuration and distributed to any subscribers.
The parameter `exchange` specifies the name of the exchange to publish to. If set to
empty string, it publishes to the default exchange.
The `routing_key` parameter specifies the routing key for the message.
The `payload` parameter specifies the message content as a binary.
In addition to the previous parameters, the following options can be used:
# Options
* `:mandatory` - If set, returns an error if the broker can't route the message to a queue (default `false`);
* `:immediate` - If set, returns an error if the broker can't deliver te message to a consumer immediately (default `false`);
* `:content_type` - MIME Content type;
* `:content_encoding` - MIME Content encoding;
* `:headers` - Message headers. Can be used with headers Exchanges;
* `:persistent` - If set, uses persistent delivery mode. Messages marked as `persistent` that are delivered to `durable` \
queues will be logged to disk;
* `:correlation_id` - application correlation identifier;
* `:priority` - message priority, ranging from 0 to 9;
* `:reply_to` - name of the reply queue;
* `:expiration` - how long the message is valid (in milliseconds);
* `:message_id` - message identifier;
* `:timestamp` - timestamp associated with this message (epoch time);
* `:type` - message type as a string;
* `:user_id` - creating user ID. RabbitMQ will validate this against the active connection user;
* `:app_id` - publishing application ID.
## Examples
iex> AMQP.Basic.publish chan, \"my_exchange\", \"my_routing_key\", \"Hello World!\", persistent: true
:ok
"""
@spec publish(Channel.t, exchange, routing_key, payload, keyword) :: :ok | error
def publish(%Channel{pid: pid}, exchange, routing_key, payload, options \\ []) do
basic_publish =
basic_publish(exchange: exchange,
routing_key: routing_key,
mandatory: Keyword.get(options, :mandatory, false),
immediate: Keyword.get(options, :immediate, false))
p_basic =
p_basic(content_type: Keyword.get(options, :content_type, :undefined),
content_encoding: Keyword.get(options, :content_encoding, :undefined),
headers: Keyword.get(options, :headers, :undefined) |> Utils.to_type_tuple,
delivery_mode: if(options[:persistent], do: 2, else: 1),
priority: Keyword.get(options, :priority, :undefined),
correlation_id: Keyword.get(options, :correlation_id, :undefined),
reply_to: Keyword.get(options, :reply_to, :undefined),
expiration: Keyword.get(options, :expiration, :undefined),
message_id: Keyword.get(options, :message_id, :undefined),
timestamp: Keyword.get(options, :timestamp, :undefined),
type: Keyword.get(options, :type, :undefined),
user_id: Keyword.get(options, :user_id, :undefined),
app_id: Keyword.get(options, :app_id, :undefined),
cluster_id: Keyword.get(options, :cluster_id, :undefined))
case :amqp_channel.call(pid, basic_publish, amqp_msg(props: p_basic, payload: payload)) do
:ok -> :ok
error -> {:error, error}
end
end
@doc """
Sets the message prefetch count or prefetech size (in bytes). If `global` is set to `true` this
applies to the entire Connection, otherwise it applies only to the specified Channel.
"""
@spec qos(Channel.t, keyword) :: :ok | error
def qos(%Channel{pid: pid}, options \\ []) do
basic_qos = basic_qos(prefetch_size: Keyword.get(options, :prefetch_size, 0),
prefetch_count: Keyword.get(options, :prefetch_count, 0),
global: Keyword.get(options, :global, false))
case :amqp_channel.call(pid, basic_qos) do
basic_qos_ok() -> :ok
error -> {:error, error}
end
end
@doc """
Acknowledges one or more messages. If `multiple` is set to `true`, all messages up to the one
specified by `delivery_tag` are considered acknowledged by the server.
"""
@spec ack(Channel.t, delivery_tag, keyword) :: :ok | error
def ack(%Channel{pid: pid}, delivery_tag, options \\ []) do
basic_ack = basic_ack(delivery_tag: delivery_tag,
multiple: Keyword.get(options, :multiple, false))
case :amqp_channel.call(pid, basic_ack) do
:ok -> :ok
error -> {:error, error}
end
end
@doc """
Rejects (and, optionally, requeues) a message.
"""
@spec reject(Channel.t, delivery_tag, keyword) :: :ok | error
def reject(%Channel{pid: pid}, delivery_tag, options \\ []) do
basic_reject = basic_reject(delivery_tag: delivery_tag,
requeue: Keyword.get(options, :requeue, true))
case :amqp_channel.call(pid, basic_reject) do
:ok -> :ok
error -> {:error, error}
end
end
@doc """
Negative acknowledge of one or more messages. If `multiple` is set to `true`, all messages up to the
one specified by `delivery_tag` are considered as not acknowledged by the server. If `requeue` is set
to `true`, the message will be returned to the queue and redelivered to the next available consumer.
This is a RabbitMQ specific extension to AMQP 0.9.1. It is equivalent to reject, but allows rejecting
multiple messages using the `multiple` option.
"""
@spec nack(Channel.t, delivery_tag, keyword) :: :ok | error
def nack(%Channel{pid: pid}, delivery_tag, options \\ []) do
basic_nack = basic_nack(delivery_tag: delivery_tag,
multiple: Keyword.get(options, :multiple, false),
requeue: Keyword.get(options, :requeue, true))
case :amqp_channel.call(pid, basic_nack) do
:ok -> :ok
error -> {:error, error}
end
end
@doc """
Polls a queue for an existing message.
Returns the tuple `{:empty, meta}` if the queue is empty or the tuple {:ok, payload, meta} if at least
one message exists in the queue. The returned meta map includes the entry `message_count` with the
current number of messages in the queue.
Receiving messages by polling a queue is not as as efficient as subscribing a consumer to a queue,
so consideration should be taken when receiving large volumes of messages.
Setting the `no_ack` option to true will tell the broker that the receiver will not send an acknowledgement of
the message. Once it believes it has delivered a message, then it is free to assume that the consuming application
has taken responsibility for it. In general, a lot of applications will not want these semantics, rather, they
will want to explicitly acknowledge the receipt of a message and have `no_ack` with the default value of false.
"""
@spec get(Channel.t, queue, keyword) :: {:ok, String.t, map} | {:empty, map} | error
def get(%Channel{pid: pid}, queue, options \\ []) do
case :amqp_channel.call pid, basic_get(queue: queue, no_ack: Keyword.get(options, :no_ack, false)) do
{basic_get_ok(delivery_tag: delivery_tag,
redelivered: redelivered,
exchange: exchange,
routing_key: routing_key,
message_count: message_count),
amqp_msg(props: p_basic(content_type: content_type,
content_encoding: content_encoding,
headers: headers,
delivery_mode: delivery_mode,
priority: priority,
correlation_id: correlation_id,
reply_to: reply_to,
expiration: expiration,
message_id: message_id,
timestamp: timestamp,
type: type,
user_id: user_id,
app_id: app_id,
cluster_id: cluster_id), payload: payload)} ->
{:ok, payload, %{delivery_tag: delivery_tag,
redelivered: redelivered,
exchange: exchange,
routing_key: routing_key,
message_count: message_count,
content_type: content_type,
content_encoding: content_encoding,
headers: headers,
persistent: delivery_mode == 2,
priority: priority,
correlation_id: correlation_id,
reply_to: reply_to,
expiration: expiration,
message_id: message_id,
timestamp: timestamp,
type: type,
user_id: user_id,
app_id: app_id,
cluster_id: cluster_id}}
basic_get_empty(cluster_id: cluster_id) ->
{:empty, %{cluster_id: cluster_id}}
error -> {:error, error}
end
end
@doc """
Asks the server to redeliver all unacknowledged messages on a specified channel.
If `requeue` is set to `true` the server will attempt to requeue the message,
potentially delivering it to another subscriber. Otherwise it will be redelivered
to the original recipient.
"""
@spec recover(Channel.t, keyword) :: :ok | error
def recover(%Channel{pid: pid}, options \\ []) do
basic_recover = basic_recover(requeue: Keyword.get(options, :requeue, false))
case :amqp_channel.call(pid, basic_recover) do
basic_recover_ok() -> :ok
error -> {:error, error}
end
end
@doc """
Registers a queue consumer process. The `pid` of the process can be set using
the `consumer_pid` argument and defaults to the calling process.
The consumer process will receive the following data structures:
* `{:basic_deliver, payload, meta}` - This is sent for each message consumed, where \
`payload` contains the message content and `meta` contains all the metadata set when \
sending with Basic.publish or additional info set by the broker;
* `{:basic_consume_ok, %{consumer_tag: consumer_tag}}` - Sent when the consumer \
process is registered with Basic.consume. The caller receives the same information \
as the return of Basic.consume;
* `{:basic_cancel, %{consumer_tag: consumer_tag, no_wait: no_wait}}` - Sent by the \
broker when the consumer is unexpectedly cancelled (such as after a queue deletion)
* `{:basic_cancel_ok, %{consumer_tag: consumer_tag}}` - Sent to the consumer process after a call to Basic.cancel
"""
@spec consume(Channel.t, String.t, pid | nil, keyword) :: {:ok, String.t} | error
def consume(%Channel{} = chan, queue, consumer_pid \\ nil, options \\ []) do
basic_consume =
basic_consume(queue: queue,
consumer_tag: Keyword.get(options, :consumer_tag, ""),
no_local: Keyword.get(options, :no_local, false),
no_ack: Keyword.get(options, :no_ack, false),
exclusive: Keyword.get(options, :exclusive, false),
nowait: Keyword.get(options, :no_wait, false),
arguments: Keyword.get(options, :arguments, []))
consumer_pid = consumer_pid || self()
adapter_pid = spawn fn ->
Process.flag(:trap_exit, true)
Process.monitor(consumer_pid)
Process.monitor(chan.pid)
do_start_consumer(chan, consumer_pid)
end
case :amqp_channel.subscribe(chan.pid, basic_consume, adapter_pid) do
basic_consume_ok(consumer_tag: consumer_tag) -> {:ok, consumer_tag}
error -> {:error, error}
end
end
defp do_start_consumer(chan, consumer_pid) do
receive do
basic_consume_ok(consumer_tag: consumer_tag) ->
send consumer_pid, {:basic_consume_ok, %{consumer_tag: consumer_tag}}
do_consume(chan, consumer_pid, consumer_tag)
end
end
defp do_consume(chan, consumer_pid, consumer_tag) do
receive do
{basic_deliver(consumer_tag: consumer_tag,
delivery_tag: delivery_tag,
redelivered: redelivered,
exchange: exchange,
routing_key: routing_key),
amqp_msg(props: p_basic(content_type: content_type,
content_encoding: content_encoding,
headers: headers,
delivery_mode: delivery_mode,
priority: priority,
correlation_id: correlation_id,
reply_to: reply_to,
expiration: expiration,
message_id: message_id,
timestamp: timestamp,
type: type,
user_id: user_id,
app_id: app_id,
cluster_id: cluster_id), payload: payload)} ->
send consumer_pid, {:basic_deliver, payload, %{consumer_tag: consumer_tag,
delivery_tag: delivery_tag,
redelivered: redelivered,
exchange: exchange,
routing_key: routing_key,
content_type: content_type,
content_encoding: content_encoding,
headers: headers,
persistent: delivery_mode == 2,
priority: priority,
correlation_id: correlation_id,
reply_to: reply_to,
expiration: expiration,
message_id: message_id,
timestamp: timestamp,
type: type,
user_id: user_id,
app_id: app_id,
cluster_id: cluster_id}}
do_consume(chan, consumer_pid, consumer_tag)
basic_consume_ok(consumer_tag: consumer_tag) ->
send consumer_pid, {:basic_consume_ok, %{consumer_tag: consumer_tag}}
do_consume(chan, consumer_pid, consumer_tag)
basic_cancel_ok(consumer_tag: consumer_tag) ->
send consumer_pid, {:basic_cancel_ok, %{consumer_tag: consumer_tag}}
basic_cancel(consumer_tag: consumer_tag, nowait: no_wait) ->
send consumer_pid, {:basic_cancel, %{consumer_tag: consumer_tag, no_wait: no_wait}}
{:DOWN, _ref, :process, ^consumer_pid, reason} ->
cancel(chan, consumer_tag)
exit(reason)
{:DOWN, _ref, :process, _pid, reason} ->
exit(reason)
end
end
@doc """
End a queue consumer.
This method cancels a consumer. This does not affect already delivered messages, but it does
mean the server will not send any more messages for that consumer. The client may receive an
arbitrary number of messages in between sending the cancel method and receiving the reply.
"""
@spec cancel(Channel.t, String.t, keyword) :: {:ok, String.t} | error
def cancel(%Channel{pid: pid}, consumer_tag, options \\ []) do
basic_cancel = basic_cancel(consumer_tag: consumer_tag, nowait: Keyword.get(options, :no_wait, false))
case :amqp_channel.call(pid, basic_cancel) do
basic_cancel_ok(consumer_tag: consumer_tag) -> {:ok, consumer_tag}
error -> {:error, error}
end
end
@doc """
Registers a handler to deal with returned messages. The registered
process will receive `{:basic_return, payload, meta}` data structures.
"""
@spec return(Channel.t, pid) :: :ok
def return(%Channel{pid: pid}, return_handler_pid) do
adapter_pid = spawn fn ->
Process.flag(:trap_exit, true)
Process.monitor(return_handler_pid)
Process.monitor(pid)
handle_return_messages(pid, return_handler_pid)
end
:amqp_channel.register_return_handler(pid, adapter_pid)
end
@doc """
Removes the return handler, if it exists. Does nothing if there is no
such handler.
"""
@spec cancel_return(Channel.t) :: :ok
def cancel_return(%Channel{pid: pid}) do
:amqp_channel.unregister_return_handler(pid)
end
defp handle_return_messages(chan_pid, return_handler_pid) do
receive do
{basic_return(reply_code: reply_code,
reply_text: reply_text,
exchange: exchange,
routing_key: routing_key),
amqp_msg(props: p_basic(content_type: content_type,
content_encoding: content_encoding,
headers: headers,
delivery_mode: delivery_mode,
priority: priority,
correlation_id: correlation_id,
reply_to: reply_to,
expiration: expiration,
message_id: message_id,
timestamp: timestamp,
type: type,
user_id: user_id,
app_id: app_id,
cluster_id: cluster_id), payload: payload)} ->
send return_handler_pid, {:basic_return, payload, %{reply_code: reply_code,
reply_text: reply_text,
exchange: exchange,
routing_key: routing_key,
content_type: content_type,
content_encoding: content_encoding,
headers: headers,
persistent: delivery_mode == 2,
priority: priority,
correlation_id: correlation_id,
reply_to: reply_to,
expiration: expiration,
message_id: message_id,
timestamp: timestamp,
type: type,
user_id: user_id,
app_id: app_id,
cluster_id: cluster_id}}
handle_return_messages(chan_pid, return_handler_pid)
{:DOWN, _ref, :process, _pid, reason} ->
exit(reason)
end
end
end