Sha256: 91ad579a27c1cdadf4dedd7f18a525e54e1fe16bbc3093b37df080887c3bd2c0

Contents?: true

Size: 1.65 KB

Versions: 2

Compression:

Stored size: 1.65 KB

Contents

# simple_consumer_09.rb

# N.B. To be used in conjunction with simple_publisher.rb

# Assumes that target message broker/server has a user called 'guest' with a password 'guest'
# and that it is running on 'localhost'.

# If this is not the case, please change the 'Bunny.new' call below to include
# the relevant arguments e.g. b = Bunny.new(:user => 'john', :pass => 'doe', :host => 'foobar')

# How this example works
#=======================
#
# Open up two console windows start this program in one of them by typing -
#
# ruby simple_consumer_09.rb
#
# Then switch to the other console window and type -
#
# ruby simple_publisher_09.rb
#
# A message will be printed out by the simple_consumer and it will wait for the next message
# until the timeout interval is reached.
#
# Run simple_publisher as many times as you like. When you want the program to stop just stop
# sending messages and the subscribe loop will timeout after 30 seconds, the program will
# unsubscribe from the queue and close the connection to the server.

$:.unshift File.dirname(__FILE__) + '/../lib'

require 'bunny'

b = Bunny.new(:logging => true, :spec => '09')

# start a communication session with the amqp server
b.start

# create/get queue
q = b.queue('po_box')

# create/get exchange
exch = b.exchange('sorting_room')

# bind queue to exchange
q.bind(exch, :key => 'fred')

# initialize counter
i = 1

# subscribe to queue
ret = q.subscribe(:consumer_tag => 'testtag1', :timeout => 30) do |msg|
	puts "#{i.to_s}: #{msg}"
	i+=1
end

if ret == :timed_out
	puts '==== simple_consumer_09.rb timed out - closing down ===='
	q.unsubscribe(:consumer_tag => 'testtag1')
	# close the connection
	b.stop
end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
celldee-bunny-0.5.2 examples/simple_consumer_09.rb
bunny-0.5.2 examples/simple_consumer_09.rb