TsgcWSPClient_AMQP1

The TsgcWSClient_AMQP1 client implements the AMQP 1.0.0 protocol following the OASIS specification. The client supports Plain TCP and WebSocket connections, TLS (secure) connections are supported too.

 

Configuration

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

 

 

The AMQP Authentication must be configured in the Authentication property.

 

 

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.

 


oAMQP.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.

 


oAMQP.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.

 

 

oAMQP.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;

 

Sending 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.

 


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

 

Receiving 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;