beniao成长笔记
奋进
博客园
::
首页
::
新随笔
::
联系
::
订阅
::
管理
posts - 90, comments - 424, trackbacks - 42
与我联系
发短消息
搜索
随笔分类
(86)
Ajax(13)
C#/ASP.NET(26)
C#编程利器(6)
Castle笔记(8)
XML/Web Service(4)
开源框架(5)
设计模式(16)
数据库系列(7)
网络编程(1)
随笔档案
(81)
2008年8月 (2)
2008年7月 (14)
2008年6月 (8)
2008年5月 (16)
2008年4月 (12)
2008年3月 (11)
2007年12月 (5)
2007年11月 (8)
2007年9月 (2)
2007年8月 (3)
文章分类
(21)
ASP.NET 2.0(8)
解决方案(收集)(1)
企业信息化(5)
网络收集(3)
网络文章(2)
网站开发(2)
关于博主
关于博主
网名:Bēniaǒ
位置: 中国.重庆
MSN:beniao123@hotmail.com
E-mail:beniao123@163.com
Blog:http://beniao.cnblogs.com QQ:277408613
友情连接
Beniao晴窗笔记
Bruce Zhang
zhenyulu
成长的强强
最新随笔
1. 解读设计模式----简单工厂模式(SimpleFactory Pattern),你要什么我就给你什么
2. C#编程利器之五:集合对象(Collections)
3. 解读设计模式----外观模式(Facade Pattern),谈阿牛讨媳妇故事
4. C#编程利器之四:委托与事件(Delegate and event)
5. C#编程利器之三:接口(Interface)
6. C#编程利器之二:结构与枚举(Structure and enumeration)
7. C#编程利器之一:类(Class)
8. 周末了,乐乐!!!!!
9. 解读设计模式----代理模式(Proxy Pattern),从买电脑的那些事儿说起
10. 解读设计模式----模板方法模式(Template Method),电脑就是这样造出来的
积分与排名
积分 - 66802
排名 - 627
最新评论
1. re: 推荐一个编辑器控件(HTML Editor for ASP.NET AJAX)
问题很多的,用起来才知道,比如客户端非空验证,和其他AJAX模块的集成,都有些不到不小的问题
--mad
2. re: 数据验证(模拟检测用户名是否存在)
@Hi
谢谢支持!
--Bēniaǒ
3. re: ASP.NET中进行消息处理(MSMQ) 三
@jcli 这个是可以的,MSMQ的数据存储在磁盘,无论你是使用B/S还是C/S,还是使用 B/S和C/S结合,都是一样的可以使用MSMQ的. "请问在BS模式下的Browser 和 Se...
--Bēniaǒ
4. re: ASP.NET中进行消息处理(MSMQ) 三
粗略的看了一下,还是有些不大明白。 请问在BS模式下的Browser 和 ServerApp 之间可以使用MSMQ吗? 应该是不行的吧。到是觉得在Browser提交数据之后,serverApp可以利用...
--jcli
5. re: 解读设计模式----简单工厂模式(SimpleFactory Pattern),你要什么我就给你什么
8cuo
--Sky-yu
6. re: 数据验证(模拟检测用户名是否存在)
你的文章很不错。
--Hi
7. re: ASP.NET中使用MSMQ进行消息处理系列文章
是不是需要客户机和服务器都要安装组件才行?
--红尘中迷茫
8. re: ASP.NET中进行消息处理(MSMQ) 三
@haideshenchu
没有实例,你可以照我在5楼的回复思想去实现,应该是没问题的.
--Bēniaǒ
9. re: 解读设计模式----简单工厂模式(SimpleFactory Pattern),你要什么我就给你什么
@kikop
^.^
--Bēniaǒ
10. re: ASP.NET中进行消息处理(MSMQ) 三
有没有 用 msmq 发送 xml文件的 实例啊
天天都在关注
--haideshenchu
ASP.NET中进行消息处理(MSMQ) 一
MSMQ
是微软消息队列的英文缩写。那么什么是消息队列?这些介绍网上一大片这里就不多说了。本文对于大虾级的人物来说这只是小玩意而已,对于初学者来说这文章还是有一定的帮助,希望路过的大虾们别笑话我班门弄斧。
一、MSMQ介绍和安装消息队列
关于MSMQ详细的介绍请大家向
http://www.baidu.com/
或
http://www.g.cn/
等专家咨询。
使用消息队列的优点:稳定、消息优先级、脱机能力以及安全性。
消息队列分为用户创建的队列(专用队列)和系统队列,用户队列分为,。我是Windows XP,看下图所示(myQueue为自己创建的消息队列,msmqtriggersnotifiations为通用队列):
对消息队列有了简单的了解后,使用
MSMQ
进行软件开发需要安装
MSMQ
,安装完后就该进入实际的开发阶段。具体的安装过程就是在控制面板里“添加/删除程序”下“添加/删除Windows组件”,完成添加就OK。安装完成后就可以通过交互界添加新的消息队列,详细如下图:
出了上面这种交互界面来创建
MSMQ
外,也可以通过编程来完成,.NET框架里的MessageQueue类下有一静态方法Create,用来完成消息队列的创建,其定义如下:
1
//
2
//
摘要:
3
//
在指定的路径中创建非事务性“消息队列”队列。
4
//
5
//
参数:
6
//
path:
7
//
要创建的队列的路径。
8
//
9
//
返回结果:
10
//
表示新队列的 System.Messaging.MessageQueue。
11
public
static
MessageQueue Create(
string
path);
12
//
13
//
摘要:
14
//
在指定的路径中创建事务性或非事务性“消息队列”队列。
15
//
16
//
参数:
17
//
transactional:
18
//
如果创建事务性队列,为 true;如果创建非事务性队列,则为 false。
19
//
20
//
path:
21
//
要创建的队列的路径。
22
//
23
//
返回结果:
24
//
表示新队列的 System.Messaging.MessageQueue。
25
public
static
MessageQueue Create(
string
path,
bool
transactional);
实现消息队列的创建简单代码(C#),创建一个名为"myQueue"的非事务性"消息队列",如下:
MessageQueue.Create(
@"
.\private$\myQueue
"
);
二、创建、删除和管理队列
在.NET环境下编写Message Queue程序的前提就是需要先安装MSMQ,本文之前已经作了详细的介绍。要开发MSMQ程序就必须学习一个很重要的类(MessageQueue),该类位于名称空间System.Messageing下。其中有几个常用的方法必须掌握:
--Create方法:创建使用指定路径的新消息队列。
--Delete方法:删除现有的消息队列。
--Existe方法:查看指定消息队列是否存在。
--
GetAllMessages()方法:得到队列中的所有消息。
--GetPublicQueues方法:在“消息队列”网络中定位消息队列。
--Peek/BeginPeek方法:查看某个特定队列中的消息队列,但不从该队列中移出消息。
--Receive/BeginReceive方法:检索指定消息队列中最前面的消息并将其从该队列中移除。
--Send方法:发送消息到指定的消息队列。
--Purge方法:清空指定队列的消息。
上述列举的方法在此就不作详细介绍,大家可以通过下面的示例程序中来体会他们各自的功能。
三、发送和序列化消息
MSMQ消息队列中定义的消息由一个主体(body)和若干属性构成。消息的主体可以由文本、二进制构成,根据需要还可以被加密。在MSMQ 中消息的大小不能够超过4MB。发送消息是通过Send方法来完成的,需要一个Message参数。
1、发送消息:
步骤:连接队列-->指定消息格式-->提供要发送的数据(主体)-->调用Send()方法将消息发送出去。详细见后面的示例程序。
2、序列化消息:
消息序列化可以通过.NET Framework附带的三个预定义格式化程序来完成:
-- XMLMessageFormatter对象----MessageQueue组件的默认格式化程序设置。
-- BinaryMessageFormatter对象;
-- ActiveXMessageFormatter对象;
由于后两者格式化后的消息通常不能为人阅读,所以我们经常用到的是XMLMessageFormatter对象。该对象构造方法有三种重载:
1
public
XmlMessageFormatter();
2
public
XmlMessageFormatter(
string
[] targetTypeNames);
3
public
XmlMessageFormatter(Type[] targetTypes);
如我们后面的示例程序中用到的序列化语句:
1
//
序列化为字符串
2
XmlMessageFormatter formatter
=
new
XmlMessageFormatter(
new
Type[]
{
typeof
(
string
) }
);
四、读取和接收消息
1、读取消息:
也就是从指定队列中获取消息,详细请查看本文前面的关于消息操作的方法介绍。
2、接收消息有两种方式:
--> 通过Receive方法--具体功能请返回本文前面有详细介绍。
--> 通过Peek方法--具体功能请返回本文前面有详细介绍。
五、消息使用实例
通过上面一系列的介绍,了解了MessageQueue类和常用的方法后,下面我们通过一个简单的示例程序来分析消息队列的创建、发送消息以及接收消息等相关知识点:
1、通过Create方法创建使用指定路径的新消息队列
1
/**/
///
<summary>
2
///
通过Create方法创建使用指定路径的新消息队列
3
///
</summary>
4
///
<param name="queuePath"></param>
5
public
static
void
Createqueue(
string
queuePath)
6
{
7
try
8
{
9
if
(
!
MessageQueue.Exists(queuePath))
10
{
11
MessageQueue.Create(
@"
.\private$\myQueue
"
);
12
}
13
else
14
{
15
Console.WriteLine(queuePath
+
"
已经存在!
"
);
16
}
17
}
18
catch
(MessageQueueException e)
19
{
20
Console.WriteLine(e.Message);
21
}
22
}
2、
连接消息队列并发送消息到队列
1
/**/
///
<summary>
2
///
连接消息队列并发送消息到队列
3
///
</summary>
4
public
static
void
SendMessage()
5
{
6
try
7
{
8
//
连接到本地的队列
9
MessageQueue myQueue
=
new
MessageQueue(
"
.\\private$\\myQueue
"
);
10
11
Message myMessage
=
new
Message();
12
myMessage.Body
=
"
消息内容
"
;
13
myMessage.Formatter
=
new
XmlMessageFormatter(
new
Type[]
{
typeof
(
string
) }
);
14
//
发送消息到队列中
15
myQueue.Send(myMessage);
16
}
17
catch
(ArgumentException e)
18
{
19
Console.WriteLine(e.Message);
20
}
21
}
3、连接消息队列并从消息队列中接收消息
1
/**/
///
<summary>
2
///
连接消息队列并从队列中接收消息
3
///
</summary>
4
public
static
void
ReceiveMessage()
5
{
6
//
连接到本地队列
7
MessageQueue myQueue
=
new
MessageQueue(
"
.\\private$\\myQueue
"
);
8
myQueue.Formatter
=
new
XmlMessageFormatter(
new
Type[]
{
typeof
(
string
) }
);
9
try
10
{
11
//
从队列中接收消息
12
Message myMessage
=
myQueue.Receive();
13
string
context
=
(
string
)myMessage.Body;
//
获取消息的内容
14
Console.WriteLine(
"
消息内容为:
"
+
context);
15
}
16
catch
(MessageQueueException e)
17
{
18
Console.WriteLine(e.Message);
19
}
20
catch
(InvalidCastException e)
21
{
22
Console.WriteLine(e.Message);
23
}
24
}
4、连接队列并清空队列的全部消息
1
/**/
///
<summary>
2
///
清空指定队列的消息
3
///
</summary>
4
public
static
void
ClearMessage()
5
{
6
MessageQueue myQueue
=
new
MessageQueue(
"
.\\private$\\myQueue
"
);
7
myQueue.Purge();
8
}
5、连接队列并获取队列的全部消息
1
/**/
///
<summary>
2
///
连接队列并获取队列的全部消息
3
///
</summary>
4
public
static
void
GetAllMessage()
5
{
6
//
连接到本地队列
7
MessageQueue myQueue
=
new
MessageQueue(
"
.\\private$\\myQueue
"
);
8
Message[] message
=
myQueue.GetAllMessages();
9
XmlMessageFormatter formatter
=
new
XmlMessageFormatter(
new
Type[]
{
typeof
(
string
) }
);
10
for
(
int
i
=
0
; i
<
message.Length; i
++
)
11
{
12
message[i].Formatter
=
formatter;
13
Console.WriteLine(message[i].Body.ToString());
14
}
15
}
上面依次的列举出来5个方法,这里我就不做测试了。上述方法全部通过测试的,我在后面提供个连接,没弄清楚的朋友可下载源程序自己去运行调试下。
本实例完整代码
1
using
System;
2
using
System.Collections.Generic;
3
using
System.Text;
4
using
System.Messaging;
5
6
namespace
MSMQ
7
{
8
class
Program
9
{
10
static
void
Main(
string
[] args)
11
{
12
Createqueue(
"
.\\myQueue
"
);
13
SendMessage();
14
GetAllMessage();
15
//
ReceiveMessage();
16
}
17
18
19
/**/
///
<summary>
20
///
通过Create方法创建使用指定路径的新消息队列
21
///
</summary>
22
///
<param name="queuePath"></param>
23
public
static
void
Createqueue(
string
queuePath)
24
{
25
try
26
{
27
if
(
!
MessageQueue.Exists(queuePath))
28
{
29
MessageQueue.Create(
@"
.\private$\myQueue
"
);
30
}
31
else
32
{
33
Console.WriteLine(queuePath
+
"
已经存在!
"
);
34
}
35
}
36
catch
(MessageQueueException e)
37
{
38
Console.WriteLine(e.Message);
39
}
40
}
41
42
/**/
///
<summary>
43
///
连接消息队列并发送消息到队列
44
///
</summary>
45
public
static
void
SendMessage()
46
{
47
try
48
{
49
//
连接到本地的队列
50
MessageQueue myQueue
=
new
MessageQueue(
"
.\\private$\\myQueue
"
);
51
52
&n