This article shows how IP multicast can be used to discover Apache ActiveMQ message broker instances in the local network using Indy. Example output:
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/