class Messengerdef self.publish(key, value)instance.publish(key, value)enddef self.instance@instance ||= beginconn = Bunny.newconn.startch = conn.create_channelex = ch.topic('topics', auto_delete: true)new exendendattr_reader :exchangedef initialize(exchange)@exchange = exchangeenddef publish(key, value)exchange.publish(value, routing_key: key)end
q.subscribe(:block => true, :ack => true) do |delivery_info, properties, payload|
puts "Received #{payload}, message properties are #{properties.inspect}"
endSo for publisher, as you mentioned "don't share channels between threads if you can", what would be the "right" approach? Do you have an example on how to do that in Rails?
Should I put mutex around the channel/exchange object?
Ruby has thread local though. You mean something like this (http://en.wikipedia.org/wiki/Thread-local_storage#Ruby)?Thread.current[:channel] = bunny_channel
You keep mentioning channel here. Should I store channel or exchange as a thread local?
It might be possible to store the channel as a rack env variable and that's used per request as I understand
--
--
Documentation guides: http://bit.ly/rubyamqp
Code examples: http://bit.ly/amq-gem-examples
API reference: http://bit.ly/mDm1JE
Drop by #rabbitmq on irc.freenode.net
Bug tracker: https://github.com/ruby-amqp/amqp/issues
Post to the group: ruby...@googlegroups.com | unsubscribe: ruby-amqp+...@googlegroups.com
---
You received this message because you are subscribed to a topic in the Google Groups "Ruby AMQP ecosystem" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ruby-amqp/aqs1t5BOdQI/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to ruby-amqp+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Do you think it makes sense to have a pool of channels shared across requests? Or maybe create a channel for the request and close it immediately after the request is done
It doesn't look right to me though - creating a bunny instance on every request
How about channels? Would you recommend to create one per request and close it after it's done? If I don't close the channel, won't it have memory leak since it caches in a local variable @channels (https://github.com/ruby-amqp/bunny/blob/master/lib/bunny/session.rb#L178)?
Then, for each request I need to create a channel, declare an exchange and do the publish and other stuff, and no worry about the thread stuff?
Or should I even put the $b.start inside of each request and stop it every time?