README.md in ears-0.12.0 vs README.md in ears-0.13.0

- old
+ new

@@ -37,14 +37,46 @@ end ``` _Note_: `connection_name` is a mandatory setting! -Next, define your exchanges, queues, and consumers by calling `Ears.setup`. +Next, you can define your exchanges, queues, and consumers in 2 ways: +#### 1. consumer specific configuration method (recommended) + +1. Pass your consumer classes to `Ears.setup`: + ```ruby Ears.setup do + Ears.setup_consumers(Consumer1, Consumer2, ...) +end +``` + +2. Implement your consumers by subclassing `Ears::Consumer`. and call the configure method. + +```ruby +class Consumer1 < Ears::Consumer + configure( + queue: 'queue_name', + exchange: 'exchange', + routing_keys: %w[routing_key1 routing_key2], + retry_queue: true, # optional configuration, defaults to false, Adds a retry queue + error_queue: true, # optional configuration, defaults to false, Adds an error queue + ) + def work(delivery_info, metadata, payload) + message = JSON.parse(payload) + do_stuff(message) + + ack + end +end +``` + +#### 2. Generic configuration method + +```ruby +Ears.setup do # define a durable topic exchange my_exchange = exchange('my_exchange', :topic, durable: true) # define a queue my_queue = queue('my_queue', durable: true) @@ -55,11 +87,11 @@ # define a consumer that handles messages for that queue consumer(my_queue, MyConsumer) end ``` -Finally, you need to implement `MyConsumer` by subclassing `Ears::Consumer`. +Finally, you need to implement `MyConsumer` by subclassing `Ears::Consumer`. and call the configure method. ```ruby class MyConsumer < Ears::Consumer def work(delivery_info, metadata, payload) message = JSON.parse(payload) @@ -68,12 +100,14 @@ ack end end ``` -And, do not forget to run it. Be prepared that unhandled errors will be reraised. So, take care of cleanup work. +### Run your consumers +Note: Be prepared that unhandled errors will be reraised. So, take care of cleanup work. + ```ruby begin Ears.run! ensure # all your cleanup work goes here... @@ -163,23 +197,57 @@ end ``` ### Implementing a retrying queue -Sometimes you want to automatically retry processing a message, in case it just failed due to temporary problems. In that case, you can set the `retry_queue` and `retry_delay` parameters when creating the queue. +Sometimes you want to automatically retry processing a message, in case it just failed due to temporary problems. In that case, you can set the `retry_queue` and `retry_delay` parameters when creating the queue OR pass it to the configure method in your consumer. ```ruby +class MyConsumer < Ears::Consumer + configure( + queue: 'queue_name', + exchange: 'exchange', + routing_keys: %w[routing_key1 routing_key2], + retry_queue: true, + ) + def work(delivery_info, metadata, payload) + message = JSON.parse(payload) + do_stuff(message) + + ack + end +end +``` + +```ruby my_queue = queue('my_queue', durable: true, retry_queue: true, retry_delay: 5000) ``` This will automatically create a queue named `my_queue.retry` and use the arguments `x-dead-letter-exchange` and `x-dead-letter-routing-key` to route rejected messages to it. When routed to the retry queue, messages will wait there for the number of milliseconds specified in `retry_delay`, after which they will be redelivered to the original queue. **Note that this will not automatically catch unhandled errors. You still have to catch any errors yourself and reject your message manually for the retry mechanism to work.** This will happen indefinitely, so if you want to bail out of this cycle at some point, it is best to use the `error_queue` option to create an error queue and then use the `MaxRetries` middleware to route messages to this error queue after a certain amount of retries. ### Implementing an error queue -You can set the `error_queue` parameter to automatically create an error queue. +You can set the `error_queue` parameter to automatically create an error queue, or add it to the configure method in your consumer. + +```ruby +class MyConsumer < Ears::Consumer + configure( + queue: 'queue_name', + exchange: 'exchange', + routing_keys: %w[routing_key1 routing_key2], + error_queue: true, + ) + def work(delivery_info, metadata, payload) + message = JSON.parse(payload) + do_stuff(message) + + ack + end +end +``` ```ruby my_queue = queue( 'my_queue',