StompClient的包装类

为了简化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 = 61613; 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 = 61613;
    ClientID: String = ‘Garfield‘; User: String = ‘‘; Password: String = ‘‘):
    Boolean;
begin
  stomp.SetUserName(User);
  stomp.SetPassword(Password);
  try
    stomp.Connect(Host, Port, ClientID, TStompAcceptProtocol.STOMP_Version_1_0);
    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<>0 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 = 0
  Top = 0
  Caption = ‘StompClientTest‘
  ClientHeight = 324
  ClientWidth = 384
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = ‘Tahoma‘
  Font.Style = []
  OldCreateOrder = False
  Position = poScreenCenter
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object memLog: TMemo
    Left = 0
    Top = 0
    Width = 384
    Height = 160
    Align = alClient
    ScrollBars = ssBoth
    TabOrder = 0
  end
  object RzPanel1: TRzPanel
    Left = 0
    Top = 160
    Width = 384
    Height = 164
    Align = alBottom
    TabOrder = 1
    object btnConnect: TButton
      Left = 261
      Top = 31
      Width = 75
      Height = 25
      Caption = #36830#25509
      TabOrder = 0
      OnClick = btnConnectClick
    end
    object btnDisconnect: TButton
      Left = 261
      Top = 124
      Width = 75
      Height = 25
      Caption = #26029#24320
      TabOrder = 1
    end
    object btnSub: TButton
      Left = 261
      Top = 62
      Width = 75
      Height = 25
      Caption = #35746#38405
      TabOrder = 2
      OnClick = btnSubClick
    end
    object chkDurable: TCheckBox
      Left = 272
      Top = 6
      Width = 64
      Height = 17
      Caption = #25345#20037#21270
      TabOrder = 3
    end
    object edtSub: TLabeledEdit
      Left = 55
      Top = 86
      Width = 160
      Height = 21
      EditLabel.Width = 24
      EditLabel.Height = 13
      EditLabel.Caption = #20027#39064
      LabelPosition = lpLeft
      TabOrder = 4
      Text = ‘/topic/hello‘
    end
    object edtHost: TLabeledEdit
      Left = 55
      Top = 12
      Width = 160
      Height = 21
      EditLabel.Width = 22
      EditLabel.Height = 13
      EditLabel.Caption = ‘Host‘
      LabelPosition = lpLeft
      TabOrder = 5
      Text = ‘localhost‘
    end
    object edtPort: TLabeledEdit
      Left = 55
      Top = 36
      Width = 160
      Height = 21
      EditLabel.Width = 20
      EditLabel.Height = 13
      EditLabel.Caption = ‘Port‘
      LabelPosition = lpLeft
      TabOrder = 6
      Text = ‘61613‘
    end
    object edtClientID: TLabeledEdit
      Left = 55
      Top = 59
      Width = 160
      Height = 21
      EditLabel.Width = 38
      EditLabel.Height = 13
      EditLabel.Caption = ‘ClientID‘
      LabelPosition = lpLeft
      TabOrder = 7
      Text = ‘garfield‘
    end
    object btnSend: TButton
      Left = 261
      Top = 93
      Width = 75
      Height = 25
      Caption = #21457#36865
      TabOrder = 8
      OnClick = btnSendClick
    end
    object edtContent: TLabeledEdit
      Left = 55
      Top = 113
      Width = 160
      Height = 21
      EditLabel.Width = 24
      EditLabel.Height = 13
      EditLabel.Caption = #20869#23481
      LabelPosition = lpLeft
      TabOrder = 9
      Text = #20320#22909#65292#27426#36814#20351#29992
    end
  end
end
时间: 2024-11-09 00:55:22

StompClient的包装类的相关文章

基本数据类型对象包装类

byte Byte shor Short int Integer long Long float Float double Double char Character 基本数据类型包装类对象的最常见作用 就是用于基本数据类型和字符串类型之间做转换 基本数据类型转成字符串 基本数据类型+"" 基本数据类型.toString(基本数据类型值) 如:Integer.toString(34);//将34整数变成"34" 字符串转成基本数据类型 xxx a = Xxx.par

常用类--包装类

一 包装类                          对应基本数据类型 java.lang.Integer                        int java.lang.Long                           long java.lang.Double                        double java.lang.character                      char java .lang.Boolean        

基本类型、引用类型、基本包装类型和单体内置对象

基本类型值指的是简单的数据段,而引用类型值指那些可能由多个值构成的对象.基本包装类型和单体内置对象都是特殊的引用类型. 一.基本类型 基本类型包含Undefined.Null.Boolean.Number.String 这5种.基本数据类型是按值访问的,因为可以操作保存在变量中的实际值. 基本类型保存在内存中的栈中,大小固定,复制其变量时会创建这个值的一个副本.用typeof方法可以确定一个值是哪种基本类型. 二.引用类型 引用类型包括Object .Array.  Date.  RegExp.

java中的包装类

每一个包装类都对应一种基本数据类型.包装类有:Integer.character.Byte.Short.Long.Floot.Double.Boolean这八种,分别对应的基本数据类型是:int.char.byte.short.long.floot.double.boolean这八种(后面6种的包装类都是将其所对应的基本数据类型的首字母大写). 包装类的作用是丰富了基本数据的类型只能存放值,不能存放应用的问题:同事还可以提供大量的方法或常量,充当了基本数据类型和引用数据类型的桥梁,可以方便基本数

day17 包装类、日期类

包装类 作用:1.丰富了基本数据类型只能存放值的问题,还提供了大量的方法或常量. 2.包装类充当了基本数据类型和引用数据类型转换的桥梁. 应用层面:包装类.String.基本数据类型的互相转换. 1.基本数据类型转为String: String str = Integer.toString(10): String str = 10 + "": 2.String转换为基本数据类型: int i = Integer.parseInt(str): 3.基本数据类型和包装类互转换: 语法糖--

2016/1/14 数字类处理 包装类

⑩数字处理类 取整:1,四舍五入,格式Math.round(浮点数) 2,取下限值,格式 Math.floor(浮点数)   3,取上限值,格式Math.ceil(浮点数) 生成随机数 1,Math.random()静态方法.介于0和1之间的小数               2,Random类 实例化Random Random x =new Random()                                  Random x=new Random(随机数种子)         

包装类和自动解封装

包装类 8个基本类型的包装类我们知道java是面向对象的语言,其精髓之一是可以使用多态性,提高程序的灵活度.但是java中有8个基本类型:byte,short,int,long,float,double,char,boolean.它们是以值的形式存在于内存中,而不是对象.它们不是Object的子类,不能参与面向对象的开发. 包装类的作用 让基本类型参与面向对象的开发 可以获取其基本类型的取值范围 可以将字符串转换成基本类型数据 基本类型包装类 对于8个基本类型,java提供了他们相应的包装类:基

Java知多少(24)包装类、拆箱和装箱详解

虽然 Java 语言是典型的面向对象编程语言,但其中的八种基本数据类型并不支持面向对象编程,基本类型的数据不具备“对象”的特性——不携带属性.没有方法可调用. 沿用它们只是为了迎合人类根深蒂固的习惯,并的确能简单.有效地进行常规数据处理. 这种借助于非面向对象技术的做法有时也会带来不便,比如引用类型数据均继承了 Object 类的特性,要转换为 String 类型(经常有这种需要)时只要简单调用 Object 类中定义的toString()即可,而基本数据类型转换为 String 类型则要麻烦得

Java 包装类 自动装箱和拆箱

包装类(Wrapper Class) 包装类是针对于原生数据类型的包装. 因为有8个原生数据类型,所以对应有8个包装类. 所有的包装类(8个)都位于java.lang下. Java中的8个包装类分别是:Byte, Short, Integer, Long, Float, Double, Character, Boolean,它们的使用方式都是一样的,可以实现原生数据类型与包装类型的双向转换. 下面以主要Integer类为例说明. Integer Integer类将int类型的值包装到一个对象中.