Skip to content

Commit

Permalink
add Elixir source for tutorial 5
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffweiss committed Dec 22, 2015
1 parent 11d1065 commit 5757dfd
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
17 changes: 17 additions & 0 deletions elixir/emit_log_topic.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{:ok, connection} = AMQP.Connection.open
{:ok, channel} = AMQP.Channel.open(connection)

{topic, message} =
System.argv
|> case do
[] -> {"anonymous.info", "Hello World!"}
[message] -> {"anonymous.info", message}
[topic|words] -> {topic, Enum.join(words, " ")}
end

AMQP.Exchange.declare(channel, "topic_logs", :topic)

AMQP.Basic.publish(channel, "topic_logs", topic, message)
IO.puts " [x] Sent '[#{topic}] #{message}'"

AMQP.Connection.close(connection)
31 changes: 31 additions & 0 deletions elixir/receive_logs_topic.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
defmodule ReceiveLogsTopic do
def wait_for_messages(channel) do
receive do
{:basic_deliver, payload, meta} ->
IO.puts " [x] Received [#{meta.routing_key}] #{payload}"

wait_for_messages(channel)
end
end
end

{:ok, connection} = AMQP.Connection.open
{:ok, channel} = AMQP.Channel.open(connection)

AMQP.Exchange.declare(channel, "topic_logs", :topic)

{:ok, %{queue: queue_name}} = AMQP.Queue.declare(channel, "", exclusive: true)

if length(System.argv) == 0 do
IO.puts "Usage: mix run receive_logs_topic.exs [binding_key]..."
System.halt(1)
end
for binding_key <- System.argv do
AMQP.Queue.bind(channel, queue_name, "topic_logs", routing_key: binding_key)
end

AMQP.Basic.consume(channel, queue_name, nil, no_ack: true)

IO.puts " [*] Waiting for messages. To exist press CTRL+C, CTRL+C"

ReceiveLogsTopic.wait_for_messages(channel)

0 comments on commit 5757dfd

Please sign in to comment.