IPWorks MQ 2020 Node.js Edition

Questions / Feedback?

FetchMessage Method

Attempts to fetch a message from a given queue.

Syntax

amqpclassic.fetchMessage(channelName, queueName, noAck, [callback])

Callback

The 'callback' parameter specifies a function which will be called when the operation completes (or an error is encountered). If the 'callback' parameter is not specified, then the method will block and will not return until the operation completes (or an error is encountered).

The callback for this method is defined as:

function(err){ }

'err' is the error that occurred. If there was no error, then 'err' is 'null'.

'err' has 2 properties which hold detailed information:

err.code
err.message

Remarks

This method attempts to fetch a message from the queue named QueueName over the channel named ChannelName.

If a message is fetched as a result of this method being called, it is immediately added to the IncomingMessage* properties, the ReceivedMessage* properties are populated, and the MessageIn event fires.

Even if no message gets fetched, the MessageIn event will still fire as long as the request was successful. The server returns the number of available messages in the specified queue in response to all successful fetch requests, and that count is exposed by MessageIn event's MessageCount parameter. Refer to the MessageIn event for more information.

QueueName must be a non-empty string consisting only of letters, digits, hyphens, underscores, periods, and colons. It must be no longer than 255 characters.

The NoAck parameter controls whether the server will expect the class to acknowledge the fetched message. Refer to MessageIn for more information about acknowledging messages.

An exception is thrown if no channel with the given ChannelName exists, or if the server returns an error because:

  • No queue with the given QueueName exists.
  • The specified queue exists, but is locked or otherwise unavailable to consume from (e.g., an exclusive consumer might be attached to it).

Note that in AMQP, server errors are grouped into "connection errors" and "channel errors", and both are fatal. That is, if the server returns a channel error, it will then close the channel which caused the error; and if it returns a connection error, it will then close the connection. The AMQPClassic class's Error Codes page includes AMQP's various connection and channel errors.

Receiving a Message

// MessageIn event handler.
amqpc1.OnMessageIn += (s, e) => {
  if (e.MessageCount == -1) {
    // The server pushed a message to us asynchronously due to a consumer we created.
    Console.WriteLine("The server pushed this message to us via consumer '" + e.ConsumerTag + "':");
    Console.WriteLine(amqpc1.ReceivedMessage.Body);
  } else if (e.DeliveryTag > 0) {
    // We pulled a message from a queue with the FetchMessage() method.
    Console.WriteLine("Message successfully pulled:");
    Console.WriteLine(amqpc1.ReceivedMessage.Body);
    Console.WriteLine(e.MessageCount + " messages are still available to pull.");
  } else {
    // We tried to pull a message, but there were none available to pull.
    Console.WriteLine("No messages available to pull.");
  }
};

// Attach a consumer to "MyQueue".
amqpc1.Consume("channel", "MyQueue", "consumerTag", false, true, false, false);

// Or, try to fetch a message from "MyQueue".
amqpc1.FetchMessage("channel", "MyQueue", true);

Copyright (c) 2022 /n software inc. - All rights reserved.
IPWorks MQ 2020 Node.js Edition - Version 20.0 [Build 8155]