A partir do sgcWebSockets 2022.1 AMQP 0.9.1 protocol é suportado. The Advanced Message Queuing Protocol (AMQP) is um abrir standard application layer protocol para message-oriented middleware. The defining features de AMQP are message orientation, queuing, routing (incluindo point-to-point e publish-and-subscribe), reliability e security.
AMQP is um binary, application layer protocol, designed para efficiently support um wide variety de messaging applications e communication patterns. It provides flow controlled, message-oriented communication com message-delivery guarantees como at-most-once (where each message is delivered once ou nunca), at-least-once (where each message is certain para be delivered, but may do so multiple times) e exactly-once (where um mensagem will sempre certainly arrive e do so somente once), e autenticação and/or encryption baseado em SASL and/or TLS. It assumes um underlying reliable transport layer protocol como Transmission Control Protocol (TCP).
Channels
AMQP is um multi-channelled protocol. Channels provide um way para multiplex um heavyweight TCP/IP conexão into several light weight conexões. This makes o protocolo more "firewall friendly" since port usage is predictable. It also means that traffic shaping e outros network QoS features pode ser facilmente employed.
Every channel run em his own thread, so every time um novo message is received, first o cliente identifies o canal e queues um mensagem em uma fila which is process por o thread channel.
The channel life-cycle is this:
1. O cliente opens um novo channel (Abrir).
2. O servidor confirms that o novo channel is ready (Abrir-Ok).
3. O cliente e server use o canal como desired.
4. One peer (client ou server) closes o canal (Fechar).
5. The other peer hand-shakes o canal fechar (Fechar-Ok).
To create um novo channel basta chamar o método OpenChannel e pass o canal name como argument. The event OnAMQPChannelOpen is raised como um confirmation sent por o servidor that o canal foi opened.
AMQP.OpenChannel('channel_name');
procedure OnAMQPChannelOpen(Sender: TObject; const aChannel: string);
begin
DoLog('#AMQP_channel_open: ' + aChannel);
end;
Exchanges
The exchange class lets um application manage exchanges em o servidor. This class lets o application script its own wiring (rather than relying em some configuração interface). Note: Most applications do not need this level de sophistication, e legacy middleware is unlikely para be able para support this semantic.
The exchange life-cycle is:
1. O cliente asks o servidor para certifique-se o exchange exists (Declare). O cliente can refine this into, "create o exchange if it does not exist", ou "warn me but do not create it, if it does not exist".
2. O cliente publishes messages para o exchange.
3. O cliente may choose para excluir o exchange (Excluir).
The DeclareExchange method creates um novo exchanges ou verifies that um Exchange already exists. O método tem os seguintes argumentos:
- ChannelName: it's o name do channel (deve ser abrir before call this method).
- ExchangeName: it's o name do exchange, deve ser no longer de 255 characters e not begin com "amq." (exceto se o parâmetro passive para true).
- ExchangeType: it's o exchange type, all AMQP servers support "direct" e "fanout" exchanges. Check o servidor documentação para know which exchanges types são suportados.
- Passive: if passive is true, o servidor somente verifies that o exchange is already declared. If passive is false, e o exchange not exists, o servidor will create um novo one.
- Durable: if true, o exchange será re-created when o servidor starts. If false, o exchange será deleted when o servidor stops.
- AutoDelete: if true, o exchange será deleted when all queues foram unbound.
- Internal: sempre false.
- NoWait: if true, o servidor doesn't sends um acknowledgment para o cliente.
To Declare um novo Exchange basta chamar o método DeclareExchange e pass o canal name, exchange name e exchange type como arguments. The event OnAMQPExchangeDeclare is raised como um confirmation sent por o servidor that o exchange foi declared.
AMQP.DeclareExchange('channel_name', 'exchange_name', 'direct');
procedure OnAMQPExchangeDeclare(Sender: TObject; const aChannel, aExchange: string);
begin
DoLog('#AMQP_exchange_declare: [' + aChannel + '] ' + aExchange);
end;
Queues
The queue class lets um application manage message queues em o servidor. This is um basic step em almost all applications that consume messages, pelo menos para verify that um expected message queue is actually present.
The life-cycle para um durable message queue is fairly simples:
1. O cliente asserts that um mensagem queue exists (Declare, com o "passive" argument).
2. O servidor confirms that um mensagem queue exists (Declare-Ok).
3. O cliente reads messages off um mensagem queue.
The life-cycle para um temporary message queue is more interesting:
1. O cliente creates um mensagem queue (Declare, often com no message queue name so o servidor will assign um name). O servidor confirms (Declare-Ok).
2. O cliente starts um consumer em um mensagem queue. The precise functionality de um consumer is defined por o Basic class.
3. O cliente cancels o consumer, either explicitly ou por closing o canal and/or conexão.
4. Quando o last consumer disappears um partir de um mensagem queue, e after um polite time-out, o servidor deletes um mensagem queue.
AMQP implements o delivery mechanism para topic inscrições como message queues. This enables interesting structures where um inscrição pode ser load balanced entre um pool de co-operating subscriber
applications.
The life-cycle para um inscrição involves um extra bind stage:
1. O cliente creates um mensagem queue (Declare), e o servidor confirms (Declare-Ok).
2. O cliente binds um mensagem queue para um topic exchange (Bind) e o servidor confirms (Bind-Ok).
3. O cliente uses um mensagem queue como no previous exemplos.
The DeclareQueue method creates um novo queue ou verifies that um Queue already exists. O método tem os seguintes argumentos:
- ChannelName: it's o name do channel (deve ser abrir before call this method).
- QueueName: it's o name do queue, deve ser no longer de 255 characters e not begin com "amq." (exceto se o parâmetro passive para true).
- Passive: if passive is true, o servidor somente verifies that um fila is already declared. If passive is false, e um fila not exists, o servidor will create um novo one.
- Durable: if true, um fila será re-created when o servidor starts. If false, um fila será deleted when o servidor stops.
- Exclusive: if true means um fila is somente accessed por o current conexão.
- AutoDelete: if true, um fila será deleted when all consumers no longer use um fila.
- NoWait: if true, o servidor doesn't sends um acknowledgment para o cliente.
To Declare um novo Queue basta chamar o método DeclareQueue e pass o canal name e queue name como arguments. The event OnAMQPQueueDeclare is raised como um confirmation sent por o servidor that o exchange foi declared.
AMQP.DeclareQueue('channel_name', 'queue_name');
procedure OnAMQPQueueDeclare(Sender: TObject; const aChannel, aQueue: string; aMessageCount, aConsumerCount: Integer);
begin
DoLog('#AMQP_queue_declare: [' + aChannel + '] ' + aQueue);
end;
