Sha256: 48ac49820f52b3e4f967a26b7c2ca67ab358569dde1cfd57a90d7fc78647199c

Contents?: true

Size: 1.58 KB

Versions: 53

Compression:

Stored size: 1.58 KB

Contents

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

require "rubygems"
require "amqp"

class Consumer

  #
  # API
  #

  def initialize(channel, queue_name = AMQ::Protocol::EMPTY_STRING)
    @queue_name = queue_name

    @channel    = channel
    @channel.on_error(&method(:handle_channel_exception))
  end # initialize

  def start
    @queue = @channel.queue(@queue_name, :exclusive => true)
    @queue.subscribe(&method(:handle_message))
  end # start



  #
  # Implementation
  #

  def handle_message(metadata, payload)
    puts "Received a message: #{payload}, content_type = #{metadata.content_type}"
  end # handle_message(metadata, payload)

  def handle_channel_exception(channel, channel_close)
    puts "Oops... a channel-level exception: code = #{channel_close.reply_code}, message = #{channel_close.reply_text}"
  end # handle_channel_exception(channel, channel_close)
end


class Producer

  #
  # API
  #

  def initialize(channel, exchange)
    @channel  = channel
    @exchange = exchange
  end # initialize(channel, exchange)

  def publish(message, options = {})
    @exchange.publish(message, options)
  end # publish(message, options = {})
end


AMQP.start("amqp://guest:guest@dev.rabbitmq.com") do |connection, open_ok|
  channel  = AMQP::Channel.new(connection)
  worker   = Consumer.new(channel, "amqpgem.objects.integration")
  worker.start

  producer = Producer.new(channel, channel.default_exchange)
  puts "Publishing..."
  producer.publish("Hello, world", :routing_key => "amqpgem.objects.integration")

  # stop in 2 seconds
  EventMachine.add_timer(2.0) { connection.close { EventMachine.stop } }
end

Version data entries

53 entries across 53 versions & 1 rubygems

Version Path
amqp-1.1.2 examples/guides/queues/12_objects_that_consume_messages.rb
amqp-1.1.1 examples/guides/queues/12_objects_that_consume_messages.rb
amqp-1.1.0 examples/guides/queues/12_objects_that_consume_messages.rb
amqp-1.0.3 examples/guides/queues/12_objects_that_consume_messages.rb
amqp-1.1.0.rc1 examples/guides/queues/12_objects_that_consume_messages.rb
amqp-1.1.0.pre2 examples/guides/queues/12_objects_that_consume_messages.rb
amqp-1.1.0.pre1 examples/guides/queues/12_objects_that_consume_messages.rb
amqp-1.0.2 examples/guides/queues/12_objects_that_consume_messages.rb
amqp-1.0.1 examples/guides/queues/12_objects_that_consume_messages.rb
amqp-1.0.0 examples/guides/queues/12_objects_that_consume_messages.rb
amqp-0.9.10 examples/guides/queues/12_objects_that_consume_messages.rb
amqp-0.9.9 examples/guides/queues/12_objects_that_consume_messages.rb
amqp-0.9.8 examples/guides/queues/12_objects_that_consume_messages.rb
amqp-1.0.0.pre2 examples/guides/queues/12_objects_that_consume_messages.rb
amqp-0.9.7 examples/guides/queues/12_objects_that_consume_messages.rb
amqp-0.9.6 examples/guides/queues/12_objects_that_consume_messages.rb
amqp-1.0.0.pre1 examples/guides/queues/12_objects_that_consume_messages.rb
amqp-0.9.5 examples/guides/queues/12_objects_that_consume_messages.rb
amqp-0.9.4 examples/guides/queues/12_objects_that_consume_messages.rb
amqp-0.9.3 examples/guides/queues/12_objects_that_consume_messages.rb