[bunny-amqp-devel] using bunny with rails to send messages

152 views
Skip to first unread message

vishnu

unread,
Jan 18, 2010, 11:04:37 PM1/18/10
to bunny-am...@rubyforge.org
Hi, 
    I was wondering if anyone on the list uses bunny with rails. Is there anything people do to keep the connection persistent across requests? By moving this code into the rails startup maybe? 

Also has anyone tried sharing the connection across multiple rails processes (does that even make sense?), or pooling the connections?
thanks

Mark Westling

unread,
Jan 18, 2010, 11:23:24 PM1/18/10
to vishnu, bunny-am...@rubyforge.org
I'm using Bunny inside a Rails 2.3 application that runs on Passenger.  I keep the connection in thread-local variables, like this:

    begin
      b = (Thread.current[:bunny] ||= Bunny.new(:host => AMQ_HOST))
      b.start unless b.status == :connected
      q = (Thread.current[queue] ||= b.queue(QUEUE_NAME))
      q.publish(message)
    rescue Exception => e
      Thread.current[:bunny] = nil   # Force a reconnection
      retry if (retries -= 1) >= 0
    end

The first time this section of code runs it opens the connection and stores it for reuse.  The thread-local variables prevent multiple threads from running into each other.  This approach is working fine (but I'm open to suggestions!).

--Mark


_______________________________________________
bunny-amqp-devel mailing list
bunny-am...@rubyforge.org
http://rubyforge.org/mailman/listinfo/bunny-amqp-devel


vishnu

unread,
Jan 20, 2010, 10:55:26 AM1/20/10
to bunny-am...@rubyforge.org
thanks mark :). Let me look at that option
Reply all
Reply to author
Forward
0 new messages