gen_amqp v3.0.0 GenAMQP
GenAMQP is a library to create easily Publish/Subscribe and RPC style in AMQP by defining some settings and using a friendly macro
In the settings file put:
config :gen_amqp,
connections: [
{:static, StaticConnSup, ConnHub, "amqp://guest:guest@localhost"},
{:dynamic, DynamicConnSup, "amqp://guest:guest@localhost"}
],
error_handler: ErrorHandler
The error handler must handle a failure structure that can be any
defmodule ErrorHandler do
def handle(msg) do
Poison.encode!(%{
status: :error,
code: 0,
message: msg
})
end
end
The ServerDemo here uses the GenAMQP.Server and implements two functions, execute and handle. The execute function receives the incoming payload in string format and must return the tuple {:reply, content} where content is the response that will be returned in amqp or no reply if you don’t need to response. The handle function handles the cases not matched in the execute function.
defmodule ServerDemo do
@moduledoc false
use GenAMQP.Server, event: "server_demo", conn_name: Application.get_env(:gen_amqp, :conn_name)
def execute(payload) do
with {:ok, _} <- {:error, "error"} do
{:reply, "ok"}
end
end
def handle({:error, cause}) do
{:reply, cause}
end
end
In the systems there is a supervisor for the connection that can be dynamic or static, if it’s static supervises one connection, if it’s dynamic creates a new supervised connection for each client
defmodule DemoApp do
@moduledoc false
use Application
def start(_type, _args) do
import Supervisor.Spec, warn: false
conn_name = Application.get_env(:gen_amqp, :conn_name)
static_sup_name = Application.get_env(:gen_amqp, :static_sup_name)
dynamic_sup_name = Application.get_env(:gen_amqp, :dynamic_sup_name)
# Define supervisors and child supervisors to be supervised
children = [
supervisor(GenAMQP.ConnSupervisor, [static_sup_name, conn_name], [id: static_sup_name]),
supervisor(GenAMQP.ConnSupervisor, [dynamic_sup_name], id: dynamic_sup_name),
supervisor(ServerDemo, []),
]
opts = [strategy: :one_for_one, name: Core.Supervisor]
Supervisor.start_link(children, opts)
end
end
Summary
Functions
Called when an application is started
Functions
Called when an application is started.
This function is called when an application is started using
Application.start/2
(and functions on top of that, such as
Application.ensure_started/2
). This function should start the top-level
process of the application (which should be the top supervisor of the
application’s supervision tree if the application follows the OTP design
principles around supervision).
start_type
defines how the application is started:
:normal
- used if the startup is a normal startup or if the application is distributed and is started on the current node because of a failover from another node and the application specification key:start_phases
is:undefined
.{:takeover, node}
- used if the application is distributed and is started on the current node because of a failover on the nodenode
.{:failover, node}
- used if the application is distributed and is started on the current node because of a failover on nodenode
, and the application specification key:start_phases
is not:undefined
.
start_args
are the arguments passed to the application in the :mod
specification key (e.g., mod: {MyApp, [:my_args]}
).
This function should either return {:ok, pid}
or {:ok, pid, state}
if
startup is successful. pid
should be the PID of the top supervisor. state
can be an arbitrary term, and if omitted will default to []
; if the
application is later stopped, state
is passed to the stop/1
callback (see
the documentation for the c:stop/1
callback for more information).
use Application
provides no default implementation for the start/2
callback.
Callback implementation for Application.start/2
.