Stomp Client

Stomp protocol provides an interoperable wire format so that any of the available Stomp Clients can communicate with any Stomp Message Broker to provide easy and widespread messaging interop among languages, platforms and brokers.

The Delphi Stomp Client is an open source implementation of the STOMP protocol for Delphi 2010 (should work with Delphi 2007 and 2009 too) and FreePascal 2.4.x.

This Delphi Stomp Client isn’t an attempt to copy JMS client architecture to Delphi. So aren’t included some JMS specific features like message transformations.

This stomp client is actually tested on ActiveMQ 5.2 and ActiveMQ 5.3, but should work with every STOMP compliant server.

In Delphi you can use the built-in INDY component suite or the OpenSource Synapse acording with a compiler directive. In StompClient.pas there is following definitions:

unit StompClient;

// For FreePascal users:
// Automatically selected synapse tcp library
{$IFDEF FPC}
{$MODE DELPHI}
{$DEFINE USESYNAPSE} //FREEPASCAL ALWAYS USE SYNAPSE
{$ENDIF}
// For Delphi users:
// Decomment following line to use synapse also in Delphi
{ .$DEFINE USESYNAPSE } //DELPHI USERS CAN USE INDY OR SYNAPSE

Some examples of basic functionalities (not real world example, use included examples instead):

program SimpleMessaging;
{$APPTYPE CONSOLE}

uses
  SysUtils, StompClient, StompTypes;

procedure Example_Pub_Subscriber;
var
  StompPub, StompSubscriber: IStompClient;
  StompFrame: IStompFrame;
begin
  WriteLn('==> Example_Pub_Subscriber');
  StompSubscriber := StompUtils.NewStomp('127.0.0.1'); // default port
  StompSubscriber.Subscribe('/topic/dummy');
  StompPub := StompUtils.NewStomp('127.0.0.1'); // default port
  StompPub.Send('/topic/dummy', 'Some test message');
  repeat
    StompFrame := StompSubscriber.Receive;
  until Assigned(StompFrame);
  WriteLn(StompFrame.GetBody); // Print "Some test message"
  WriteLn;
end;

procedure Example_OnePub_TwoSubscriber;
var
  StompPub, StompSub1, StompSub2: IStompClient;
  StompFrame: IStompFrame;
begin
  WriteLn('==> Example_OnePub_TwoSubscriber');
  StompSub1 := StompUtils.NewStomp('127.0.0.1'); // default port
  StompSub2 := StompUtils.NewStomp('127.0.0.1'); // default port
  StompSub1.Subscribe('/topic/dummy');
  StompSub2.Subscribe('/topic/dummy');

 
  StompPub := StompUtils.NewStomp('127.0.0.1'); // default port
  StompPub.Send('/topic/dummy', 'First test message on a topic');
  StompPub.Send('/topic/dummy', 'Second test message on a topic');

  StompFrame := StompSub1.Receive(2000);
  if Assigned(StompFrame) then
    WriteLn(StompFrame.GetBody);
  StompFrame := StompSub1.Receive(2000);
  if Assigned(StompFrame) then
    WriteLn(StompFrame.GetBody);

  StompFrame := StompSub2.Receive(2000);
  if Assigned(StompFrame) then
    WriteLn(StompFrame.GetBody);
  StompFrame := StompSub2.Receive(2000);
  if Assigned(StompFrame) then
    WriteLn(StompFrame.GetBody);
  WriteLn;
end;

procedure Example_PointToPoint;
var
  StompPub, StompSub1, StompSub2: IStompClient;
  StompFrame: IStompFrame;
begin
  WriteLn('==> Example_PointToPoint');
  StompSub1 := StompUtils.NewStomp('127.0.0.1'); // default port
  StompSub2 := StompUtils.NewStomp('127.0.0.1'); // default port
  StompSub1.Subscribe('/queue/dummy');
  StompSub2.Subscribe('/queue/dummy');

  StompPub := StompUtils.NewStomp('127.0.0.1'); // default port
  StompPub.Send('/queue/dummy', 'First test message on a queue');
  StompPub.Send('/queue/dummy', 'Second test message on a queue');

  StompFrame := StompSub1.Receive(2000);
  if Assigned(StompFrame) then
    WriteLn(StompFrame.Output);
  StompFrame := StompSub1.Receive(2000);
  if Assigned(StompFrame) then
    WriteLn(StompFrame.Output);

  StompFrame := StompSub2.Receive(2000);
  if Assigned(StompFrame) then
    WriteLn(StompFrame.Output);
  StompFrame := StompSub2.Receive(2000);
  if Assigned(StompFrame) then
    WriteLn(StompFrame.Output);

  WriteLn;
end;


begin
  try
    Example_Pub_Subscriber;
    Example_OnePub_TwoSubscriber;
    Example_PointToPoint;
  except
    on E: Exception do
      WriteLn(E.ClassName, ': ', E.message);
  end;
end.

DelphiStompClient have also a simple listener for asynchronous use. To use the listener you should implement follwing interface:

  IStompClientListener = interface
    ['{C4C0D932-8994-43FB-9D32-A03FE86AEFE4}']
    procedure OnMessage(StompFrame: IStompFrame);
  end;

Also a normal form can be used

  TForm1 = class(TForm, IStompClientListener)
  ...
  public
    procedure OnMessage(StompFrame: IStompFrame);  //Called by the stomp receiver
  end;

And then you can manager your message in a simple form’s method.

There are many features in this STOMP client. I’ve tried to respect all the STOMP specs also in the transactions side (http://stomp.codehaus.org/Protocol).

You can find all the source code and the examples at following GitHub project:

https://github.com/danieleteti/delphistompclient

The FreePascal version is actually mantained by Daniel Gaspary, thank Daniel.

Lascia una risposta