Skip to content

Commit

Permalink
initial commit with basics 1 example
Browse files Browse the repository at this point in the history
  • Loading branch information
luos committed Feb 28, 2020
0 parents commit bcc0240
Show file tree
Hide file tree
Showing 6 changed files with 196 additions and 0 deletions.
27 changes: 27 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# The directory Mix will write compiled artifacts to.
/_build/

# If you run "mix test --cover", coverage assets end up here.
/cover/

# The directory Mix downloads your dependencies sources to.
/deps/

# Where 3rd-party dependencies like ExDoc output generated docs.
/doc/

# Ignore .fetch files in case you like to edit your project deps locally.
/.fetch

# If the VM crashes, it generates a dump, let's ignore it too.
erl_crash.dump

# Also ignore archive artifacts (built via "mix archive.build").
*.ez

# Ignore package tarball (built via "mix hex.build").
consumer_and_publisher-*.tar

/.history/
/log/
/.elixir_ls/
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# ConsumerAndPublisher

Demo Application for Erlang Solutions Budapest Office RabbitMQ Dojo.

## Installation

1. Clone this repo

2. `mix deps.get`

## Usage

### Baics 1

Fill in the gaps. Don't look in the `_solutions` file. :)
94 changes: 94 additions & 0 deletions lib/basics_1.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
require Logger
alias AMQP.{Connection, Channel, Queue, Basic, Exchange}

rabbitmq_host = "rabbitmq.gerecs.hu"

rabbitmq_user = "guest"

rabbitmq_password = "guest"

your_name = "##PUT YOUR NAME HERE%%"


{:ok, my_connection} = Connection.open(host: rabbitmq_host, port: 5672, heartbeat: 60, name: "Connection of " <> your_name)

{:ok, my_channel} = Channel.open(my_connection)

Logger.info("")
Logger.info("")
Logger.info("")

## Declare a DIRECT exchange with your name, for example robert-exchange-1

# :ok = ...

## Declare a queue with your name, for example robert-queue-1

# {:ok, _queue_info} = ...

## Bind the queue to the exchange, using today's date as the ROUTING KEY, for example 2020-02-28

# :ok = ...

## Publish a persistent message to the queue through the exchange

# :ok = ...

## Consume the message from the queue, for this we have to subscribe to the message stream

# {:ok, _tag} = ...

receive do
{:basic_consume_ok, _} ->
Logger.info("Successfully subscribed to our queue")
after 5000 ->
Logger.info("Something went wrong")
Kernel.exit(1)
end

receive do
{:basic_deliver, payload, metadata} ->
Logger.info("Received message: ")
Logger.info(inspect(payload))
Logger.info(inspect(metadata))

## Acknowledge the message

:timer.sleep(60000) # remove this line after acking the message

after 5000 ->
Logger.info("Something went wrong")
Kernel.exit(1)
end


Logger.info("Exiting...")
:ok = Channel.close(my_channel)
:ok = Connection.close(my_connection)



























22 changes: 22 additions & 0 deletions lib/basics_1_solutions.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
## Declare a DIRECT exchange with your name, for example robert-exchange-1

:ok = Exchange.declare(my_channel, "lajos-exchange-1", :direct)

## Declare a queue with your name, for example robert-queue-1

{:ok, _queue_info} = Queue.declare(my_channel, "lajos-queue-1", durable: true)

## Bind the queue to the exchange, using today's date as the ROUTING KEY, for example 2020-02-28

:ok = Queue.bind(my_channel, "lajos-queue-1", "lajos-exchange-1", routing_key: "28th of February")

## Publish a persistent message to the queue through the exchange

:ok = Basic.publish(my_channel, "lajos-exchange-1", "28th of February", "hello world", persistent: true)

## Consume the message from the queue, for this we have to subscribe to the message stream

{:ok, _tag} = Basic.consume(my_channel, "lajos-queue-1")


Basic.ack(my_channel, delivery_tag)
26 changes: 26 additions & 0 deletions mix.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
defmodule ConsumerAndPublisher.MixProject do
use Mix.Project

def project do
[
app: :consumer_and_publisher,
version: "0.1.0",
elixir: "~> 1.6",
start_permanent: Mix.env() == :prod,
deps: deps()
]
end

def application do
[
extra_applications: [:logger]
]
end

defp deps do
[
{:amqp, "~> 1.0"},
{:poison, "~> 3.1"}
]
end
end
12 changes: 12 additions & 0 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
%{
"amqp": {:hex, :amqp, "1.4.1", "b4ccde3cd5c097fb9ee44bc061af4468db6d3d2297a0515dea6156a49b0e116e", [:mix], [{:amqp_client, "~> 3.8.0", [hex: :amqp_client, repo: "hexpm", optional: false]}], "hexpm"},
"amqp_client": {:hex, :amqp_client, "3.8.2", "b50ac381c3c016a697d6ab8f08367043a08358cfeb8ee97832ccc7d101e59cef", [:make, :rebar3], [{:rabbit_common, "3.8.2", [hex: :rabbit_common, repo: "hexpm", optional: false]}], "hexpm"},
"credentials_obfuscation": {:hex, :credentials_obfuscation, "1.1.0", "513793cc20c18afc9e03e584b436192a751a8344890e03a8741c65c8d6866fab", [:rebar3], [], "hexpm"},
"goldrush": {:hex, :goldrush, "0.1.9", "f06e5d5f1277da5c413e84d5a2924174182fb108dabb39d5ec548b27424cd106", [:rebar3], [], "hexpm"},
"jsx": {:hex, :jsx, "2.9.0", "d2f6e5f069c00266cad52fb15d87c428579ea4d7d73a33669e12679e203329dd", [:mix, :rebar3], [], "hexpm"},
"lager": {:hex, :lager, "3.8.0", "3402b9a7e473680ca179fc2f1d827cab88dd37dd1e6113090c6f45ef05228a1c", [:rebar3], [{:goldrush, "0.1.9", [hex: :goldrush, repo: "hexpm", optional: false]}], "hexpm"},
"poison": {:hex, :poison, "3.1.0", "d9eb636610e096f86f25d9a46f35a9facac35609a7591b3be3326e99a0484665", [:mix], [], "hexpm"},
"rabbit_common": {:hex, :rabbit_common, "3.8.2", "6f5653e7ba8bbf76447b126d1ac224e1be5ed853808542bd67cbcff87fbd2493", [:make, :rebar3], [{:credentials_obfuscation, "1.1.0", [hex: :credentials_obfuscation, repo: "hexpm", optional: false]}, {:jsx, "2.9.0", [hex: :jsx, repo: "hexpm", optional: false]}, {:lager, "3.8.0", [hex: :lager, repo: "hexpm", optional: false]}, {:ranch, "1.7.1", [hex: :ranch, repo: "hexpm", optional: false]}, {:recon, "2.5.0", [hex: :recon, repo: "hexpm", optional: false]}], "hexpm"},
"ranch": {:hex, :ranch, "1.7.1", "6b1fab51b49196860b733a49c07604465a47bdb78aa10c1c16a3d199f7f8c881", [:rebar3], [], "hexpm"},
"recon": {:hex, :recon, "2.5.0", "2f7fcbec2c35034bade2f9717f77059dc54eb4e929a3049ca7ba6775c0bd66cd", [:mix, :rebar3], [], "hexpm"},
}

0 comments on commit bcc0240

Please sign in to comment.