Tatva-Artha

meaning of "it"

Producer/Consumer using beanstalkd in Ruby

with 2 comments

Got basic queueing to work in ruby using beanstalkd/beanstalk-client.

Producer.rb

#!/usr/bin/env ruby
 
require 'rubygems'
require 'beanstalk-client'
 
beanstalk = Beanstalk::Pool.new(['localhost:11300'])
while(true) do
	sleep(10)
	puts "posting message ..."
	beanstalk.put(Time.now.to_s)
end

Consumer.rb

#!/usr/bin/env ruby
 
require 'rubygems'
require 'beanstalk-client'
 
beanstalk = Beanstalk::Pool.new(['localhost:11300'])
loop do
  job = beanstalk.reserve
  puts job.body
  job.delete
end

Commands:

$ beanstalkd -d -p 11300
$ ./producer.rb
# consumer in a different terminal
$ ./consumer.rb

Enjoy!

http://www.tatvartha.com/wp-content/plugins/sociofluid/images/digg_16.png http://www.tatvartha.com/wp-content/plugins/sociofluid/images/reddit_16.png http://www.tatvartha.com/wp-content/plugins/sociofluid/images/stumbleupon_16.png http://www.tatvartha.com/wp-content/plugins/sociofluid/images/delicious_16.png http://www.tatvartha.com/wp-content/plugins/sociofluid/images/google_16.png http://www.tatvartha.com/wp-content/plugins/sociofluid/images/twitter_16.png

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

Written by Sharad

July 21st, 2009 at 9:37 pm

Posted in All

2 Responses to 'Producer/Consumer using beanstalkd in Ruby'

Subscribe to comments with RSS or TrackBack to 'Producer/Consumer using beanstalkd in Ruby'.

  1. Have a look at AMQP if you need persistent queues…
    http://github.com/tmm1/amqp/tree/master

    RabbitMQ is also pretty nice for a server as it is written in Erlang.

    Ray Krueger

    21 Jul 09 at 9:44 pm

  2. Thanks Ray, wasn’t aware of this. We are migrating away from ActiveMQ, ’cause it is too heavy weight. In my mind, beanstalkd is lean. AMQP seems similar and flexible incase if we need advanced features (persistence) in future.

    Will take a look. Thanks again.

    Sharad

    23 Jul 09 at 1:03 am

Leave a Reply