jeudi 13 septembre 2018

Common mistakes in RabbitMQ

I have two applications S-APP1 and S-APP2 and just started to use rabbitMQ with client bunny gem, so when i click(this could be more click in a minute) a button from S-APP1 then i will call publisher.rb that will publish the message into queue and read from S-APP2 - consumer.rb and put it in sidekiq for processing.

Today i just saw this link https://www.cloudamqp.com/blog/2018-01-19-part4-rabbitmq-13-common-errors.html and read this, bit confused on these lines from that link "reuse connections" and "Don’t use too many connections or channels", am i doing correctly this thing? I am using heroku to run this and using CloudAMQP addon.

publisher.rb (S-APP1)

def publish
    connection = Bunny.new( :host => ENV["AMQP_HOST"],
                            :vhost => ENV["AMQP_VHOST"],
                            :user => ENV["AMQP_USER"],
                            :password => ENV["AMQP_PASSWORD"])
    connection.start # start a communication session with the amqp server

    channel = connection.channel()

    channel.queue('order-queue', durable: true)

    exchange = channel.default_exchange

    message = { order_id: '1542' }

    # publish a message to the queue
    exchange.publish(message.to_json, routing_key: 'order-queue')

    puts " [x] Sent #{message}"
    connection.close()
end

This is my consumer part

consumer.rb (S-APP2)

connection = Bunny.new( :host => ENV["AMQP_HOST"],
                        :vhost => ENV["AMQP_VHOST"],
                        :user => ENV["AMQP_USER"],
                        :password => ENV["AMQP_PASSWORD"])

connection.start # start a communication session with the amqp server

channel = connection.channel()

queue = channel.queue('order-queue', durable: true)

puts ' [*] Waiting for messages. To exit press CTRL+C'

queue.subscribe(manual_ack: true, block: true) do |delivery_info, properties, payload|
  puts " [x] Received #{payload}"
  puts " [x] Done"

  channel.ack(delivery_info.delivery_tag, false)
  # Call worker to do refresh
  callSidekiqWorker.perform_async(payload)
end

Aucun commentaire:

Enregistrer un commentaire