From 9f0db0be049ab6827afd9083fb7d4e8678ebe128 Mon Sep 17 00:00:00 2001 From: Jeff Weiss Date: Sat, 12 Dec 2015 12:25:12 -0800 Subject: [PATCH] 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. --- elixir/.gitignore | 4 ++++ elixir/mix.exs | 34 ++++++++++++++++++++++++++++++++++ elixir/receive.exs | 17 +++++++++++++++++ elixir/send.exs | 6 ++++++ 4 files changed, 61 insertions(+) create mode 100644 elixir/.gitignore create mode 100644 elixir/mix.exs create mode 100644 elixir/receive.exs create mode 100644 elixir/send.exs diff --git a/elixir/.gitignore b/elixir/.gitignore new file mode 100644 index 00000000..c5e7de21 --- /dev/null +++ b/elixir/.gitignore @@ -0,0 +1,4 @@ +.mix_tasks +mix.lock +/_build +/deps diff --git a/elixir/mix.exs b/elixir/mix.exs new file mode 100644 index 00000000..22487417 --- /dev/null +++ b/elixir/mix.exs @@ -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 \ No newline at end of file diff --git a/elixir/receive.exs b/elixir/receive.exs new file mode 100644 index 00000000..9d06f241 --- /dev/null +++ b/elixir/receive.exs @@ -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 diff --git a/elixir/send.exs b/elixir/send.exs new file mode 100644 index 00000000..4a84a10f --- /dev/null +++ b/elixir/send.exs @@ -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)