Delphi下MSMQ(Mircosoft Message Queue)实例(私有队列)
2015-12-25 00:00
459 查看
摘要: MSMQ(Mircosoft Message Queue
打开控制面板->程序->添加组件,添加消息队列
打开控制面板->计算机管理->服务与应用程序->消息队列,添加私有有消息Test.
在Delphi中添加MSMQ控件, TMSMQMessage; TMSMQQueueInfo; TMSMQQueue; TMSMQEvent; 这些控件在Project->Import type Library里存在。
源代码如下:
view plaincopy
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,MSMQ_TLB,ComObj, StdCtrls, OleServer;
type
TForm1 = class(TForm)
MSMQMessage1: TMSMQMessage;
MSMQQueueInfo1: TMSMQQueueInfo;
MSMQQueue1: TMSMQQueue;
MSMQEvent1: TMSMQEvent;
Button1: TButton;
edit1: TEdit;
edit2: TEdit;
Button2: TButton;
lbl1: TLabel;
lbl2: TLabel;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure MSMQEvent1Arrived(Sender: TObject; var Queue: OleVariant;
Cursor: Integer);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
//发送消息
procedure TForm1.Button1Click(Sender: TObject);
begin
//确定消息队列路径
MSMQQueueInfo1.PathName :='./Private$/Test';
//远程机器名
MSMQQueueInfo1.RemoteMachineName := '127.0.0.1' ;
//消息内容
(MSMQMessage1.DefaultInterface as IMSMQMessage).body :=edit1.Text;
//连接到消息队列
MSMQQueue1.ConnectTo(MSMQQueueInfo1.Open(MQ_SEND_ACCESS, 0));
//发送消息
MSMQMessage1.Send(MSMQQueueInfo1.Open(MQ_SEND_ACCESS, MQ_DENY_NONE));
showmessage( '已经把信息入写入消息队列中 ');
end;
//接收消息
procedure TForm1.Button2Click(Sender: TObject);
begin
msmqqueueinfo1.PathName :='./Private$/Test';
msmqqueue1.Disconnect;
msmqqueue1.ConnectTo(msmqqueueinfo1.Open(1, 0));
//
msmqqueue1.EnableNotification(MSMQEvent1.DefaultInterface);
end;
//MSMQEvent事件
procedure TForm1.MSMQEvent1Arrived(Sender: TObject; var Queue: OleVariant;
Cursor: Integer);
var
Msg: Variant;
begin
//从队列中读取消息
Msg := msmqqueue1.Receive;
edit2.Text := Msg.body;
end;
end.
Delphi下MSMQ(Mircosoft Message Queue)实例(私有队列)
网上关于消息队列技术原理说明的详细文档很多,但涉及到Delphi的具体实现很少,这是我从网上找了一上午的资料,自己整合和尝试的能运行的程序。打开控制面板->程序->添加组件,添加消息队列
打开控制面板->计算机管理->服务与应用程序->消息队列,添加私有有消息Test.
在Delphi中添加MSMQ控件, TMSMQMessage; TMSMQQueueInfo; TMSMQQueue; TMSMQEvent; 这些控件在Project->Import type Library里存在。
源代码如下:
view plaincopy
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,MSMQ_TLB,ComObj, StdCtrls, OleServer;
type
TForm1 = class(TForm)
MSMQMessage1: TMSMQMessage;
MSMQQueueInfo1: TMSMQQueueInfo;
MSMQQueue1: TMSMQQueue;
MSMQEvent1: TMSMQEvent;
Button1: TButton;
edit1: TEdit;
edit2: TEdit;
Button2: TButton;
lbl1: TLabel;
lbl2: TLabel;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure MSMQEvent1Arrived(Sender: TObject; var Queue: OleVariant;
Cursor: Integer);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
//发送消息
procedure TForm1.Button1Click(Sender: TObject);
begin
//确定消息队列路径
MSMQQueueInfo1.PathName :='./Private$/Test';
//远程机器名
MSMQQueueInfo1.RemoteMachineName := '127.0.0.1' ;
//消息内容
(MSMQMessage1.DefaultInterface as IMSMQMessage).body :=edit1.Text;
//连接到消息队列
MSMQQueue1.ConnectTo(MSMQQueueInfo1.Open(MQ_SEND_ACCESS, 0));
//发送消息
MSMQMessage1.Send(MSMQQueueInfo1.Open(MQ_SEND_ACCESS, MQ_DENY_NONE));
showmessage( '已经把信息入写入消息队列中 ');
end;
//接收消息
procedure TForm1.Button2Click(Sender: TObject);
begin
msmqqueueinfo1.PathName :='./Private$/Test';
msmqqueue1.Disconnect;
msmqqueue1.ConnectTo(msmqqueueinfo1.Open(1, 0));
//
msmqqueue1.EnableNotification(MSMQEvent1.DefaultInterface);
end;
//MSMQEvent事件
procedure TForm1.MSMQEvent1Arrived(Sender: TObject; var Queue: OleVariant;
Cursor: Integer);
var
Msg: Variant;
begin
//从队列中读取消息
Msg := msmqqueue1.Receive;
edit2.Text := Msg.body;
end;
end.
相关文章推荐
- C#队列Queue用法实例分析
- 用PHP写的基于Memcache的Queue实现代码
- C#队列Queue多线程用法实例
- linux中编写自己的并发队列类(Queue 并发阻塞队列)
- android的消息处理机制(图文+源码分析)―Looper/Handler/Message
- vector,map,list,queue的区别详细解析
- Laravel 4.2 中队列服务(queue)使用感受
- Python Queue模块详解
- Python多进程通信Queue、Pipe、Value、Array实例
- python使用Queue在多个子进程间交换数据的方法
- HAZELCAST 客户端命令 可用于简单调试
- EJB3.0 JBoss的JMS实例
- IBM WebSphere MQ介绍安装以及配置服务详解
- java 队列知识
- python多线程队列安全
- Message、Handler、Message Queue、Looper、Thread之间的关系(未完成)
- CentOS网络启动错误
- packet to client mac reached max retries removing the client
- PowerShell 多线程测试IP端口
- 队列的使用