diff --git a/README.md b/README.md index 1492e32..3e17097 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 @@ -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 diff --git a/examples/processor.ex b/examples/processor.ex new file mode 100644 index 0000000..0bd9d8f --- /dev/null +++ b/examples/processor.ex @@ -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 diff --git a/lib/processor.ex b/lib/processor.ex index 4e7614b..f40a774 100644 --- a/lib/processor.ex +++ b/lib/processor.ex @@ -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