为了简化MQ调用,写了个StompClient的包装类,可以供需要的参考:

unit FStompClient;

interface

uses
SysUtils, Windows, Messages, Classes, Graphics, Controls, Forms, Dialogs,
StompClient,StompTypes; type
TMQLogEvent = procedure (log: String) of object;
TMQMessageEvent = procedure (msgTime: TDateTime; msgBody: String) of object;
type
TMQStompClient = class(TInterfacedObject, IStompClientListener)
private
FOnMQLogEvent: TMQLogEvent;
FOnMQMsgEvent: TMQMessageEvent;
FTR: string;
stomp: IStompClient;
th: TStompClientListener;
public
constructor Create;
destructor Destroy; override;
procedure AbortTransaction(tr: String);
procedure BeginTransaction(tr: String);
procedure CommitTransaction(tr: String);
function ConnectToMQ(Host: String; Port: Integer = ; ClientID: String
= 'Garfield'; User: String = ''; Password: String = ''): Boolean;
procedure DisconnectMQ;
procedure DurableSubscribe(subName, clientID: String); overload;
procedure OnMessage(StompClient: IStompClient; StompFrame: IStompFrame; var
StompListening: Boolean);
procedure OnStopListen(StompClient: IStompClient);
procedure SendPub(subName, body: String; Persistent: Boolean = True);
procedure Subscribe(subName: String); overload;
procedure Unsubscribe(subName: String);
published
property OnMQLogEvent: TMQLogEvent read FOnMQLogEvent write FOnMQLogEvent;
property OnMQMsgEvent: TMQMessageEvent read FOnMQMsgEvent write
FOnMQMsgEvent;
end; implementation { TMQStompClient } {
******************************** TMQStompClient ********************************
}
constructor TMQStompClient.Create;
begin
stomp := TStompClient.Create;
end; destructor TMQStompClient.Destroy;
begin
if assigned(th) then
begin
th.StopListening;
//By garfield
//FreeAndNil(th);
end;
stomp := nil;
end; procedure TMQStompClient.AbortTransaction(tr: String);
begin
stomp.AbortTransaction(tr);
FTR:='';
end; procedure TMQStompClient.BeginTransaction(tr: String);
begin
FTR:=tr;
stomp.BeginTransaction(tr);
end; procedure TMQStompClient.CommitTransaction(tr: String);
begin
stomp.CommitTransaction(tr);
FTR:='';
end; function TMQStompClient.ConnectToMQ(Host: String; Port: Integer = ;
ClientID: String = 'Garfield'; User: String = ''; Password: String = ''):
Boolean;
begin
stomp.SetUserName(User);
stomp.SetPassword(Password);
try
stomp.Connect(Host, Port, ClientID, TStompAcceptProtocol.STOMP_Version__);
th := TStompClientListener.Create(stomp, Self); if Assigned(FOnMQLogEvent) then
FOnMQLogEvent('连接消息服务器成功!');
Result:=True;
except
on E: Exception do
begin
Result:=False;
if Assigned(FOnMQLogEvent) then
FOnMQLogEvent('连接消息服务器失败!错误信息:'+E.ClassName + sLineBreak + E.Message);
end;
end;
end; procedure TMQStompClient.DisconnectMQ;
begin
th.StopListening;
//By garfield
//FreeAndNil(th);
stomp.Disconnect; if Assigned(FOnMQLogEvent) then
FOnMQLogEvent('与消息服务器成功断开!');
end; procedure TMQStompClient.DurableSubscribe(subName, clientID: String);
begin
stomp.Subscribe(subName, amAuto,
StompUtils.NewHeaders.Add(TStompHeaders.NewDurableSubscriptionHeader(clientID))); if Assigned(FOnMQLogEvent) then
FOnMQLogEvent('订阅持久化主题成功:'+subName+' clientID:'+clientID);
end; procedure TMQStompClient.OnMessage(StompClient: IStompClient; StompFrame:
IStompFrame; var StompListening: Boolean);
begin
TThread.Synchronize(nil,
procedure
begin
if StompFrame.GetBody.Length<> then
begin
if Assigned(FOnMQMsgEvent) then
FOnMQMsgEvent(Now,StompFrame.GetBody);
end;
end);
end; procedure TMQStompClient.OnStopListen(StompClient: IStompClient);
begin
if Assigned(FOnMQLogEvent) then
FOnMQLogEvent('监听停止');
end; procedure TMQStompClient.SendPub(subName, body: String; Persistent: Boolean =
True);
var
h: IStompHeaders;
begin
h := StompUtils.NewHeaders;
if Persistent then
h.Add(TStompHeaders.NewPersistentHeader(true));
if FTR <> '' then
stomp.Send(subName, body, FTR, h)
else
stomp.Send(subName, body, h);
end; procedure TMQStompClient.Subscribe(subName: String);
begin
stomp.Subscribe(subName);
if Assigned(FOnMQLogEvent) then
FOnMQLogEvent('订阅主题成功:'+subName);
end; procedure TMQStompClient.Unsubscribe(subName: String);
begin
stomp.Unsubscribe(subName);
end; end.

调用起来就比较简单了:

unit FfrmMain;

interface

uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs,FStompClient, Vcl.StdCtrls, Vcl.ExtCtrls,
RzPanel; type
TfrmMain = class(TForm)
memLog: TMemo;
RzPanel1: TRzPanel;
btnConnect: TButton;
btnDisconnect: TButton;
btnSub: TButton;
chkDurable: TCheckBox;
edtSub: TLabeledEdit;
edtHost: TLabeledEdit;
edtPort: TLabeledEdit;
edtClientID: TLabeledEdit;
btnSend: TButton;
edtContent: TLabeledEdit;
procedure FormCreate(Sender: TObject);
procedure btnConnectClick(Sender: TObject);
procedure btnSubClick(Sender: TObject);
procedure btnSendClick(Sender: TObject);
private
{ Private declarations }
aClient:TMQStompClient;
public
{ Public declarations }
procedure OnLog(log: String);
procedure OnMsg(msgTime: TDateTime; msgBody: String);
end; var
frmMain: TfrmMain; implementation {$R *.dfm} procedure TfrmMain.btnConnectClick(Sender: TObject);
begin
aClient.ConnectToMQ(edtHost.Text,StrToInt(edtPort.Text));
end; procedure TfrmMain.btnSendClick(Sender: TObject);
begin
aClient.SendPub(edtSub.Text,edtContent.Text,chkDurable.Checked);
end; procedure TfrmMain.btnSubClick(Sender: TObject);
begin
if chkDurable.Checked then
aClient.DurableSubscribe(edtSub.Text,edtClientID.Text)
else
aClient.Subscribe(edtSub.Text);
end; procedure TfrmMain.FormCreate(Sender: TObject);
begin
aClient:=TMQStompClient.Create;
aClient.OnMQLogEvent:=OnLog;
aClient.OnMQMsgEvent:=OnMsg;
end; procedure TfrmMain.OnLog(log: String);
begin
memLog.Lines.Add(log);
end; procedure TfrmMain.OnMsg(msgTime: TDateTime; msgBody: String);
begin
memLog.Lines.Add('收到消息:'+FormatDateTime('yyyy-mm-dd hh:mm:ss',msgTime)+' '+msgBody);
end; end.

窗口定义:

object frmMain: TfrmMain
Left =
Top =
Caption = 'StompClientTest'
ClientHeight =
ClientWidth =
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
Position = poScreenCenter
OnCreate = FormCreate
PixelsPerInch =
TextHeight =
object memLog: TMemo
Left =
Top =
Width =
Height =
Align = alClient
ScrollBars = ssBoth
TabOrder =
end
object RzPanel1: TRzPanel
Left =
Top =
Width =
Height =
Align = alBottom
TabOrder =
object btnConnect: TButton
Left =
Top =
Width =
Height =
Caption = ##
TabOrder =
OnClick = btnConnectClick
end
object btnDisconnect: TButton
Left =
Top =
Width =
Height =
Caption = ##
TabOrder =
end
object btnSub: TButton
Left =
Top =
Width =
Height =
Caption = ##
TabOrder =
OnClick = btnSubClick
end
object chkDurable: TCheckBox
Left =
Top =
Width =
Height =
Caption = ###
TabOrder =
end
object edtSub: TLabeledEdit
Left =
Top =
Width =
Height =
EditLabel.Width =
EditLabel.Height =
EditLabel.Caption = ##
LabelPosition = lpLeft
TabOrder =
Text = '/topic/hello'
end
object edtHost: TLabeledEdit
Left =
Top =
Width =
Height =
EditLabel.Width =
EditLabel.Height =
EditLabel.Caption = 'Host'
LabelPosition = lpLeft
TabOrder =
Text = 'localhost'
end
object edtPort: TLabeledEdit
Left =
Top =
Width =
Height =
EditLabel.Width =
EditLabel.Height =
EditLabel.Caption = 'Port'
LabelPosition = lpLeft
TabOrder =
Text = ''
end
object edtClientID: TLabeledEdit
Left =
Top =
Width =
Height =
EditLabel.Width =
EditLabel.Height =
EditLabel.Caption = 'ClientID'
LabelPosition = lpLeft
TabOrder =
Text = 'garfield'
end
object btnSend: TButton
Left =
Top =
Width =
Height =
Caption = ##
TabOrder =
OnClick = btnSendClick
end
object edtContent: TLabeledEdit
Left =
Top =
Width =
Height =
EditLabel.Width =
EditLabel.Height =
EditLabel.Caption = ##
LabelPosition = lpLeft
TabOrder =
Text = #######
end
end
end

StompClient的包装类的更多相关文章

  1. Integer与int的区别(包装类和基本数据类型的区别)

    1. 默认值 int默认值为0,Integer的默认值为null.推论:Integer既可以表示null又可以表示0 2. 包装类中提供了该类型相关的很多算法操作方法 如把十进制装换为2进制(toBi ...

  2. final和包装类

    ==================================================================================================== ...

  3. java之数据结构之链表及包装类、包

    链表是java中的一种常见的基础数据结构,是一种线性表,但是不会按线性的顺序存储数据,而是在每一个节点里存到下一个节点的指针.与线性对应的一种算法是递归算法:递归算法是一种直接或间接的调用自身算法的过 ...

  4. 0024 Java学习笔记-面向对象-包装类、对象的比较、String常量池问题

    包装类 基本类型-->包装类 byte-->Byte short-->Short int-->Integer long-->Long char-->Characte ...

  5. Java基础知识点1:基本类型包装类

    基本类型的包装类 简介 通常来说我们在程序中经常会使用元类型,比如 int data = 1; float data = 2.1F; 但是在有些场景中不能直接使用元类型,比如如果我们想要建立一个int ...

  6. Java学习_int和Integer的区别+包装类

    1.Java 中的数据类型分为基本数据类型和引用数据类型 int是基本数据类型,Integer是引用数据类型: Ingeter是int的包装类,int的初值为0,Ingeter的初值为null. 2. ...

  7. Java基础——基本类型和包装类、基本类型和字符串之间的转换

    基本类型和包装类之间的转换 基本类型和包装类之间经常需要互相转换,以 Integer 为例(其他几个包装类的操作雷同哦): 在 JDK1.5 引入自动装箱和拆箱的机制后,包装类和基本类型之间的转换就更 ...

  8. java : 包装类 缓冲机制的使用(转载)

    摘要: 八种基本数据类型和其包装类中 Integer valueOf(int i).Byte valueOf(byte b).Short valueOf(short s).Long valueOf(l ...

  9. Java学习笔记 05 数据包装类

    一.包装类 综述 >>java.lang包中的Integer类.Long类和Short类,分别将基本数据类型int.long和short封装成一个类.这些类都是Number的子类. Int ...

随机推荐

  1. PHP入门(一)

    一.概述 PHP(Hypertext Preprocessor缩写),全称超级文本预处理器,是一种在服务器端执行的脚本语言.因此既具备了脚本语言的优缺点 ,又具备了后台服务器语言的优异性能.可以说PH ...

  2. python Selenium+phantomjs 小技巧

    1.元素模糊定位 如抓取下面列表: elements = doc("li[id^='result_']") 2.元素精确定位 elements =doc("div[cla ...

  3. Android -- Camera.ShutterCallback

    干货 相机拍照的回调 /** * Equivalent to takePicture(shutter, raw, null, jpeg). * * @see #takePicture(ShutterC ...

  4. shell脚本用crontab执行和手动执行结果不一致

    加上 PATH=/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin 这行就好了, shell首部用 #!/usr/bin/env bash 这个移植性更 ...

  5. MySQL数据复制到其他主机时报错

    问题1: MySQL server has gone away With statement 原因:SQl insert 插入的语句天长导致 问题2:mysql a bulk size specifi ...

  6. 例子:使用Grunt创建一个Node.js类库

      创建一个文件夹. 打开命令行或者powershell, 运行npm init,根据提示填入package的信息. 在文件夹中创建index.js文件. /*! * mymongolib * Cop ...

  7. (转)No row with the given identifier exists问题的解决

    产生此问题的原因:         有两张表,table1和table2.产生此问题的原因就是table1里做了关联<one-to-one>或者<many-to-one unique ...

  8. 页面显示This is the initial start page for the WebDriver server.的解决办法

    今天在做项目的时候,遇到一个奇怪的问题,打开浏览器是正常的,但是页面不会跳转到需要的URL,而是提示一行白字,如图: 反复研究了脚本,没有问题啊,但是就是不跳转. 后来查了下,在某论坛上找到了答案: ...

  9. 持续集成之代码质量管理-Sonar [三]

    转载:https://www.abcdocker.com/abcdocker/2053 摘要 Sonar 是一个用于代码质量管理的开放平台.通过插件机制,Sonar 可以集成不同的测试工具,代码分析工 ...

  10. (纪录片)光的故事 BBC Light Fantastic (2004)

    简介: 导演: Jeremy Turner主演: Simon Schaffer / Dimitri Andreas ... Al Hazen / Edmund Dehn ... Priest/Old ...