RabbitMQ

时间:2019-06-19 08:35:49

标签: node.js rabbitmq

当我的Node服务器尝试与Java服务器通信时,每隔10-15分钟就会收到一个巨大的错误。每次遇到此错误时,我都无法继续工作,需要重新启动节点服务器来解决此问题。
这是错误:

AssertionError [ERR_ASSERTION] The expression evaluated to a falsy value: assert(this.pending.length === 0) AssertionError [ERR_ASSERTION]: The expression evaluated to a falsy value: assert(this.pending.length === 0) at Channel.C.sendOrEnqueue (/opt/osr/client_dev/node_modules/amqplib/lib/channel.js:73:5) at Channel.C._rpc (/opt/osr/client_dev/node_modules/amqplib/lib/channel.js:136:8) at /opt/osr/client_dev/node_modules/amqplib/lib/channel_model.js:59:17 at tryCatcher (/opt/osr/client_dev/node_modules/bluebird/js/release/util.js:16:23) at Function.Promise.fromNode.Promise.fromCallback (/opt/osr/client_dev/node_modules/bluebird/js/release/promise.js:185:30) at Channel.C.rpc (/opt/osr/client_dev/node_modules/amqplib/lib/channel_model.js:58:18) at Channel.C.assertQueue (/opt/osr/client_dev/node_modules/amqplib/lib/channel_model.js:86:15) at Promise (/opt/osr/client_dev/src/services/rabbitmq.service.js:143:52) at new Promise () at RabbitMQService.produceMessage (/opt/osr/client_dev/src/services/rabbitmq.service.js:99:16) at StreamService.requestStreamData (/opt/osr/client_dev/src/services/stream.service.js:1373:53) at StreamRoute.router.post (/opt/osr/client_dev/src/routes/stream.route.js:113:66) at Layer.handle [as handle_request] (/opt/osr/client_dev/node_modules/express/lib/router/layer.js:95:5) at next (/opt/osr/client_dev/node_modules/express/lib/router/route.js:137:13) at module.exports (/opt/osr/client_dev/src/middlewares/validate-authentication.js:30:13) at process._tickCallback (internal/process/next_tick.js:68:7)


这是我的代码:

  // This method produce a message to the RabbitMQ.
produceMessage(message, fieldNameToExcludeForLog = null) {

    return new Promise(async (resolve, reject) => {

        const {
            rabbitChannel,
            routingkey,
            exchangeName,
            queueName
        } = this;

        // Verify that the message data is not empty.
        if (!message) {
            reject('No message object was provided (1000168)');
        }

        // Log the send message data.
        logUtils.logObjectData({
            logLevel: LogLevel.INFO,
            applicationType: ApplicationType.SERVER,
            upperTitle: 'RabbitMQ Producer - Send message',
            lowerTitle: 'message',
            objectData: textUtils.getObjectKeyValueRecursive({
                object: message
            })
        });

        // Convert the message in JSON file.
        const requestJSON = JSON.stringify(message);

        // Generate a unique correlationId.
        const correlationId = textUtils.getCreateUniqueGuid(configurations.RABBITMQ_CORRELATION_ID_GUID_LENGTH);
        if (!correlationId) {
            reject('No correlationId parameter was provided (1000169)');
        }

        try {

            // Create the assert exchange and bind to the queue.
            rabbitChannel.assertExchange(exchangeName, 'direct', {
                durable: true
            });

            rabbitChannel.bindQueue(queueName, exchangeName, routingkey);

            // Create the assert queue.
            const q = await this.rabbitChannel.assertQueue('', {
                exclusive: true
            });

            // Publish the message to the exchange.
            rabbitChannel.publish(exchangeName, routingkey, Buffer.from(requestJSON), {
                correlationId: correlationId,
                replyTo: q.queue
            });

            // Register for the response event.
            rabbitChannel.consume(q.queue, (response) => {

                // Validate the data existence.
                if (!response) {
                    reject('No response object was provided (1000170)');
                }

                if (!response.content) {
                    reject('No response object was provided (1000171)');
                }

                // Check if the correlationId is identical.
                if (response.properties.correlationId !== correlationId) {
                    reject('Invalid correlationId message (1000172)');
                }

                const parsedMessage = (x => {
                    try {
                        return JSON.parse(x);
                    }
                    catch (ex) {
                        reject({ message: x.toString() });
                    }
                })(response.content);

                // This object holds the data to log.
                let objectLog = null;

                // Determine if to log a shallow copy of the parsedMessage, or the full parsedMessage data.
                // Create a shallow copy (without the main data) to log.
                if (fieldNameToExcludeForLog) {
                    objectLog = coreUtils.deleteObjectPath({
                        object: parsedMessage,
                        path: fieldNameToExcludeForLog
                    });
                }
                else {
                    objectLog = parsedMessage;
                }

                // Log the received message data.
                logUtils.logObjectData({
                    logLevel: LogLevel.INFO,
                    applicationType: ApplicationType.SERVER,
                    upperTitle: 'RabbitMQ Producer - Receive message',
                    lowerTitle: 'objectLog',
                    objectData: textUtils.getObjectKeyValueRecursive({
                        object: objectLog
                    })
                });

                resolve(parsedMessage);

            }, { noAck: false });

        } catch (error) {

            if (error) {
                (async () => {
                    await this.close();
                    this.rabbitConnection = null;
                    this.rabbitChannel = null;
                    this.rabbitConnection = await this.connect();
                    this.rabbitChannel = await this.createChannel(this.rabbitConnection);
                });
                reject(error);
            }
        }
    });
}


根据堆栈错误日志,异常行如下:

        // Create the assert queue.
        const q = await this.rabbitChannel.assertQueue('', {
            exclusive: true
        });


知道该怎么解决吗?

0 个答案:

没有答案