AMQP 1.0.0 Delphi Client

From sgcWebSockets 2024.2.0 AMQP 1.0.0 is supported.

AMQP (Advanced Message Queuing Protocol) 1.0.0 is a messaging protocol designed for reliable, asynchronous communication between distributed systems. It facilitates the exchange of messages between applications or components in a decoupled manner, allowing them to communicate without direct dependencies. 

Overall, AMQP 1.0.0 provides a standardized and interoperable way for different software components and systems to communicate in a loosely coupled manner, making it suitable for various distributed and enterprise-level applications. 

AMQP Features

  • Message-oriented communication: AMQP 1.0.0 is centered around the concept of messages. Messages can carry data, instructions, or commands and are the fundamental units of communication.
  • Message Brokers: The protocol operates on a brokered messaging model. Brokers, which can be servers or intermediary entities, manage the routing and delivery of messages between producers and consumers.
  • Queues and Exchanges: Queues are storage entities within the broker where messages are temporarily stored. Exchanges define the rules for routing messages from producers to queues based on criteria like message content or routing keys.
  • Addresses and Links: Addresses identify message destinations within the messaging infrastructure. Links are communication channels between a sender (producer) and a receiver (consumer) associated with a specific address.
  • Sessions and Connections: Sessions represent a logical channel for communication, allowing multiple streams of messages within a single connection. Connections manage the overall communication link between client applications and the message broker.
  • Security: AMQP 1.0.0 supports various security mechanisms, including authentication and authorization, to ensure secure communication between clients and brokers.
  • Transport Agnostic: The protocol is designed to be transport agnostic, meaning it can operate over different network transports such as TCP, TLS, or WebSockets, providing flexibility in deployment.
  • Flow Control: AMQP 1.0.0 includes mechanisms for flow control, allowing consumers to indicate their ability to handle incoming messages at a given rate. This helps prevent overwhelming consumers with a large number of messages.
  • Error Handling: The protocol specifies mechanisms for handling errors, including acknowledgment and rejection of messages, ensuring robustness and reliability in message delivery.
  • SASL Authentication: Simple Authentication and Security Layer (SASL) is used for authenticating and securing connections between clients and brokers.

Configuration

The AMQP 1.0.0 client has the property AMQPOptions where you can configure the connection.

  • ChannelMax: The channel-max value is the highest channel number that can be used on the connection. This
    value plus one is the maximum number of sessions that can be simultaneously active on the
    connection
  • ContainerId: (optional) is the name of the source container, identifies uniquely the connection in the server.
  • CreditSize: default size of the credit flow.
  • IdleTimeout: The timeout is triggered by a local peer when no frames
    are received after a threshold value is exceeded. The idle timeout is measured in milliseconds, and starts from
    the time the last frame is received.
  • MaxFrameSize: the max accepted frame size.
  • MaxLinksPerSession: the max number of links per session.
  • WindowSize: the default window size.

The AMQP Authentication must be configured in the Authentication property.

  • AuthType: type of authentication
    • amqp1authNone: not configured.
    • amqp1authSASLAnonymous: anonymous authentication
    • amqp1authSASLPlain: user/password authentication. This type of authentication requires to fill the following properties:
      • Username
      • Password
    • amqp1authSASLExternal: external authentication

Connection

 The connection starts with the client (usually a messaging application or service) initiating a TCP connection to the server (the message broker). The client connects to the server's port, typically 5672 for non-TLS connections and 5671 for TLS-secured connections. Once the TCP connection is established, the client and server negotiate the AMQP protocol version they will use. AMQP 1.0.0 supports various versions, and during negotiation, both parties agree on using version 1.0.0.

After protocol negotiation, the client may need to authenticate itself to the server, depending on the server's configuration. Authentication mechanisms can include SASL (Simple Authentication and Security Layer) mechanisms like PLAIN, EXTERNAL, or others supported by the server.

Example: connect to AMQP server listening on secure port 5671 and using SASL credentials

// Creating AMQP client
oAMQP := TsgcWSPClient_AMQP1.Create(nil);
// Setting AMQP authentication options
oAMQP.AMQPOptions.Authentication.AuthType := amqp1authSASLPlain;
oAMQP.AMQPOptions.Authentication.Username := 'sgc';
oAMQP.AMQPOptions.Authentication.Password := 'sgc';
// Creating WebSocket client
oClient := TsgcWebSocketClient.Create(nil);
// Setting WebSocket specifications
oClient.Specifications.RFC6455 := False;
// Setting WebSocket client properties
oClient.Host := 'www.esegece.com';
oClient.Port := 5671;
oClient.TLS := True;
// Assigning WebSocket client to AMQP client
oAMQP.Client := oClient;
// Activating WebSocket client
oClient.Active := True; 

Sessions

Once authenticated, the client opens an AMQP session. A session is a logical context for communication between the client and server. Sessions are used to group related messaging operations together. Use the method CreateSession to create a new session, the method allows to set the session name or leave empty and the component will assign automatically one.

If the session has been created successfully, the event OnAMQPSessionOpen will be fired with the details of the session. 

AMQP1.CreateSession('MySession');

procedure AMQP1AMQPSessionOpen(Sender: TObject; const aSession: TsgcAMQP1Session; 
  const aBegin: TsgcAMQP1FrameBegin);
begin
  ShowMessage('#session-open: ' + aSession.Id);
end; 

Links

Within a session, the client creates links to communicate with specific entities like queues, topics, or other resources provided by the server. Links are bidirectional communication channels used for sending and receiving messages.

The component can work as a sender and receiver node. Allows to create any number of links for each session, up to the limit set in the MaxLinksPerSession property. 

Sender Links

To create a new sender link, use the method CreateSenderLink and pass the name of the session and optionally the name of the sender link. If the link is created successfully, the event OnAMQPLinkOpen is raised.

AMQP1.CreateSenderLink('MySession', 'MySenderLink');

procedure procedure TfrmClientAMQP1.AMQP1AMQPLinkOpen(Sender: TObject; 
  const aSession: TsgcAMQP1Session; const aLink: TsgcAMQP1Link; const aAttach: TsgcAMQP1FrameAttach);
begin
  ShowMessage('#link-open: ' + aLink.Name);
end; 

Receiver Links

To create a new receiver link, use the method CreateReceiverLink and pass the name of the session and optionally the name of the receiver link. If the link is created successfully, the event OnAMQPLinkOpen is raised. 

AMQP1.CreateReceiverLink('MySession', 'MyReceiverLink');

procedure procedure TfrmClientAMQP1.AMQP1AMQPLinkOpen(Sender: TObject; 
  const aSession: TsgcAMQP1Session; const aLink: TsgcAMQP1Link; const aAttach: TsgcAMQP1FrameAttach);
begin
  ShowMessage('#link-open: ' + aLink.Name);
end; 

Send Messages

With the session established and links created, the client can start performing message operations such as sending messages to a destination. Use the method SendMessage to send a message using a sender link. 

AMQP1.SendMessage('MySession', 'MySenderLink', 'My first AMQP Message'); 

Read Messages

By default, the Receiver Links are created in Automatic mode, which means that every time a new message arrives, it will be delivered to the client.

If the Receiver Links has been created in manual mode, use the Sync Method GetMessage to fetch and wait till a new message arrives.

In Automatic and Manual mode, every time a new message arrives, the event OnAMQPMessage is fired. 

procedure OnAMQPMessageEvent(Sender: TObject; const aSession:
    TsgcAMQP1Session; const aLink: TsgcAMQP1ReceiverLink; const aMessage:
    TsgcAMQP1Message; var DeliveryState: TsgcAMQP1MessageDeliveryState);
begin
  ShowMessage(aMessage.ApplicationData.AMQPValue.Value);
end; 

Documentation

Download Demo

Download the AMQP 1.0.0 Client Demo compiled for Windows using the sgcWebSockets library. 

sgcAMQP1
2.9 mb
×
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.

sgcWebSockets 2024.2
sgcIndy & DevExpress

Related Posts