Skip to content

Commit

Permalink
Update to v0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
nsweeting committed Jun 13, 2019
1 parent 173ba89 commit bd9eabf
Show file tree
Hide file tree
Showing 18 changed files with 640 additions and 546 deletions.
79 changes: 53 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ The package can be installed by adding `rabbit` to your list of dependencies in
```elixir
def deps do
[
{:rabbit, "~> 0.2"}
{:rabbit, "~> 0.3"}
]
end
```
Expand All @@ -29,10 +29,15 @@ Create a connection module:
defmodule MyConnection do
use Rabbit.Connection

def start_link(opts \\ []) do
Rabbit.Connection.start_link(__MODULE__, opts, name: __MODULE__)
end

# Callbacks

# Perform runtime config
def init(opts) do
@impl Rabbit.Connection
def init(_type, opts) do
# Perform runtime config
uri = System.get_env("RABBITMQ_URI") || "amqp://guest:guest@127.0.0.1:5672"
opts = Keyword.put(opts, :uri, uri)

Expand All @@ -51,74 +56,92 @@ Create a consumer module:
defmodule MyConsumer do
use Rabbit.Consumer

def start_link(opts \\ []) do
Rabbit.Consumer.start_link(__MODULE__, opts, name: __MODULE__)
end

# Callbacks

# Perform runtime config
def init(opts) do
@impl Rabbit.Consumer
def init(_type, opts) do
# Perform runtime config
{:ok, opts}
end

# Perform any exchange or queue setup
def after_connect(channel, queue) do
@impl Rabbit.Consumer
def handle_setup(channel, queue) do
# Perform any exchange or queue setup
AMQP.Queue.declare(channel, queue)
:ok
end

# Handle message consumption
@impl Rabbit.Consumer
def handle_message(message) do
# Handle message consumption
IO.inspect(message.payload)
{:ack, message}
end

# Handle message errors
@impl Rabbit.Consumer
def handle_error(message) do
# Handle message errors
{:nack, message}
end
end

MyConsumer.start_link(MyConnection, queue: "my_queue", prefetch_count: 10)
MyConsumer.start_link(connection: MyConnection, queue: "my_queue", prefetch_count: 10)
```

Or, create a consumer supervisor:

```elixir
defmodule MyConsumers do
defmodule MyConsumerSupervisor do
use Rabbit.ConsumerSupervisor

def start_link(consumers \\ []) do
Rabbit.ConsumerSupervisor.start_link(__MODULE__, consumers, name: __MODULE__)
end

# Callbacks

# Specifiy a list of consumers under the supervisor
def consumers do
[
[queue: "my_queue", prefetch_count: 10],
[queue: "my_other_queue", prefetch_count: 10]
@impl Rabbit.ConsumerSupervisor
def init(:consumer_supervisor, _consumers) do
# Perform runtime config for the consumer supervisor
consumers = [
[connection: MyConnection, queue: "my_queue1", prefetch_count: 5],
[connection: MyConnection, queue: "my_queue2", prefetch_count: 10],
]

{:ok, consumers}
end

# Perform runtime config per consumer
def init(opts) do
def init(:consumer, opts) do
# Perform runtime config per consumer
{:ok, opts}
end

# Perform any exchange or queue setup per consumer
def after_connect(channel, queue) do
@impl Rabbit.ConsumerSupervisor
def handle_setup(channel, queue) do
# Perform any exchange or queue setup per consumer
AMQP.Queue.declare(channel, queue)
:ok
end

# Handle message consumption per consumer
@impl Rabbit.ConsumerSupervisor
def handle_message(message) do
# Handle message consumption per consumer
IO.inspect(message.payload)
{:ack, message}
end

# Handle message errors per consumer
@impl Rabbit.ConsumerSupervisor
def handle_error(message) do
# Handle message errors per consumer
{:nack, message}
end
end

MyConsumers.start_link(MyConnection)
MyConsumerSupervisor.start_link()
```

## Producers
Expand All @@ -129,14 +152,18 @@ Create a producer module:
defmodule MyProducer do
use Rabbit.Producer

def start_link(opts \\ []) do
Rabbit.Producer.start_link(__MODULE__, opts, name: __MODULE__)
end

# Callbacks

# Perform runtime config
def init(opts) do
def init(:producer, opts) do
{:ok, opts}
end
end

MyProducer.start_link(MyConnection)
MyProducer.publish("", "my_queue", "hello")
MyProducer.start_link(connection: MyConnection)
Rabbit.Producer.publish(MyProducer, "", "my_queue", "hello")
```
1 change: 1 addition & 0 deletions lib/rabbit/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule Rabbit.Application do

use Application

@doc false
@impl Application
def start(_type, _args) do
children = [
Expand Down
Loading

0 comments on commit bd9eabf

Please sign in to comment.