lib/message_queue/adapters/bunny/connection.rb in message_queue-0.0.4 vs lib/message_queue/adapters/bunny/connection.rb in message_queue-0.1.0

- old
+ new

@@ -1,64 +1,62 @@ -class MessageQueue::Adapters::Bunny::Connection - attr_reader :serializer, :settings, :connection +class MessageQueue::Adapters::Bunny::Connection < MessageQueue::Connection + attr_reader :connection - # Public: Initialize a new Bunny connection. - # - # serializer - The Serializer for dumping and loading payload. - # - # settings - The Hash settings used to connect with Bunny. - # Details in http://rubybunny.info/articles/connecting.html. - # - # Returns a Connection wrapper for Bunny. - def initialize(serializer, settings) - @serializer = serializer - @settings = settings - end - # Public: Connect to RabbitMQ # # Returns the Bunny instance def connect @connection ||= begin - bunny = ::Bunny.new(settings) + super + bunny = ::Bunny.new(bunny_settings) bunny.start bunny end end # Public: Disconnect from RabbitMQ # # Returns nothing def disconnect if @connection + super @connection.close if @connection.open? @connection = nil end end - # Public: Connect to RabbitMQ, execute the block and disconnect + # Public: Check if it's connected to the message queue # - # Returns nothing - def with_connection(&block) - begin - connect - block.call(self) - ensure - disconnect - end + # Returns true if it's connected + def connected? + @connection.open? if @connection end - def new_publisher(options) + def new_producer(options) raise "No connection to RabbitMQ" unless connection - Publisher.new(self, options) + Producer.new(self, options) end def new_consumer(options) raise "No connection to RabbitMQ" unless connection Consumer.new(self, options) end + + private + + def bunny_settings + default_bunny_settings.merge(settings) + end + + def default_bunny_settings + { + :heartbeat => 1, + :automatically_recover => true, + :network_recovery_interval => 1 + } + end end -require "message_queue/adapters/bunny/publisher" +require "message_queue/adapters/bunny/producer" require "message_queue/adapters/bunny/consumer"