IPWorks MQ 2020 Node.js Edition

Questions / Feedback?

Consume Method

Starts a new consumer for a given queue.

Syntax

amqpclassic.consume(channelName, queueName, consumerTag, noLocal, noAck, exclusive, noWait, [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 instructs the server to start a new consumer on the queue named QueueName; once the consumer is created, it will cause messages enqueued to the specified queue to be delivered to the class over the channel specified by ChannelName.

Consumers last as long as the channel they were created on, or until they are cancelled using the CancelConsume method. Each time a message is delivered to the class, it is immediately added to the IncomingMessage* properties, the ReceivedMessage* properties are populated, and the MessageIn event fires.

ConsumerTag is a string which uniquely identifies the new consumer on the specified channel. If empty string is passed for ConsumerTag, the server will generate a consumer tag automatically when it creates the . this auto-generated consumer tag can then be retrieved by querying the ConsumerTag configuration setting after this method returns.

The NoLocal parameter, if True, ensures that the consumer never consumes messages published on the same channel. (Note that this functionality is not available on RabbitMQ servers, which ignore the NoLocal parameter).

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

The Exclusive parameter, if True, will cause the class to request that the server create an exclusive consumer. Attaching an exclusive consumer to a queue prevents any other consumers from consuming messages from that queue.

The NoWait parameter, if True, will cause the server to execute the request asynchronously. For asynchronous request handling, the server only sends back a response in case of an error.

Additional arguments may be sent with this request by adding them to the Argument* properties. Arguments are server-dependent; refer to your server's documentation to determine if any additional arguments apply for this request.

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 given ConsumerTag is already in use on the specified channel.
  • An exclusive consumer was requested for a queue which already has consumers 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]