Class: RabbitMQDispatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/postcard_rb/dispatchers/RabbitMQ/RabbitMQDispatcher.rb

Instance Method Summary collapse

Constructor Details

#initialize(host:) ⇒ RabbitMQDispatcher

Returns a new instance of RabbitMQDispatcher.



10
11
12
13
14
# File 'lib/postcard_rb/dispatchers/RabbitMQ/RabbitMQDispatcher.rb', line 10

def initialize host:
  @connection = Bunny.new(host: host)
  @channel = nil 
  @topics = []
end

Instance Method Details

#connect(connectionInterval:, connectionRetries:) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/postcard_rb/dispatchers/RabbitMQ/RabbitMQDispatcher.rb', line 21

def connect connectionInterval:, connectionRetries: 
  begin  
    @connection.start

    createChannel()
  rescue Bunny::TCPConnectionFailedForAllHosts
    sleep connectionInterval
    connectionRetries -= 1

    raise DispatcherConnectionRefused if connectionRetries == 0

    connect(
      connectionInterval: connectionInterval, 
      connectionRetries: connectionRetries
    )
  end
end

#createChannelObject



16
17
18
19
# File 'lib/postcard_rb/dispatchers/RabbitMQ/RabbitMQDispatcher.rb', line 16

def createChannel
  @channel = @connection.create_channel
  @channel.prefetch(1)
end

#createTopic(name:, routing:) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/postcard_rb/dispatchers/RabbitMQ/RabbitMQDispatcher.rb', line 39

def createTopic name:, routing:
  topic = nil

  case routing
  when Routing.Wide
    topic = WideRabbitMQTopic.new(name: name, channel: @channel)
  when Routing.Explicit
    topic = ExplicitRabbitMQTopic.new(name: name, channel: @channel)
  when Routing.PatternMatching
    topic = PatternMatchingRabbitMQTopic.new(name: name, channel: @channel)
  end

  @topics.push(topic)

  return topic
end