AMQP Receive Messages (3 / 3)

AMQP allows to receive the messages in 2 modes:

  • Request by Client: using the GetMessage method. If there aren't messages in the queue, the event OnAMQPBasicGetEmpty will be called.
  • Pushed by Server: using the Consume method.

Consume 

Consumers consume from queues. In order to consume messages there has to be a queue. When a new consumer is added, assuming there are already messages ready in the queue, deliveries will start immediately.

The target queue can be empty at the time of consumer registration. In that case first deliveries will happen when new messages are enqueued.

Consuming messages is an asynchronous task, which means that every time a new message can be delivered to the consumer queue, it's pushed by the server to the client automatically. You can read an alternative method to Receive Message Synchronously.

The method Consume creates a new consumer in the queue, and every time there is a new message this will be delivered automatically to the consumer client.

The method has the following arguments:

  • ChannelName: it's the name of the channel (must be open before call this method).
  • QueueName: it's the name of the queue, must be no longer of 255 characters and not begin with "amq." (except if passive parameter is true).
  • ConsumerTag: it's the name of the consumer and must be unique. If it's not set, then the server creates a new one.
  • NoLocal: if true means the consumer never consumes messages published on the same channel.
  • NoAck: if true means the server doesn't expect an acknowledgment for every message delivered.
  • Exclusive: if true prevents that other consumers consume messages from this queue.
  • NoWait: if true, the server won't send an acknowledgment to the client.

The messages are delivered OnAMQPBasigGetOk event.

AMQP.Consume('channel_name', 'exchange_name', 'consumer_tag');
 
procedure OnAMQPBasicGetOk(Sender: TObject; const aChannel: string; const aGetOk: TsgcAMQPFramePayload_Method_BasicGetOk; const aContent: TsgcAMQPMessageContent);
begin
  DoLog('#AMQP_basic_GetOk: ' + aChannel + ' ' + IntToStr(aGetOk.MessageCount) + ' ' + aContent.Body.AsString);
end; 

Get Messages 

Getting messages is a Synchronous task, which means that is the client who ask to server is there are messages in the queue. You can read an alternative method to Receive Message Aynchronously.

The method GetMessage sends a request to the AMQP server asking if there are messages available in a queue. If there are messages these will be dispatched OnAMQPBasicGetOk event and if the queue is empty, the event OnAMQPBasicGetEmpty will be called.

The method has the following arguments:

  • ChannelName: it's the name of the channel (must be open before call this method).
  • QueueName: it's the name of the queue, must be no longer of 255 characters and not begin with "amq." (except if passive parameter is true).
  • NoWait: if true, the server won't send an acknowledgment to the client.
AMQP.GetMessage('channel_name', 'exchange_name');
 
procedure OnAMQPBasicGetOk(Sender: TObject; const aChannel: string; const aGetOk: TsgcAMQPFramePayload_Method_BasicGetOk; const aContent: TsgcAMQPMessageContent);
begin
  DoLog('#AMQP_basic_GetOk: ' + aChannel + ' ' + IntToStr(aGetOk.MessageCount) + ' ' + aContent.Body.AsString);
end;
 
procedure OnAMQPBasicGetEmpty(Sender: TObject; const aChannel: string);
begin
  DoLog('#AMQP_basic_GetEmpty: ' + aChannel);
end; 
×
Stay Informed

When you subscribe to the blog, we will send you an e-mail when there are new updates on the site so you wouldn't miss them.

Bitmex Delphi Client Updated
AMQP Publish Messages (2 / 3)

Related Posts