STOMP RabbitMQ Client Component: sgcMQ | eSeGeCe

STOMP RabbitMQ Client

TsgcWSPClient_STOMP_RabbitMQ is the STOMP client with RabbitMQ's destination conventions built in. RabbitMQ encodes its semantics in the destination string, so /queue/, /topic/, /exchange/, /amq/queue/ and /temp-queue/ each mean something different. This component turns those into named methods, and typed header records replace raw string parsing.

TsgcWSPClient_STOMP_RabbitMQ

Everything in the generic STOMP client is still available. The additions are the destination helpers and the RabbitMQ-named events with typed header objects.

Component class

TsgcWSPClient_STOMP_RabbitMQ

Specification

STOMP 1.0, 1.1 and 1.2

Transport

TCP (61613) or TLS (61614)

Languages

Delphi, C++ Builder

Transport: plain TCP and TLS. sgcMQ talks to the RabbitMQ STOMP plugin over plain TCP and TLS. RabbitMQ's Web-STOMP plugin is a WebSocket endpoint, so reaching it requires a sgcWebSockets package, which provides the WebSocket client.

Subscribe to a queue, publish to an exchange

This sample uses the RabbitMQ STOMP plugin on its native TCP port, 61613. For an encrypted link, switch on TLS on the carrier and use port 61614.

uses
  sgcTCP_Client_WS, sgcWebSocket_Classes, sgcWebSocket_Protocols,
  sgcWebSocket_Protocol_STOMP_Broker_Client,
  sgcWebSocket_Protocol_STOMP_RabbitMQ_Client;

var
  TCPClient: TsgcTCPClient;
  STOMP: TsgcWSPClient_STOMP_RabbitMQ;
begin
  TCPClient := TsgcTCPClient.Create(nil);
  TCPClient.Host := 'rabbit.example.com';
  TCPClient.Port := 61613;

  STOMP := TsgcWSPClient_STOMP_RabbitMQ.Create(nil);
  STOMP.Client := TCPClient;
  STOMP.Authentication.Enabled  := True;
  STOMP.Authentication.UserName := 'guest';
  STOMP.Authentication.Password := 'guest';

  STOMP.OnRabbitMQConnected := RabbitMQConnected;
  STOMP.OnRabbitMQMessage   := RabbitMQMessage;

  TCPClient.Active := True;
end;

procedure TForm1.RabbitMQConnected(Connection: TsgcWSConnection;
  Headers: TsgcWSRabbitMQSTOMPHeadersConnected);
begin
  // /queue/orders, durable
  STOMP.SubscribeQueue('orders');

  // /exchange/orders with routing pattern 'create'
  STOMP.SubscribeExchange('orders', 'create');
  STOMP.PublishExchange('orders', 'create', '{"id":42}');
end;

procedure TForm1.RabbitMQMessage(Connection: TsgcWSConnection;
  MessageText: string; Headers: TsgcWSRabbitMQSTOMPHeadersMessage;
  Subscription: TsgcWSBrokerSTOMPSubscriptionItem);
begin
  Memo1.Lines.Add(Headers.Destination + ': ' + MessageText);
end;
// include: sgcTCP_Client_WS.hpp, sgcWebSocket_Protocols.hpp,
// sgcWebSocket_Protocol_STOMP_RabbitMQ_Client.hpp

TsgcTCPClient *TCPClient = new TsgcTCPClient(this);
TCPClient->Host = "rabbit.example.com";
TCPClient->Port = 61613;

TsgcWSPClient_STOMP_RabbitMQ *STOMP =
  new TsgcWSPClient_STOMP_RabbitMQ(this);
STOMP->Client = TCPClient;
STOMP->Authentication->Enabled  = true;
STOMP->Authentication->UserName = "guest";
STOMP->Authentication->Password = "guest";

STOMP->OnRabbitMQConnected = RabbitMQConnected;
STOMP->OnRabbitMQMessage   = RabbitMQMessage;

TCPClient->Active = true;

// In the OnRabbitMQConnected handler:
STOMP->SubscribeQueue("orders");
STOMP->SubscribeExchange("orders", "create");
STOMP->PublishExchange("orders", "create", "{\"id\":42}");

Key properties & methods

Every helper takes the plain name, and the component builds the RabbitMQ destination string for you.

Queues

SubscribeQueue, PublishQueue and UnSubscribeQueue work with RabbitMQ's /queue/ destinations, with durable, auto-delete and exclusive flags as arguments.

Topics

SubscribeTopic, PublishTopic and UnSubscribeTopic use the /topic/ destination, which routes through RabbitMQ's amq.topic exchange.

Exchanges

SubscribeExchange(name, pattern) and PublishExchange(name, routingKey, text) address /exchange/ destinations, where a binding pattern selects what you receive.

External queues

SubscribeQueueOutside and PublishQueueOutside map to /amq/queue/, for queues that were declared elsewhere and must not be redeclared by the client.

Temporary queues

SubscribeTemporaryQueue(queue, replyTo) and PublishTemporaryQueue implement RabbitMQ's /temp-queue/ request/reply pattern with a short-lived reply destination.

Generic destinations

SubscribeEx, PublishEx and UnSubscribeEx take the raw destination string when you need something the named helpers do not cover.

Typed headers

OnRabbitMQMessage delivers a TsgcWSRabbitMQSTOMPHeadersMessage and the matching subscription item, so the destination, message id and ack mode are properties rather than substrings.

Custom headers

Every publish helper accepts a TStrings of extra headers, which is how you set RabbitMQ extensions such as message priority, expiration or persistence.

Connection

Client, Authentication, HeartBeat, Versions and Options behave exactly as they do on the generic STOMP client.

Keep exploring

Online HelpFull API reference and usage guide.
Generic STOMP ClientThe base component with the standard frame set.
AMQP 0.9.1 ClientRabbitMQ's native protocol, if STOMP is not a requirement.
Download Free TrialRun against a local RabbitMQ container.
PricingSingle, Team and Site licenses with full source code.
Best value: All-AccessEvery eSeGeCe product, Premium Support included, from €1,059/year.
See All-Access pricing

Ready to Get Started?

Download the free trial and talk to RabbitMQ over STOMP from Delphi or C++ Builder.