Skip to content

Commit 9f0db0b

Browse files
committed
add Elixir tutorial 1
Prior to this commit, the RabbitMQ tutorials contained no Elixir examples. One _could_ look at the Erlang examples and adapt them, but that certainly does not result in idiomatic Elixir. This commit adds the first tutorial and an Elixir project file specifying the `amqp` library as the dependency for communicating with RabbitMQ via idiomatic Elixir.
1 parent 3b471a4 commit 9f0db0b

File tree

4 files changed

+61
-0
lines changed

4 files changed

+61
-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/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/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/send.exs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{:ok, connection} = AMQP.Connection.open
2+
{:ok, channel} = AMQP.Channel.open(connection)
3+
AMQP.Queue.declare(channel, "hello")
4+
AMQP.Basic.publish(channel, "", "hello", "Hello World!")
5+
IO.puts " [x] Sent 'Hello World!'"
6+
AMQP.Connection.close(connection)

0 commit comments

Comments
 (0)