Skip to content

Commit aa20759

Browse files
committed
add Elixir source for tutorial 2
1 parent 9f0db0b commit aa20759

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

elixir/new_task.exs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{:ok, connection} = AMQP.Connection.open
2+
{:ok, channel} = AMQP.Channel.open(connection)
3+
4+
AMQP.Queue.declare(channel, "task_queue", durable: true)
5+
6+
message =
7+
case System.argv do
8+
[] -> "Hello World!"
9+
words -> Enum.join(words, " ")
10+
end
11+
12+
AMQP.Basic.publish(channel, "", "hello", message, persistent: true)
13+
IO.puts " [x] Sent '#{message}'"
14+
15+
AMQP.Connection.close(connection)

elixir/worker.exs

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
defmodule Worker do
2+
def wait_for_messages(channel) do
3+
receive do
4+
{:basic_deliver, payload, meta} ->
5+
IO.puts " [x] Received #{payload}"
6+
payload
7+
|> to_char_list
8+
|> Enum.count(fn x -> x == ?. end)
9+
|> Kernel.*(1000)
10+
|> :timer.sleep
11+
IO.puts " [x] Done."
12+
AMQP.Basic.ack(channel, meta.delivery_tag)
13+
14+
wait_for_messages(channel)
15+
end
16+
end
17+
end
18+
19+
{:ok, connection} = AMQP.Connection.open
20+
{:ok, channel} = AMQP.Channel.open(connection)
21+
22+
AMQP.Queue.declare(channel, "task_queue", durable: true)
23+
AMQP.Basic.qos(channel, prefetch_count: 1)
24+
25+
AMQP.Basic.consume(channel, "hello")
26+
IO.puts " [*] Waiting for messages. To exit press CTRL+C, CTRL+C"
27+
28+
Worker.wait_for_messages(channel)

0 commit comments

Comments
 (0)