为了简化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. 【Java】Java-正则匹配-性能优化

    Java-正则匹配-性能优化 Java 正则 点_百度搜索 在Java类中如何用正则表达式表示小数点啊?_百度知道 使用Jakarta-ORO库的几个例子 - 小橡树 - ITeye博客 正则表达式以 ...

  2. Docker创建MySQL容器环境两部曲

    1:下载MySQL镜像 需要执行以下命令,确保主机或者VM联网,从官网下载mysql的最新镜像(镜像版本以官网为主) docker  pull  mysql 下载成功后执行 docker image ...

  3. 轻松解决vuejs跨域

    Vuejs跨域问题实战 有时候,本地使用webpack开启一个node的dev端口,项目中使用vuejs去访问别人家的api,比如豆瓣或者其他的api,不使用jsonp肯定就会报跨域的问题. 如何让我 ...

  4. java实现文件的断点续传的下载

    java的断点续传是基于之前java文件下载基础上的功能拓展 首先设置一个以线程ID为名的下载进度文件, 每一次下载的进度会保存在这个文件中,下一次下载的时候,会根据进度文件里面的内容来判断下载的进度 ...

  5. 配置 Windows 下的 nodejs C++ 模块编译环境 安装 node-gyp

    配置 Windows 下的 nodejs C++ 模块编译环境 根据 node-gyp 指示的 Windows 编译环境说明, 简单一句话就是 "Python + VC++ 编译环境&quo ...

  6. mybatis学习资源

    官网:http://mybatis.org/index.html 代码:https://code.google.com/p/mybatisnet/ wiki:http://zh.wikipedia.o ...

  7. SuperMap打包部署要点

    折腾了一段时间,终于要发布一个版本了,但SuperMap程序怎么发布呢,需要些什么必要条件呢?本来想问问超图的技术人员的,但都没人理我,估计都去开大会去了. 下面是自己测试出来的结果,主要是根据Sup ...

  8. WIP 001 - design the applicant screen

    In this item, you only need to design the screen

  9. 8个很实用的在线工具来提高你的Web设计和开发能力

    近期在网上看到一张帖子感觉不错.只是是英文版的今天闲着没事锻炼一下英语能力分享给大家看看! 原文地址:http://webdesignledger.com/tools/8-useful-online- ...

  10. 【leetcode】solution in java——Easy5

    转载请注明原文地址: 21:Assign Cookies Assume you are an awesome parent and want to give your children some co ...