-
Notifications
You must be signed in to change notification settings - Fork 0
/
reciever.rb
47 lines (37 loc) · 1.24 KB
/
reciever.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
require "bunny"
class Reciever
def initialize queue_name, exchange_name
@queue_name = queue_name
@exchange_name = exchange_name
end
def start
# then connect to RabbitMQ server
# default is localhost
@conn = Bunny.new
@conn.start
# Next we create a channel, which is where most of the API for getting things done resides:
@conn.with_channel do |channel|
# pedimos para criar a fila @queue_name automaticamente
# de modo que não precisamos explicitamente cria-la no rabbitmq
queue = channel.queue(@queue_name)
# pedimos para que o exchange mande mensagens para a queue
# de modo que não precisamos explicitamente fazer o 'bind' no rabbitmq
queue.bind(@exchange_name)
begin
p "Listening to messages in #{queue.name}"
queue.subscribe(:block => true) do |delivery_info, properties, body|
self.proccess_message delivery_info, properties, body
end
rescue Interrupt => _
@conn.close
self.after_proccess
end
end
end
def proccess_message delivery_info, properties, body
fail NotImplementedError, "A Reciever class must be able to #proccess_message!"
end
def after_proccess
# we will do nothing
end
end