Class: RubyRabbitmqJanus::Rabbit::Connect

Inherits:
Object
  • Object
show all
Defined in:
lib/rrj/rabbit/connect.rb

Overview

Class for manage connection with RabbitMQ

Author:

Instance Method Summary collapse

Constructor Details

#initializeConnect

Initialize connection to server RabbitMQ



12
13
14
# File 'lib/rrj/rabbit/connect.rb', line 12

def initialize
  @rabbit = Bunny.new(bunny_conf)
end

Instance Method Details

#channelObject

Create an channel



59
60
61
# File 'lib/rrj/rabbit/connect.rb', line 59

def channel
  @rabbit.create_channel
end

#closeObject

Close connection to server RabbitMQ



54
55
56
# File 'lib/rrj/rabbit/connect.rb', line 54

def close
  @rabbit.close
end

#startObject

Opening a connection with RabbitMQ



49
50
51
# File 'lib/rrj/rabbit/connect.rb', line 49

def start
  @rabbit.start
end

#transaction_longObject

Create an transaction with rabbitmq and not close



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rrj/rabbit/connect.rb', line 35

def transaction_long
  raise RubyRabbitmqJanus::Errors::Rabbit::Connect::MissingAction \
    unless block_given?

  Timeout.timeout(60) do
    start
    yield
  end
rescue Timeout::Error
  raise RubyRabbitmqJanus::Errors::Rabbit::Connect::TransactionTimeout, \
        'The "Long transaction" have raised Timeout exception.'
end

#transaction_short(&block) ⇒ Object

Create an transaction with rabbitmq and close after response is received



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rrj/rabbit/connect.rb', line 17

def transaction_short(&block)
  raise RubyRabbitmqJanus::Errors::Rabbit::Connect::MissingAction \
    unless block

  response = nil

  Timeout.timeout(5) do
    response = transaction_long(&block)
  end
rescue Timeout::Error
  raise RubyRabbitmqJanus::Errors::Rabbit::Connect::TransactionTimeout, \
        'The "Short transaction" have raised Timeout exception.'
ensure
  close
  response
end