Top Level Namespace

Defined Under Namespace

Modules: RabbitHutch Classes: Logger

Instance Method Summary collapse

Instance Method Details

#initialize_consumers(rabbitmq_host) ⇒ Object

Initialize all enabled consumners NOTE: this method will be replaced with a routine to reflect through all valid consumers and initialze them implicitly



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rabbithutch.rb', line 18

def initialize_consumers(rabbitmq_host)
  puts "Initializing Consumers for #{rabbitmq_host["displayname"]}"
  consumers = []
  @config.consumers.each do |consumer|
    if consumer["enabled"] == true
      case consumer["name"]
        when "console_consumer"
          consumers << RabbitHutch::ConsoleConsumer.new()
        when "mongo_consumer"
          consumers << RabbitHutch::MongoConsumer.new(rabbitmq_host, @config)
        when "log4r_consumer"
          consumers << RabbitHutch::Log4rConsumer.new(rabbitmq_host, @config)
        end 
      end
  end
  puts "\tdone"
  consumers 
end

#startObject

Entry Point to the application, Begins queue listener and initializes all consmers



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/rabbithutch.rb', line 55

def start
  begin
    EventMachine.run do
      
      @config.rabbitmq_hosts.each do |rabbitmq_host|
        if rabbitmq_host["enabled"] == true
          start_worker(rabbitmq_host)
        end
      end
      Signal.trap("INT"){EventMachine.stop}
      Signal.trap("QUIT"){EventMachine.stop}
      Signal.trap("TERM"){EventMachine.stop}
      Signal.trap("TSTP"){EventMachine.stop}
    end
  rescue Exception=>e
    puts "#{e.message} #{e.backtrace}"
  end
end

#start_worker(rabbitmq_host) ⇒ Object

Start the worker process to listen to a RabbitMq Node



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rabbithutch.rb', line 38

def start_worker(rabbitmq_host)
  displayname = rabbitmq_host["displayname"]
  hostname = rabbitmq_host["hostname"]
  username = rabbitmq_host["username"]
  password = rabbitmq_host["password"]
  
  consumers = initialize_consumers(rabbitmq_host)
     
  puts "Listening to RabbitMq #{displayname}, host: #{hostname}, username #{username}"
  AMQP.connect(:host => hostname, :user => username, :password => password) do |connection|
    channel = AMQP::Channel.new(connection)
    worker = RabbitHutch::Worker.new(channel, @config, consumers)
    worker.start
  end
end