Sha256: 3606b2c1219bb4183215bc80d32983f52b6059c058b5cb8811635bd108aadcb1

Contents?: true

Size: 1.77 KB

Versions: 33

Compression:

Stored size: 1.77 KB

Contents

#!/usr/bin/env ruby
# encoding: utf-8

require "bundler"
Bundler.setup

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

require 'amqp'


puts "=> Auxiliary script that tests automatically recovering message consumer(s)"
puts
AMQP.start(:host => ENV.fetch("BROKER_HOST", "localhost")) do |connection, open_ok|
  puts "Connected to #{connection.hostname}"
  connection.on_error do |ch, connection_close|
    raise connection_close.reply_text
  end

  connection.on_tcp_connection_loss do |conn, settings|
    puts "[network failure] Trying to reconnect..."
    conn.reconnect(false, 2)
  end


  ch1 = AMQP::Channel.new(connection, :auto_recovery => true)
  ch1.on_error do |ch, channel_close|
    raise channel_close.reply_text
  end


  exchange = ch1.fanout("amq.fanout", :durable => true)
  EventMachine.add_periodic_timer(0.9) do
    puts "Publishing via default exchange..."
    # messages must be routable & there must be at least one consumer.
    ch1.default_exchange.publish("Routed via default_exchange", :routing_key => "amqpgem.examples.autorecovery.queue")
  end

  EventMachine.add_periodic_timer(0.8) do
    puts "Publishing via amq.fanout..."
    # messages must be routable & there must be at least one consumer.
    exchange.publish("Routed via amq.fanout", :mandatory => true)
  end


  show_stopper = Proc.new {
    connection.disconnect { puts "Disconnected. Exiting…"; EventMachine.stop }
  }

  Signal.trap "TERM", show_stopper
  Signal.trap "INT",  show_stopper
  EM.add_timer(ENV.fetch("TIMER", 15), show_stopper)

  puts "This example a helper that publishes messages to amq.fanout. Use together with examples/error_handling/automatically_recovering_hello_world_consumer.rb."
  puts "This example terminates in 15 seconds and needs MANUAL RESTART when connection fails"
end

Version data entries

33 entries across 33 versions & 1 rubygems

Version Path
amqp-1.8.0 examples/error_handling/hello_world_producer.rb
amqp-1.7.0 examples/error_handling/hello_world_producer.rb
amqp-1.6.0 examples/error_handling/hello_world_producer.rb
amqp-1.5.3 examples/error_handling/hello_world_producer.rb
amqp-1.5.2 examples/error_handling/hello_world_producer.rb
amqp-1.5.1 examples/error_handling/hello_world_producer.rb
amqp-1.5.0 examples/error_handling/hello_world_producer.rb
amqp-1.4.2 examples/error_handling/hello_world_producer.rb
amqp-1.4.1 examples/error_handling/hello_world_producer.rb
amqp-1.4.0 examples/error_handling/hello_world_producer.rb
amqp-1.3.0 examples/error_handling/hello_world_producer.rb
amqp-1.2.1 examples/error_handling/hello_world_producer.rb
amqp-1.2.0 examples/error_handling/hello_world_producer.rb
amqp-1.1.8 examples/error_handling/hello_world_producer.rb
amqp-1.1.7 examples/error_handling/hello_world_producer.rb
amqp-1.1.6 examples/error_handling/hello_world_producer.rb
amqp-1.1.5 examples/error_handling/hello_world_producer.rb
amqp-1.1.4 examples/error_handling/hello_world_producer.rb
amqp-1.1.3 examples/error_handling/hello_world_producer.rb
amqp-1.0.4 examples/error_handling/hello_world_producer.rb