Class: MessageQueue::Adapters::Bunny::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/message_queue/adapters/bunny/connection.rb

Defined Under Namespace

Classes: Consumer, Publisher

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(serializer, settings) ⇒ 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.



12
13
14
15
# File 'lib/message_queue/adapters/bunny/connection.rb', line 12

def initialize(serializer, settings)
  @serializer = serializer
  @settings = settings
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



2
3
4
# File 'lib/message_queue/adapters/bunny/connection.rb', line 2

def connection
  @connection
end

#serializerObject (readonly)

Returns the value of attribute serializer.



2
3
4
# File 'lib/message_queue/adapters/bunny/connection.rb', line 2

def serializer
  @serializer
end

#settingsObject (readonly)

Returns the value of attribute settings.



2
3
4
# File 'lib/message_queue/adapters/bunny/connection.rb', line 2

def settings
  @settings
end

Instance Method Details

#connectObject

Public: Connect to RabbitMQ

Returns the Bunny instance



20
21
22
23
24
25
26
# File 'lib/message_queue/adapters/bunny/connection.rb', line 20

def connect
  @connection ||= begin
                    bunny = ::Bunny.new(settings)
                    bunny.start
                    bunny
                  end
end

#disconnectObject

Public: Disconnect from RabbitMQ

Returns nothing



31
32
33
34
35
36
# File 'lib/message_queue/adapters/bunny/connection.rb', line 31

def disconnect
  if @connection
    @connection.close if @connection.open?
    @connection = nil
  end
end

#new_consumer(options) ⇒ Object



56
57
58
59
60
# File 'lib/message_queue/adapters/bunny/connection.rb', line 56

def new_consumer(options)
  raise "No connection to RabbitMQ" unless connection

  Consumer.new(self, options)
end

#new_publisher(options) ⇒ Object



50
51
52
53
54
# File 'lib/message_queue/adapters/bunny/connection.rb', line 50

def new_publisher(options)
  raise "No connection to RabbitMQ" unless connection

  Publisher.new(self, options)
end

#with_connection(&block) ⇒ Object

Public: Connect to RabbitMQ, execute the block and disconnect

Returns nothing



41
42
43
44
45
46
47
48
# File 'lib/message_queue/adapters/bunny/connection.rb', line 41

def with_connection(&block)
  begin
    connect
    block.call(self)
  ensure
    disconnect
  end
end