Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- defmodule MT.Client do
- require Logger
- @host Application.get_env(:mt, :host)
- @username Application.get_env(:mt, :username)
- @password Application.get_env(:mt, :password)
- @channel Application.get_env(:mt, :channel)
- def start_link do
- MT.Client.connect
- end
- def connect do
- {:ok, connection} = AMQP.Connection.open(host: @host, username: @username, password: @password)
- {:ok, channel} = AMQP.Channel.open(connection)
- AMQP.Queue.declare(channel, @channel, durable: true)
- AMQP.Basic.qos(channel, prefetch_count: 1)
- AMQP.Basic.consume(channel, @channel)
- Logger.info "[*] Waiting for messages"
- MT.Client.loop(channel)
- end
- def loop(channel) do
- receive do
- {:basic_deliver, payload, meta} ->
- Logger.info "[x] Received #{payload}"
- payload
- |> to_char_list
- |> Enum.count(fn x -> x == ?. end)
- |> Kernel.*(1000)
- |> :timer.sleep
- Logger.info "[x] Done."
- AMQP.Basic.ack(channel, meta.delivery_tag)
- MT.Client.loop(channel)
- end
- end
- end
- defmodule MT.Client.Supervisor do
- use Supervisor
- require Logger
- @name MTClientSupervisor
- def start_link do
- Supervisor.start_link(__MODULE__, :ok, name: @name)
- end
- def init(:ok) do
- children = [
- worker(MT.Client, [], restart: :transient, id: "MTClient01"),
- worker(MT.Client, [], restart: :transient, id: "MTClient02"),
- worker(MT.Client, [], restart: :transient, id: "MTClient03")
- ]
- supervise(children, strategy: :one_for_one)
- end
- end
- iex -S mix
- MT.Client.Supervisor.start_link`
- 08:46:50.746 [info] [*] Waiting for messages
- 08:46:50.746 [info] [x] Received {"job":"TestMessage","data":{"message":"message........"}}
- 08:46:58.747 [info] [x] Done.
- 08:46:58.748 [info] [x] Received {"job":"TestMessage","data":{"message":"last........"}}
- 08:47:06.749 [info] [x] Done.
- MT.Client.start_link
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement