Skip to content

Commit

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

{severities, raw_message, _} =
System.argv
|> OptionParser.parse(strict: [info: :boolean,
warning: :boolean,
error: :boolean])
|> case do
{[], msg, _} -> {[info: true], msg, []}
other -> other
end

message =
case raw_message do
[] -> "Hello World!"
words -> Enum.join(words, " ")
end

AMQP.Exchange.declare(channel, "direct_logs", :direct)

for {severity, true} <- severities do
severity = severity |> to_string
AMQP.Basic.publish(channel, "direct_logs", severity, message)
IO.puts " [x] Sent '[#{severity}] #{message}'"
end

AMQP.Connection.close(connection)
35 changes: 35 additions & 0 deletions elixir/receive_logs_direct.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
defmodule ReceiveLogsDirect 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)

{severities, _, _} =
System.argv
|> OptionParser.parse(strict: [info: :boolean,
warning: :boolean,
error: :boolean])

AMQP.Exchange.declare(channel, "direct_logs", :direct)

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

for {severity, true} <- severities do
binding_key = severity |> to_string
AMQP.Queue.bind(channel, queue_name, "direct_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"


ReceiveLogsDirect.wait_for_messages(channel)

0 comments on commit 11d1065

Please sign in to comment.