Skip to content

Commit 37086be

Browse files
committed
add Elixir source for tutorial 3
1 parent aa20759 commit 37086be

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

elixir/emit_log.exs

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

elixir/receive_logs.exs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
defmodule ReceiveLogs do
2+
def wait_for_messages(channel) do
3+
receive do
4+
{:basic_deliver, payload, _meta} ->
5+
IO.puts " [x] Received #{payload}"
6+
7+
wait_for_messages(channel)
8+
end
9+
end
10+
end
11+
12+
{:ok, connection} = AMQP.Connection.open
13+
{:ok, channel} = AMQP.Channel.open(connection)
14+
15+
AMQP.Exchange.declare(channel, "logs", :fanout)
16+
{:ok, %{queue: queue_name}} = AMQP.Queue.declare(channel, "", exclusive: true)
17+
AMQP.Queue.bind(channel, queue_name, "logs")
18+
AMQP.Basic.consume(channel, queue_name, nil, no_ack: true)
19+
IO.puts " [*] Waiting for messages. To exit press CTRL+C, CTRL+C"
20+
21+
ReceiveLogs.wait_for_messages(channel)

0 commit comments

Comments
 (0)