gen_amqp v5.0.5 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"}
      ],
      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

Link to this section Summary

Functions

Called when an application is started

Link to this section Functions

Link to this function

start(type, args)

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 node node.
  • {:failover, node} - used if the application is distributed and is started on the current node because of a failover on node node, 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.