Cliente Delphi AMQP 1.0.0

· Componentes

A partir do sgcWebSockets 2024.2.0 AMQP 1.0.0 é suportado.

AMQP (Advanced Message Queuing Protocol) 1.0.0 is um messaging protocol designed para reliable, asynchronous communication entre distributed systems. It facilitates o exchange de messages entre applications ou components em um decoupled manner, allowing them para communicate sem direct dependencies. 

Overall, AMQP 1.0.0 provides um standardized e interoperable way para different software components e systems para communicate em um loosely coupled manner, making it suitable para various distributed e enterprise-level applications. 

Recursos do AMQP

Configuração

The AMQP 1.0.0 client has um propriedade AMQPOptions where você pode configure o conexão.

The AMQP Autenticação deve ser configured no Autenticação property.

Conexão

 The conexão starts com o cliente (usually um messaging application ou service) initiating um TCP conexão para o servidor (a mensagem broker). O cliente connects para o servidor's port, typically 5672 para non-TLS conexões e 5671 para TLS-secured conexões. Once o TCP conexão is established, o cliente e server negotiate o AMQP protocol version they will use. AMQP 1.0.0 suporta various versions, e during negotiation, both parties agree em using version 1.0.0.

After protocol negotiation, o cliente may precisa autenticar itself para o servidor, depending em o servidor's configuração. Autenticação mechanisms can include SASL (Simples Autenticação e Security Layer) mechanisms like PLAIN, EXTERNAL, ou others suportado por o servidor.

Exemplo: conectar um servidor AMQP escutando em secure port 5671 e 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; 

Sessões

Once authenticated, o cliente opens um AMQP session. A session is um logical context para communication entre o cliente e server. Sessions são usados para group related messaging operations together. Use o método CreateSession para criar um novo session, o método permite set o session name ou leave empty e o componente will assign automaticamente one.

If o session foi created successfully, o evento OnAMQPSessionOpen será fired com o details do session. 

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

Vínculos

Within um session, o cliente creates links para communicate com specific entities like queues, topics, ou other resources provided por o servidor. Links are bidirectional communication channels used para sending e receiving messages.

The component can work como um sender e receiver node. Allows para criar any number de links para each session, até o limit set no MaxLinksPerSession property. 

Sender Links

To create um novo sender link, use o método CreateSenderLink and pass o name do session e optionally o name do sender link. If o link is created successfully, o evento 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; 

Receptor Links

To create um novo receiver link, use o método CreateReceiverLink and pass o name do session e optionally o name do receiver link. If o link is created successfully, o evento 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; 

Enviar Mensagens

Com o session established e links created, o cliente can iniciar performing message operations como sending messages para um destination. Use o método SendMessage para enviar uma mensagem using um sender link. 

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

Receber Mensagens

Por padrão, o Receptor Links are created em Automático mode, o que significa that every time um novo message arrives, it será delivered para o cliente.

If o Receptor Links foi created em manual mode, use o Sync Method GetMessage to fetch e wait till um novo message arrives.

In Automático e Manual mode, every time um novo message arrives, o evento OnAMQPMessage é disparado. 

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

Documentação

AMQP1 Delphi Client

Leia mais sobre o Delphi / CBuilder AMQP 1.0.0 Client https://www.esegece.com/help/sgcWebSockets/br/#t=Components%2FProtocols%2FSubprotocols%2FAMQP1%2FProtocol_AMQP1.htm

Baixar Demo

Download o AMQP 1.0.0 Client Demo compiled para Windows usando um biblioteca sgcWebSockets.