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.
This commit is contained in:
Jeff Weiss 2015-12-12 12:25:12 -08:00
parent 3b471a45d7
commit 9f0db0be04
No known key found for this signature in database
GPG Key ID: 863E21C5E735FBB2
4 changed files with 61 additions and 0 deletions

4
elixir/.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
.mix_tasks
mix.lock
/_build
/deps

34
elixir/mix.exs Normal file
View File

@ -0,0 +1,34 @@
defmodule RabbitmqTutorials.Mixfile do
use Mix.Project
def project do
[app: :rabbitmq_tutorials,
version: "0.0.1",
elixir: "~> 1.1",
build_embedded: Mix.env == :prod,
start_permanent: Mix.env == :prod,
deps: deps]
end
# Configuration for the OTP application
#
# Type "mix help compile.app" for more information
def application do
[applications: [:logger, :amqp]]
end
# Dependencies can be Hex packages:
#
# {:mydep, "~> 0.3.0"}
#
# Or git/path repositories:
#
# {:mydep, git: "https://github.com/elixir-lang/mydep.git", tag: "0.1.0"}
#
# Type "mix help deps" for more examples and options
defp deps do
[
{:amqp, "~> 0.1.4"},
]
end
end

17
elixir/receive.exs Normal file
View File

@ -0,0 +1,17 @@
defmodule Receive do
def wait_for_messages do
receive do
{:basic_deliver, payload, _meta} ->
IO.puts " [x] Received #{payload}"
wait_for_messages
end
end
end
{:ok, connection} = AMQP.Connection.open
{:ok, channel} = AMQP.Channel.open(connection)
AMQP.Queue.declare(channel, "hello")
AMQP.Basic.consume(channel, "hello", nil, no_ack: true)
IO.puts " [*] Waiting for messages. To exit press CTRL+C, CTRL+C"
Receive.wait_for_messages

6
elixir/send.exs Normal file
View File

@ -0,0 +1,6 @@
{:ok, connection} = AMQP.Connection.open
{:ok, channel} = AMQP.Channel.open(connection)
AMQP.Queue.declare(channel, "hello")
AMQP.Basic.publish(channel, "", "hello", "Hello World!")
IO.puts " [x] Sent 'Hello World!'"
AMQP.Connection.close(connection)