Skip to content

Commit 7ba330d

Browse files
Merge branch 'add_elixir_tutorials' of https://github.com/jeffweiss/rabbitmq-tutorials into jeffweiss-add_elixir_tutorials
2 parents 55374bd + 5c7c726 commit 7ba330d

14 files changed

+319
-0
lines changed

elixir/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.mix_tasks
2+
mix.lock
3+
/_build
4+
/deps

elixir/emit_log.exs

Lines changed: 14 additions & 0 deletions
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/emit_log_direct.exs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{:ok, connection} = AMQP.Connection.open
2+
{:ok, channel} = AMQP.Channel.open(connection)
3+
4+
{severities, raw_message, _} =
5+
System.argv
6+
|> OptionParser.parse(strict: [info: :boolean,
7+
warning: :boolean,
8+
error: :boolean])
9+
|> case do
10+
{[], msg, _} -> {[info: true], msg, []}
11+
other -> other
12+
end
13+
14+
message =
15+
case raw_message do
16+
[] -> "Hello World!"
17+
words -> Enum.join(words, " ")
18+
end
19+
20+
AMQP.Exchange.declare(channel, "direct_logs", :direct)
21+
22+
for {severity, true} <- severities do
23+
severity = severity |> to_string
24+
AMQP.Basic.publish(channel, "direct_logs", severity, message)
25+
IO.puts " [x] Sent '[#{severity}] #{message}'"
26+
end
27+
28+
AMQP.Connection.close(connection)

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/mix.exs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
defmodule RabbitmqTutorials.Mixfile do
2+
use Mix.Project
3+
4+
def project do
5+
[app: :rabbitmq_tutorials,
6+
version: "0.0.1",
7+
elixir: "~> 1.1",
8+
build_embedded: Mix.env == :prod,
9+
start_permanent: Mix.env == :prod,
10+
deps: deps]
11+
end
12+
13+
# Configuration for the OTP application
14+
#
15+
# Type "mix help compile.app" for more information
16+
def application do
17+
[applications: [:logger, :amqp]]
18+
end
19+
20+
# Dependencies can be Hex packages:
21+
#
22+
# {:mydep, "~> 0.3.0"}
23+
#
24+
# Or git/path repositories:
25+
#
26+
# {:mydep, git: "https://github.com/elixir-lang/mydep.git", tag: "0.1.0"}
27+
#
28+
# Type "mix help deps" for more examples and options
29+
defp deps do
30+
[
31+
{:amqp, "~> 0.1.4"},
32+
]
33+
end
34+
end

elixir/new_task.exs

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

elixir/receive.exs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
defmodule Receive do
2+
def wait_for_messages do
3+
receive do
4+
{:basic_deliver, payload, _meta} ->
5+
IO.puts " [x] Received #{payload}"
6+
wait_for_messages
7+
end
8+
end
9+
end
10+
11+
{:ok, connection} = AMQP.Connection.open
12+
{:ok, channel} = AMQP.Channel.open(connection)
13+
AMQP.Queue.declare(channel, "hello")
14+
AMQP.Basic.consume(channel, "hello", nil, no_ack: true)
15+
IO.puts " [*] Waiting for messages. To exit press CTRL+C, CTRL+C"
16+
17+
Receive.wait_for_messages

elixir/receive_logs.exs

Lines changed: 21 additions & 0 deletions
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)

elixir/receive_logs_direct.exs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
defmodule ReceiveLogsDirect 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+
{severities, _, _} =
16+
System.argv
17+
|> OptionParser.parse(strict: [info: :boolean,
18+
warning: :boolean,
19+
error: :boolean])
20+
21+
AMQP.Exchange.declare(channel, "direct_logs", :direct)
22+
23+
{:ok, %{queue: queue_name}} = AMQP.Queue.declare(channel, "", exclusive: true)
24+
25+
for {severity, true} <- severities do
26+
binding_key = severity |> to_string
27+
AMQP.Queue.bind(channel, queue_name, "direct_logs", routing_key: binding_key)
28+
end
29+
30+
AMQP.Basic.consume(channel, queue_name, nil, no_ack: true)
31+
32+
IO.puts " [*] Waiting for messages. To exist press CTRL+C, CTRL+C"
33+
34+
35+
ReceiveLogsDirect.wait_for_messages(channel)

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)