AMQP连接使用节点js关闭某个时间间隔

时间:2014-10-07 05:21:19

标签: node.js rabbitmq amqp

我用以下问题试了3天。请帮我解决这个问题,

>Error: Unexpected close
at succeed (/usr/local/lib/node_modules/amqplib/lib/connection.js:259:13)
at onOpenOk (/usr/local/lib/node_modules/amqplib/lib/connection.js:241:5)
at /usr/local/lib/node_modules/amqplib/lib/connection.js:160:32
at /usr/local/lib/node_modules/amqplib/lib/connection.js:154:12
at Socket.recv (/usr/local/lib/node_modules/amqplib/lib/connection.js:480:12)
at Socket.g (events.js:180:16)
at Socket.EventEmitter.emit (events.js:92:17)
at emitReadable_ (_stream_readable.js:407:10)
at emitReadable (_stream_readable.js:403:5)
at readableAddChunk (_stream_readable.js:165:9)

我正在使用amqplib + node js。每当我启动服务器时,我都会在一段时间内得到上述错误。最大值将以5分钟的间隔发生。

amqplib = amqplib.connect('amqp://'+rabit_host).then(function(conn)
{
    amqpconnection = conn;
});

io.sockets.on('connection', function(client)
{   
    client.on('receivemsg', function(arg)
    {
        amqpconnection.createConfirmChannel().then(function(channelObjSuccess)
        {
            channelObjSuccess.assertQueue(queue_name,{durable:false,autoDelete:true});
            client.assignObj = channelObjSuccess;
            channelObjSuccess.consume(queue_name, function(msg)
            {
                var encodemsg = msg.content.toString();
                var json_msg = JSON.parse(encodemsg);                           
                client.emit('chatrecive',json_msg);
            }).then(function(){
                console.log("Receive Consiuume Close");
            }); 
        });
    });

client.on('loginentry', function(arg)
{
    amqpconnection.createConfirmChannel().then(function(channelObjSuccess) {
        channelObjSuccess.assertQueue(queue_name,{durable:false,autoDelete:true});
    });         
});

client.on('sendmsg', function(arg)
{   
    var payload_stringify = JSON.stringify(arg);            
    amqpconnection.createConfirmChannel().then(function(channelObjSuccess) {
    channelObjSuccess.assertQueue(queue_name,{durable:false,autoDelete:true});
    channelObjSuccess.sendToQueue(queue_name, new Buffer(payload_stringify), {},
        function(err, ok) 
        {
            if (err !== null)
                console.log('Message Send Failure! ');
            else
            {
                channelObjSuccess.close();
            }
        });
    });
}); 

client.on('disconnect', function()
{   
    try {
    console.log("AMPQ Connection Closed - Disconnect");
    if(typeof(client.assignObj)!=undefined)
    {
        client.assignObj.close();
    }   
    }
    catch (alreadyClosed) {
        console.log("RabbitMQ Connection Already Closed " + alreadyClosed.stackAtStateChange);
    }
    });         
});

server.listen(port);

1 个答案:

答案 0 :(得分:2)

我想我找到了答案,可能是连接到AMQP时心跳作为选项的问题。 例如:

url = "amqp://turtle.rmq.cloudamqp.com/bqftjxzn?heartbeat=45";