H
H
HoHsi2015-11-04 15:01:37
Node.js
HoHsi, 2015-11-04 15:01:37

Do I need to pass Options to RabbitMQ every time?

Good afternoon! I use the amqp
library for the node. Since there is no sane guide for this thing, I studied PHP / Python versions. Faced with a cardinal misunderstanding of the initialization of the queue and decided to write a wrapper over it all:

# Reqyures
events          = require 'events'
amqp            = require 'amqp'

# Init
EventEmitter = events.EventEmitter

QUEUE_NAME = "test_q"
EXCHANGE_NAME = "test_e"

class AmqpConnection extends EventEmitter
    constructor: ->
        @connection = amqp.createConnection { 
            host: 'localhost'
            port: 5672
            login: 'test'
            password: '123456'
            vhost: '/'
        }

        # @queue = undefined

        @connection.on 'ready', =>
            @connection.exchange EXCHANGE_NAME,
                type:           'topic'
                autoDelete:     false
                durable:        true
                confirm:        true
                deliveryMode:   2
            , (@exchange)=>
                @connection.queue QUEUE_NAME,
                    type:           'topic'
                    autoDelete:     false
                    durable:        true
                    confirm:        true
                    deliveryMode:   2
                , (@queue)=>
                    @queue.bind @exchange, QUEUE_NAME, =>
                        @.emit 'ready', {
                            @connection
                            @exchange
                            @queue
                        }

        @connection.on 'error', (err)=>
            @.emit 'error', err

    sendMessage: (message, callback)->
        @exchange.publish QUEUE_NAME, message, {
            mandatory: true
            deliveryMode: 2
        }, (err)=>
            callback err, !err

    subscribe: (count, callback)->
        @queue.subscribe {
            ack: true 
            prefetchCount: count
        }, callback

ac = new AmqpConnection()
module.exports = ac

Actually, the problem is that I don’t quite understand why I pass the same options every time in exchange and queue, as well as publish. After testing, it seems that if you pass with different options, then everything breaks.
How can I wrap this up more directly?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question