Skip to content
This repository has been archived by the owner on Nov 27, 2023. It is now read-only.

Commit

Permalink
Add example and more detailed description for GenRMQ.Processor (#146)
Browse files Browse the repository at this point in the history
  • Loading branch information
Joel C authored Mar 9, 2020
1 parent a6fe01f commit 317c475
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ The project currently provides the following functionality:

- `GenRMQ.Consumer` - a behaviour for implementing RabbitMQ consumers
- `GenRMQ.Publisher` - a behaviour for implementing RabbitMQ publishers
- `GenRMQ.Processor` - a behaviour for implementing RabbitMQ message processors
- `GenRMQ.Processor` - a behaviour for implementing RabbitMQ message processors, this is useful to separate out business logic from your consumer
- `GenRMQ.RabbitCase` - test utilities for RabbitMQ ([example usage][rabbit_case_example])

## Installation
Expand All @@ -35,7 +35,7 @@ Please check [how to migrate to gen_rmq `1.0.0`][migrating_to_100] from previous

## Examples

More thorough examples for using `GenRMQ.Consumer` and `GenRMQ.Publisher` can be found in the [examples][examples] directory.
More thorough examples for using `GenRMQ.Consumer`, `GenRMQ.Publisher`, and `GenRMQ.Processor` can be found in the [examples][examples] directory.

### Consumer

Expand Down Expand Up @@ -197,7 +197,7 @@ Are you using `gen_rmq` in production? Please let us know, we are curious!

The MIT License (MIT)

Copyright (c) 2018 - 2019 Meltwater Inc. [http://underthehood.meltwater.com/][underthehood]
Copyright (c) 2018 - 2020 Meltwater Inc. [http://underthehood.meltwater.com/][underthehood]

[behaviours]: https://hexdocs.pm/elixir/behaviours.html
[amqp]: https://github.com/pma/amqp
Expand Down
24 changes: 24 additions & 0 deletions examples/processor.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
defmodule ExampleProcessor do
@moduledoc """
Example GenRMQ.Processor implementation
Sample usage:
```
iex -S mix
iex(1)> ExampleProcessor.process(%GenRMQ.Message{payload: "Hello", attributes: [], state: %{}})
Received message: %GenRMQ.Message{attributes: [], payload: "Hello", state: %{}}
iex(2)> ExampleProcessor.process(%GenRMQ.Message{payload: "error", attributes: [], state: %{}})
** (RuntimeError) Exception triggered by message
(gen_rmq) examples/processor.ex:18: ExampleProcessor.process/1
```
"""

@behaviour GenRMQ.Processor

def process(%GenRMQ.Message{payload: "error"}), do: raise("Exception triggered by message")

def process(%GenRMQ.Message{} = message) do
IO.puts("Received message: #{inspect(message)}")
:ok
end
end
6 changes: 5 additions & 1 deletion lib/processor.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
defmodule GenRMQ.Processor do
@moduledoc """
Defines functions to implement by any AMQP processor
Behaviour module for implementing a RabbitMQ message processor.
A message processor is typically used to separate out business logic from a consumer,
to let the consumer only deal with RabbitMQ specifics.
"""

@callback process(message :: %GenRMQ.Message{}) :: :ok | {:error, term}
end

0 comments on commit 317c475

Please sign in to comment.