Packages
amqp
0.0.5
4.1.1
4.1.0
4.0.0
4.0.0-rc.1
3.3.2
3.3.1
retired
3.3.0
3.2.0
3.1.1
3.1.0
3.0.1
3.0.0
3.0.0-rc.1
2.1.2
2.1.1
2.1.0
2.0.0
2.0.0-rc.2
2.0.0-rc.1
1.6.0
1.5.0
1.4.2
1.4.1
1.4.0
1.4.0-rc.0
1.3.2
1.3.1
1.3.0
1.2.2
1.2.1
1.2.0
1.2.0-rc.0
1.1.1
1.1.0
1.0.3
1.0.2
1.0.1
1.0.0
1.0.0-pre.4
1.0.0-pre.3
1.0.0-pre.2
1.0.0-pre.1
0.3.1
0.3.0
0.2.4
0.2.3
0.2.2
0.2.1
0.2.0
0.2.0-pre.2
0.2.0-pre.1
0.1.5
0.1.4
0.1.3
0.1.2
0.1.1
0.1.0
0.0.6
0.0.5
0.0.4
0.0.3
Idiomatic Elixir client for RabbitMQ.
Current section
Files
Jump to
Current section
Files
lib/amqp/connection.ex
defmodule AMQP.Connection do
@moduledoc """
Functions to operate on Connections.
"""
import Record
import AMQP.Core
alias __MODULE__
defstruct [:pid]
@doc """
Opens an new Connection to an AMQP broker.
The connection parameters can be passed as a keyword list or as a AMQP URI.
When using a keyword list, the options `host`, `port`, `virtual_host`,
`username` and `password` can be used.
## Examples
iex> AMQP.Connection.open host: \"localhost\", port: 5672, virtual_host: \"/\", username: \"guest\", password: \"guest\"
{:ok, %AMQP.Connection{}}
"""
def open(options \\ [])
def open(options) when is_list(options) do
amqp_params_network(username: Keyword.get(options, :username, "guest"),
password: Keyword.get(options, :password, "guest"),
virtual_host: Keyword.get(options, :virtual_host, "/"),
host: Keyword.get(options, :host, 'localhost') |> normalize_host,
port: Keyword.get(options, :port, :undefined),
channel_max: Keyword.get(options, :channel_max, 0),
frame_max: Keyword.get(options, :frame_max, 0),
heartbeat: Keyword.get(options, :heartbeat, 0),
connection_timeout: Keyword.get(options, :connection_timeout, :infinity),
ssl_options: Keyword.get(options, :ssl_options, :none),
auth_mechanisms: Keyword.get(options, :auth_mechanisms, [&:amqp_auth_mechanisms.plain/3,
&:amqp_auth_mechanisms.amqplain/3]),
client_properties: Keyword.get(options, :client_properties, []),
socket_options: Keyword.get(options, :socket_options, []))
|> do_open
end
def open(uri) when is_binary(uri) do
case String.to_char_list(uri) |> :amqp_uri.parse do
{:ok, amqp_params} -> do_open(amqp_params)
error -> error
end
end
@doc """
Closes an open Connection.
"""
def close(conn), do: :amqp_connection.close(conn.pid)
defp do_open(amqp_params) when is_record(amqp_params) do
case :amqp_connection.start(amqp_params) do
{:ok, pid} -> {:ok, %Connection{pid: pid}}
error -> error
end
end
defp normalize_host(host) do
cond do
is_binary(host) -> String.to_char_list(host)
true -> host
end
end
end