This article shows how IP multicast can be used to discover Apache ActiveMQ message broker instances in the local network using Indy. Example output:

Screenshot 2021-08-14 164137

With the code below, an application can list all ActiveMQ brokers, see their status, and get the IP addresses, protocols, and port numbers of discoverable transport connectors. Transport connectors are discoverable, if their broker.xml configuration entry includes a discoveryUri attribute, for example discoveryUri=”multicast://default”.

Source code

The solution below is based on the Internet Direct (Indy) component TIdIPMCastClient.

program DiscoverMulticast;

{$APPTYPE CONSOLE}

uses
  IdIPMCastClient, IdGlobal, IdSocketHandle,
  Classes, SysUtils;

type
  TActiveMQDiscovery = class(TIdIPMCastClient)
  private
    FBrokers: TStrings;
  public
    constructor Create;
    destructor Destroy; override;
    procedure MyIPMCastRead(Sender: TObject;
      const AData: TIdBytes; ABinding: TIdSocketHandle);
    property Brokers: TStrings read FBrokers;
  end;

{ TActiveMQDiscovery }

constructor TActiveMQDiscovery.Create;
begin
  inherited Create(nil);
  DefaultPort := 6155;
  MulticastGroup := '239.255.2.3';
  OnIPMCastRead := MyIPMCastRead;
  FBrokers := TStringList.Create;
end;

destructor TActiveMQDiscovery.Destroy;
begin
  Brokers.Free;
  inherited;
end;

procedure TActiveMQDiscovery.MyIPMCastRead(Sender: TObject;
  const AData: TIdBytes; ABinding: TIdSocketHandle);
var
  S: string;
begin
  S := StringOf(TBytes(AData));
  if Brokers.IndexOf(S) = -1 then
  begin
    Brokers.Add(S);
  end;
end;

// run the demo ----------------------------------------------------

procedure Main;
var
  MC: TActiveMQDiscovery;
begin
  MC := TActiveMQDiscovery.Create;
  try
    MC.ReuseSocket := rsTrue;
    MC.ThreadedEvent := True;
    try
      MC.Active := True;
      WriteLn('Waiting for broker discovery messages ...');
      Sleep(2500);
      if MC.Brokers.Count = 0 then
      begin
        WriteLn('No brokers found');
      end
      else
      begin
        WriteLn('Brokers found:');
        Writeln(MC.Brokers.Text);
      end;
      WriteLn('Hint any key to continue');
      Readln;
    except
      on E: Exception do
      begin
        WriteLn(E.Message);
        ReadLn;
      end;
    end;
  finally
    MC.Free;
  end;
end;

begin
  Main;
end.

This article is an updated version of https://habarisoft.wordpress.com/2013/07/07/discover-activemq-brokers-with-delphi-xe4-and-indy-10-6/

Advertisement

One thought on “Discover ActiveMQ brokers with Indy 10.6 and IP multicast

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s