diff --git a/elixir/emit_log_topic.exs b/elixir/emit_log_topic.exs new file mode 100644 index 00000000..5e4e9697 --- /dev/null +++ b/elixir/emit_log_topic.exs @@ -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) diff --git a/elixir/receive_logs_topic.exs b/elixir/receive_logs_topic.exs new file mode 100644 index 00000000..00e408de --- /dev/null +++ b/elixir/receive_logs_topic.exs @@ -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)