File: automatic_recovery_with_multiple_consumers.rb

package info (click to toggle)
ruby-bunny 2.23.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,644 kB
  • sloc: ruby: 10,256; sh: 70; makefile: 8
file content (46 lines) | stat: -rw-r--r-- 1,201 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/env ruby
# encoding: utf-8

require "bundler"
Bundler.setup

$:.unshift(File.expand_path("../../../lib", __FILE__))

require 'bunny'

conn = Bunny.new(heartbeat_timeout: 8)
conn.start

ch1 = conn.create_channel
x1  = ch1.topic("bunny.examples.recovery.e1", :durable => false)
q1  = ch1.queue("bunny.examples.recovery.q1", :durable => false)

q1.bind(x1, :routing_key => "abc").bind(x1, :routing_key => "def")

ch2 = conn.create_channel
x2  = ch2.topic("bunny.examples.recovery.e2", :durable => false)
q2  = ch2.queue("bunny.examples.recovery.q2", :durable => false)

q2.bind(x2, :routing_key => "abc").bind(x2, :routing_key => "def")

q1.subscribe do |delivery_info, metadata, payload|
  puts "Consumed #{payload} at stage one"
  x2.publish(payload, :routing_key => ["abc", "def", "xyz"].sample)
end

q2.subscribe do |delivery_info, metadata, payload|
  puts "Consumed #{payload} at stage two"
end

loop do
  sleep 2
  rk = ["abc", "def", "ghi", "xyz"].sample
  puts "Publishing with routing key #{rk}"

  begin
    x1.publish(rand.to_s, :routing_key => rk)
  # happens when a message is published before the connection
  # is recovered
  rescue Bunny::ConnectionClosedError => e
  end
end