Class: LontaraUtilities::RMQ::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/lontara_utilities/rmq/connection.rb

Overview

Initializing the connection to RabbitMQ.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url: ENV['RABBITMQ_URL'], default_exchange: true) ⇒ Connection

**Connection can be initialized within a block.**

If block is given, connection object will be yielded. You can use the object to define the ‘exchange`, `queue`, or `reply queue`.

Example:

def initialize(url, queue, reply_queue)
  Connection.new(url:) do |conn|
    @exchange = conn.channel.default_exchange
    @queue = conn.channel.queue(queue)
    @reply_queue = conn.channel.queue(reply_queue, exclusive: true)
  end
end
# ... your code goes here


22
23
24
25
26
27
28
# File 'lib/lontara_utilities/rmq/connection.rb', line 22

def initialize(url: ENV['RABBITMQ_URL'], default_exchange: true)
  @connection = Bunny.new(url)
  @connection.start

  @channel = channel_pool
  @exchange = channel.default_exchange if default_exchange
end

Instance Attribute Details

#channelObject (readonly)

Returns the value of attribute channel.



34
35
36
# File 'lib/lontara_utilities/rmq/connection.rb', line 34

def channel
  @channel
end

#connectionObject (readonly)

Returns the value of attribute connection.



34
35
36
# File 'lib/lontara_utilities/rmq/connection.rb', line 34

def connection
  @connection
end

#exchangeObject (readonly)

Returns the value of attribute exchange.



34
35
36
# File 'lib/lontara_utilities/rmq/connection.rb', line 34

def exchange
  @exchange
end

Instance Method Details

#closeObject



30
31
32
# File 'lib/lontara_utilities/rmq/connection.rb', line 30

def close
  @connection.close
end