Skip to content

Commit 5757dfd

Browse files
committed
add Elixir source for tutorial 5
1 parent 11d1065 commit 5757dfd

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

elixir/emit_log_topic.exs

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

elixir/receive_logs_topic.exs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
defmodule ReceiveLogsTopic do
2+
def wait_for_messages(channel) do
3+
receive do
4+
{:basic_deliver, payload, meta} ->
5+
IO.puts " [x] Received [#{meta.routing_key}] #{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, "topic_logs", :topic)
16+
17+
{:ok, %{queue: queue_name}} = AMQP.Queue.declare(channel, "", exclusive: true)
18+
19+
if length(System.argv) == 0 do
20+
IO.puts "Usage: mix run receive_logs_topic.exs [binding_key]..."
21+
System.halt(1)
22+
end
23+
for binding_key <- System.argv do
24+
AMQP.Queue.bind(channel, queue_name, "topic_logs", routing_key: binding_key)
25+
end
26+
27+
AMQP.Basic.consume(channel, queue_name, nil, no_ack: true)
28+
29+
IO.puts " [*] Waiting for messages. To exist press CTRL+C, CTRL+C"
30+
31+
ReceiveLogsTopic.wait_for_messages(channel)

0 commit comments

Comments
 (0)