Kbase P20022: Sample on How to access SonicMQ via Delphi
Autor |
  Progress Software Corporation - Progress |
Acesso |
  Público |
Publicação |
  25/02/2003 |
|
Status: Unverified
GOAL:
Sample on How to access SonicMQ via Delphi
FIX:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs,
StdCtrls, OleCtrls, SonicMQ_TLB, stdole_TLB;
// SonicMQ_TLB is a Delphi unit made using activex.tlb.
// Delphi doesn't use the OCX's, DLL's or TLB's directly, but through
// a Delphi unit that implements them.
type
TForm1 = class(TForm, IDispatch)
Label1: TLabel;
Edit1: TEdit;
ListBox1: TListBox;
Button1: TButton;
Button2: TButton;
SonicMQ1: TSonicMQ;
// I must include this visual component from the Delphi VCL
component
palette
// If the SonicMQ ActiveX component was well installed into Delphi,
in the
// Activex tab of the Delphi VCL palette
(standard,additional,etc...), there
// must appear this element. In order to include it in a unit, just
select
// the unit's form, select the SonicMQ ActiveX component from the
ActiveX
// tab and drop it into the form.
Label2: TLabel;
procedure HandleException();
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure SonicMQ1onJMSMessage(Sender: TObject;
const OnJMSMessageEvent1: IDispatch);
private
{ Private declarations }
connectresult : LongInt;
sessionid : LongInt;
topicid : LongInt;
subscriberid : LongInt;
publisherid : LongInt;
result : LongInt;
messageid : LongInt;
setresult : LongInt;
pubresult : LongInt;
username : String;
password : String;
public
{ Public declarations }
end;
var
Form1: TForm1;
failmsg : String;
implementation
{$R *.DFM}
// This is the equivalent in Delphi to the Form_Load() method in VB.
procedure TForm1.FormCreate(Sender: TObject);
var
listener : LongInt;
SonicException : Exception;
begin
username := 'ActiveXUser';
password := 'password';
failmsg := '';
SonicMQ1.setBrokerURL ('localhost:2506');
SonicMQ1.setClientID ('ActiveXClient');
SonicMQ1.setUsername (username);
SonicMQ1.setPassword (password);
try
connectresult :=
SonicMQ1.jms_CreateTopicConnection_WithDefaultUser();
if connectresult < 0 then begin
failmsg := 'jms_CreateTopicConnection_withDefaultUser failed';
raise SonicException;
end;
sessionid := SonicMQ1.jms_CreateTopicSession (False,
SonicMQ1.jms_Session_AUTO_ACKNOWLEDGE);
if sessionid < 0 then begin
failmsg := 'jms_CreateTopicSession failed';
raise SonicException;
end;
topicid := SonicMQ1.jms_CreateTopic (sessionid, 'jms.samples.chat');
if topicid < 0 then begin
failmsg := 'jms_CreateTopic failed';
raise SonicException;
end;
subscriberid := SonicMQ1.jms_CreateSubscriber (sessionid,topicid);
if subscriberid < 0 then begin
failmsg := 'jms_CreateSubscriber failed';
raise SonicException;
end;
publisherid := SonicMQ1.jms_CreatePublisher (sessionid, topicid);
if publisherid < 0 then begin
failmsg := 'jms_CreatePublisher failed';
raise SonicException;
end;
listener := SonicMQ1.jms_CreateMessageListener();
if listener < 0 then begin
failmsg := 'jms_CreateMessageListener failed';
raise SonicException;
end;
result := SonicMQ1.jms_MessageConsumer_setMessageListener
(subscriberid,
listener);
if result < 0 then begin
failmsg := 'jms_MessageConsumer_setMessageListener failed';
raise SonicException;
end;
result := SonicMQ1.jms_Connection_start();
if result < 0 then begin
failmsg := 'jms_Connection_start failed';
raise SonicException;
end;
except
on SonicException: Exception do HandleException();
end;
end;
// This action delivers a message to the topic 'jms.samples.chat'
procedure TForm1.Button1Click(Sender: TObject);
begin
messageid := SonicMQ1.jms_Session_createTextMessage(sessionid);
setresult := SonicMQ1.jms_TextMessage_setText (messageid, Edit1.Text).;
pubresult := SonicMQ1.jms_PublishMessage (publisherid,messageid);
SonicMQ1.jms_Message_free(messageid);
end;
// Closes the connection when leaving the application
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
SonicMQ1.jms_Connection_close;
end;
procedure TForm1.SonicMQ1onJMSMessage(Sender: TObject;
const OnJMSMessageEvent1: IDispatch);
var
id: Integer;
disp : DISPPARAMS;
exep : EXCEPINFO;
did : Integer;
vparam : VARIANT;
vpp : ^Variant;
uint : Integer;
result : LongInt;
nArgErr : Word;
msgtype : WideString;
iid_null : TGUID;
messageHandler: Integer;
string_null: WideString;
msgid: String;
const
str_null : array[0..18] of Char = 'getMessageID'#0;
begin
{ string_null := @str_null;}
string_null := 'getMessageID';
disp.rgvarg := nil;
disp.rgvarg := nil;
disp.cArgs := 0;
disp.cNamedArgs := 0;
iid_null.D1 := 0;
iid_null.D2 := 0;
iid_null.D3 := 0;
iid_null.D4[0] := 0;
iid_null.D4[1] := 0;
iid_null.D4[2] := 0;
iid_null.D4[3] := 0;
iid_null.D4[4] := 0;
iid_null.D4[5] := 0;
iid_null.D4[6] := 0;
iid_null.D4[7] := 0;
vpp := @vparam;
ShowMessage ('A message has been received');
result := OnJMSMessageEvent1.GetIDsOfNames(iid_null,
@string_null,1,LOCALE_SYSTEM_DEFAULT,@did);
if result >= 0 then begin
result :=
OnJMSMessageEvent1.Invoke(did,iid_null,LOCALE_SYSTEM_DEFAULT,2,disp,@vparam,@exe
p,@nArgErr);
messagehandler := vpp^;
If (messagehandler >= 0) Then begin
msgtype := SonicMQ1.jms_CheckMessageType(messagehandler);
ShowMessage ('Message type: ' + msgtype);
msgid := SonicMQ1.jms_Message_getJMSMessageID(messagehandler);
ShowMessage ('Message ID: ' + msgid);
end;
end;
end;
// Exception handling.
procedure TForm1.HandleException();
begin
SonicMQ1.jms_Connection_close();
showmessage(failmsg);
end;
end.
DISCLAIMER: This example code is provided for informational purposes
only. Its inclusion in the Progress Support Knowledgebase does not
constitute endorsement of it or support for it, nor does it
guarantee that it has been tested to the extent recommended for
application deployment. Programmers are advised that they use it at
their own risk..