http://msdn2.microsoft.com/en-us/library/ms711472.aspx
<!--[if supportFields]><span
lang=EN-US style='mso-fareast-font-family:SimSun;mso-fareast-language:ZH-CN'><span
style='mso-element:field-begin'></span><span style='mso-spacerun:yes'></span>TOC
\o "1-3" \h \z \u <span style='mso-element:field-separator'></span></span><![endif]-->
1.目的
Message Queuing (MSMQ) 消息队列技术可以实现应用系统间的网络数据传输。应用程序可以发送消息给队列,或从队列中读取消息。
<!--[if gte vml 1]><v:shapetype id="_x0000_t75"
coordsize="21600,21600" o:spt="75" o:preferrelative="t" path="m@4@5l@4@11@9@11@9@5xe"
filled="f" stroked="f">
<v:stroke joinstyle="miter" />
<v:formulas>
<v:f eqn="if lineDrawn pixelLineWidth 0" />
<v:f eqn="sum @0 1 0" />
<v:f eqn="sum 0 0 @1" />
<v:f eqn="prod @2 1 2" />
<v:f eqn="prod @3 21600 pixelWidth" />
<v:f eqn="prod @3 21600 pixelHeight" />
<v:f eqn="sum @0 0 1" />
<v:f eqn="prod @6 1 2" />
<v:f eqn="prod @7 21600 pixelWidth" />
<v:f eqn="sum @8 21600 0" />
<v:f eqn="prod @7 21600 pixelHeight" />
<v:f eqn="sum @10 21600 0" />
</v:formulas>
<v:path o:extrusionok="f" gradientshapeok="t" o:connecttype="rect" />
<o:lock v:ext="edit" aspectratio="t" />
</v:shapetype><v:shape id="_x0000_i1025" type="#_x0000_t75" style='width:222pt;
height:138pt'>
<v:imagedata src="file:///C:\DOCUME~1\趙磊\LOCALS~1\Temp\msohtml1\01\clip_image001.png"
o:title="" />
</v:shape><![endif]--><!--[if !vml]--><!--[endif]-->
消息队列保证消息的传递,路由和安全。它可以实现高性能的异步数据传输。
Message Queuing应用程序可以使用C++ API或者COM对象。最新版MSMQ 3.0可安装到Windows XP Professional和Windows Server 2003 family。
2.队列
队列是一种逻辑容器,可被用来保存和转发消息。应用程序可以创建一个队列,指定一个已有队列,打开一个队列,发送消息给队列,也可以从队列中读取消息, 设置,获取队列属性。
目标队列是一种消息队列。用来保存消息。目标队列被设置在服务器中,客户端程序发送消息给服务器计算机的目标队列,服务器程序则可以接收消息。
<!--[if gte vml 1]><v:shape
id="_x0000_i1026" type="#_x0000_t75" style='width:291.75pt;height:125.25pt'>
<v:imagedata src="file:///C:\DOCUME~1\趙磊\LOCALS~1\Temp\msohtml1\01\clip_image003.png"
o:title="" />
</v:shape><![endif]--><!--[if !vml]--><!--[endif]-->
队列路径名提供创建消息队列的信息。队列路径名指定了消息将要保存到队列的计算机名。要创建一个队列,必须要指定队列路径名。
查询一个队列时,可以通过directory service。
<!--[if gte vml 1]><v:shape
id="_x0000_i1027" type="#_x0000_t75" style='width:285.75pt;height:192.75pt'>
<v:imagedata src="file:///C:\DOCUME~1\趙磊\LOCALS~1\Temp\msohtml1\01\clip_image005.png"
o:title="" />
</v:shape><![endif]--><!--[if !vml]--><!--[endif]-->
消息队列提供了两个结构体来设置查询条件MQPROPERTYRESTRICTION和 MQRESTRICTION.
VT_I1:1个字符(1 byte)
VT_I2:short integer.
VT_I4:long integer.
VT_R4:A float (4-byte floating point).
VT_R8:A double-float (8-byte floating point).
VT_CY:A CURRENCY type (8-byte).
VT_DATE:An OLE DATE (8-byte) type.
VT_BSTR:Unicode的字符串
VT_ARRAY | VT_UI1:1组bytes.
Format名称提供了需要打开队列的信息。Format名称是在队列被创建时生成的唯一名称。获得Format名称的方法是:
1.当创建队列,会返回一个Format名称。
2.当查询一个队列时,应用程序可以使用API函数获得队列路径名或者队列标识(GUID).,然后可以通过MQPathNameToFormatName和MQInstanceToFormatName把它们转换为format名称。
3.实例
<!--[if gte vml 1]><v:shape
id="_x0000_i1028" type="#_x0000_t75" style='width:274.5pt;height:300pt'>
<v:imagedata src="file:///C:\DOCUME~1\趙磊\LOCALS~1\Temp\msohtml1\01\clip_image007.png"
o:title="" />
</v:shape><![endif]--><!--[if !vml]--><!--[endif]-->
开发消息队列应用程序之前保证:
<!--[if !supportLists]-->l <!--[endif]-->已经部署了消息队列。
<!--[if !supportLists]-->l <!--[endif]-->应用程序已经引用了相应的头文件和mqrt.lib库。
<!--[if !supportLists]-->1. <!--[endif]-->定一个MQQUEUEPROPS结构体;
<!--[if !supportLists]-->2. <!--[endif]-->设置队列属性PROPID_Q_PATHNAME;
<!--[if !supportLists]-->3. <!--[endif]-->初始化MQQUEUEPROPS结构体;
<!--[if !supportLists]-->4. <!--[endif]-->调用MQCreateQueue创建队列。
参考4.1 代码实现
3.2 寻找一个队列
<!--[if !supportLists]-->1. <!--[endif]-->指定寻找条件,使用查询结构体MQPROPERTYRESTRICTION和 MQRESTRICTION.
<!--[if !supportLists]-->2. <!--[endif]-->使用MQCOLUMNSET指定队列的属性;
<!--[if !supportLists]-->3. <!--[endif]-->调用MQLocateBegin开始查询;
<!--[if !supportLists]-->4. <!--[endif]-->循环中调用MQLocateNext查找下一个;
<!--[if !supportLists]-->5. <!--[endif]-->调用MQLocateEnd释放资源。
参考4.2 代码实现
1.获得队列的Format名;
2.调用MQOpenQueue;
3.返回队列句柄。
参考4.3 代码实现。
读取消息有两种,1是同步读取,2时异步读取
同步读取消息时
1.定义队列属性最大值;
2.定义MQMSGPROPS结构体;
3.指定消息属性。如LABEL,LABEL长度,BODY,BODY长度,和BODY类型;
4.实例化MQMSGPROPS结构体;
5.创建队列的format名称;
6.调用MQOpenQueue打开队列;
7.循环调用MQReceiveMessage;
8.调用MQCloseQueue释放资源。
参考4.4代码实现
发送消息时:
<!--[if !supportLists]-->1. <!--[endif]-->定义需要的常量和变量;
<!--[if !supportLists]-->2. <!--[endif]-->定义MQMSGPROPS结构体;
<!--[if !supportLists]-->3. <!--[endif]-->指定消息属性,例如LABEL;
<!--[if !supportLists]-->4. <!--[endif]-->初始化MQMSGPROPS结构体;
<!--[if !supportLists]-->5. <!--[endif]-->创建format名称打开队列;
<!--[if !supportLists]-->6. <!--[endif]-->调用MQSendMessage发送消息;
<!--[if !supportLists]-->7. <!--[endif]-->调用MQCloseQueue关闭资源。
参考4.5代码实现
4.代码实现
HRESULT CreateMSMQQueue(
LPWSTR wszPathName,
PSECURITY_DESCRIPTOR pSecurityDescriptor,
LPWSTR wszOutFormatName,
DWORD *pdwOutFormatNameLength
)
{
// 定义队列最大属性.
const int NUMBEROFPROPERTIES = 2;
// 定义属性
MQQUEUEPROPS QueueProps;
MQPROPVARIANT aQueuePropVar[NUMBEROFPROPERTIES];
QUEUEPROPID aQueuePropId[NUMBEROFPROPERTIES];
HRESULT aQueueStatus[NUMBEROFPROPERTIES];
HRESULT hr = MQ_OK;
// Validate the input parameters.
if (wszPathName == NULL || wszOutFormatName == NULL || pdwOutFormatNameLength == NULL)
{
return MQ_ERROR_INVALID_PARAMETER;
}
// 设置属性
DWORD cPropId = 0;
aQueuePropId[cPropId] = PROPID_Q_PATHNAME;
aQueuePropVar[cPropId].vt = VT_LPWSTR;
aQueuePropVar[cPropId].pwszVal = wszPathName;
cPropId++;
WCHAR wszLabel[MQ_MAX_Q_LABEL_LEN] = L"Test Queue";
aQueuePropId[cPropId] = PROPID_Q_LABEL;
aQueuePropVar[cPropId].vt = VT_LPWSTR;
aQueuePropVar[cPropId].pwszVal = wszLabel;
cPropId++;
// 初始化MQQUEUEPROPS 结构体.
QueueProps.cProp = cPropId; // Number of properties
QueueProps.aPropID = aQueuePropId; // IDs of the queue properties
QueueProps.aPropVar = aQueuePropVar; // Values of the queue properties
QueueProps.aStatus = aQueueStatus; // Pointer to the return status
// 调用MQCreateQueue创建队列
WCHAR wszFormatNameBuffer[256];
DWORD dwFormatNameBufferLength = sizeof(wszFormatNameBuffer)/sizeof(wszFormatNameBuffer[0]);
hr = MQCreateQueue(pSecurityDescriptor, // Security descriptor
&QueueProps, // Address of queue property structure
wszFormatNameBuffer, // Pointer to format name buffer
&dwFormatNameBufferLength); // Pointer to receive the queue's format name length
// Return the format name if the queue is created successfully.
if (hr == MQ_OK || hr == MQ_INFORMATION_PROPERTY)
{
if (*pdwOutFormatNameLength >= dwFormatNameBufferLength)
{
wcsncpy(wszOutFormatName, wszFormatNameBuffer, *pdwOutFormatNameLength - 1);
wszOutFormatName[*pdwOutFormatNameLength - 1] = L'\0';
*pdwOutFormatNameLength = dwFormatNameBufferLength;
}
else
{
wprintf(L"The queue was created, but its format name cannot be returned.\n");
}
}
return hr;
}
// Define the MQPROPERTYRESTRICTION
// and MQRESTRICTION structures.
const int MAX_PROPERTIES = 13; // 13 possible queue properties
CLSID PRINTER_SERVICE_TYPE = // Dummy GUID
{0x1, 0x2, 0x3, {0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb}};
HRESULT hr = MQ_OK;
MQPROPERTYRESTRICTION PropertyRestriction;
MQRESTRICTION Restriction;
//////////////////////////////////////////
// Set search criteria according to the
// type of service provided by the queue.
/////////////////////////////////////////
// Set the queue restriction to PROPID_Q_TYPE = PRINTER_SERVICE_TYPE.
PropertyRestriction.rel = PREQ;
PropertyRestriction.prop = PROPID_Q_TYPE;
PropertyRestriction.prval.vt = VT_CLSID;
PropertyRestriction.prval.puuid = &PRINTER_SERVICE_TYPE;
// Specify one property restriction.
Restriction.cRes = 1;
Restriction.paPropRes = &PropertyRestriction;
// Set a MQCOLUMNSET structure to specify
// the properties to be returned:
// PROPID_Q_INSTANCE and PROPID_Q_CREATE_TIME.
MQCOLUMNSET Column;
PROPID aPropId[2]; // Only two properties to retrieve
DWORD dwColumnCount = 0;
aPropId[dwColumnCount] = PROPID_Q_INSTANCE;
dwColumnCount++;
aPropId[dwColumnCount] = PROPID_Q_CREATE_TIME;
dwColumnCount++;
Column.cCol = dwColumnCount;
Column.aCol = aPropId;
// Call MQLocateBegin to start a query.
HANDLE hEnum = NULL;
hr = MQLocateBegin(
NULL, //Start search at the top
&Restriction, //Search criteria
&Column, //Properties to return
NULL, //No sort order
&hEnum //Enumeration handle
);
if (FAILED(hr))
{
//
// Error handling.
//
}
// Call MQLocateNext in a loop to examine the
// query results.
MQPROPVARIANT aPropVar[MAX_PROPERTIES];
DWORD cProps, index;
do
{
cProps = MAX_PROPERTIES;
hr = MQLocateNext(
hEnum, // Handle returned by MQLocateBegin
&cProps, // Size of aPropVar array
aPropVar // An array of MQPROPVARIANT for results
);
if (FAILED(hr))
{
break;
}
for (index = 0; index < cProps; index += dwColumnCount)
{
// Process properties of a queue stored in:
// aPropVar[index], aPropVar[index + 1], …,
// aPropVar[index + dwColumnCount - 1].
}
} while (cProps > 0);
// Call MQLocateEnd to end query.
hr = MQLocateEnd(hEnum); // Handle returned by MQLocateBegin.
if (FAILED(hr))
{
//
//Error handling
//
}
int OpenMyQueue(
LPWSTR wszPathName,
DWORD dwAccess,
DWORD dwShareMode,
QUEUEHANDLE *phQueue
)
{
HRESULT hr = MQ_OK;
// Validate the input parameters.
if ((wszPathName == NULL) || (phQueue == NULL))
{
return MQ_ERROR_INVALID_PARAMETER;
}
// Call MQPathNameToFormatName to obtain the format name required
// to open the queue.
DWORD dwFormatNameBufferLength = 256; // Length of the format name buffer
WCHAR wszFormatNameBuffer[256]; // Format name buffer
hr = MQPathNameToFormatName(wszPathName,
wszFormatNameBuffer,
&dwFormatNameBufferLength);
if (FAILED(hr))
{
fprintf(stderr, "An error occurred in MQPathNameToFormatName (error: 0x%x).\n", hr);
return hr;
}
// Call MQOpenQueue to open the queue with the access and
// share mode provided by the caller.
hr = MQOpenQueue(
wszFormatNameBuffer, // Format name of the queue
dwAccess, // Access mode
dwShareMode, // Share mode
phQueue // OUT: Queue handle
);
if (FAILED(hr))
{
fprintf(stderr, "An error occurred in MQOpenQueue (error: 0x%x.)\n",hr);
return hr;
}
return hr;
}
HRESULT ReadingDestQueue(
WCHAR * wszQueueName,
WCHAR * wszComputerName
)
{
// Define the required constants and variables.
const int NUMBEROFPROPERTIES = 5;
DWORD cPropId = 0;
HRESULT hr = MQ_OK; // Return code
HANDLE hQueue = NULL; // Queue handle
ULONG ulBufferSize = 2;
// Define an MQMSGPROPS structure.
MQMSGPROPS msgprops;
MSGPROPID aMsgPropId[NUMBEROFPROPERTIES];
MQPROPVARIANT aMsgPropVar[NUMBEROFPROPERTIES];
HRESULT aMsgStatus[NUMBEROFPROPERTIES];
// Specify the message properties to be retrieved.
aMsgPropId[cPropId] = PROPID_M_LABEL_LEN; // Property ID
aMsgPropVar[cPropId].vt = VT_UI4; // Type indicator
aMsgPropVar[cPropId].ulVal = MQ_MAX_MSG_LABEL_LEN; // Length of label
cPropId++;
WCHAR wszLabelBuffer[MQ_MAX_MSG_LABEL_LEN]; // Label buffer
aMsgPropId[cPropId] = PROPID_M_LABEL; // Property ID
aMsgPropVar[cPropId].vt = VT_LPWSTR; // Type indicator
aMsgPropVar[cPropId].pwszVal = wszLabelBuffer; // Label buffer
cPropId++;
UCHAR * pucBodyBuffer = NULL;
pucBodyBuffer = (UCHAR*)malloc(ulBufferSize);
if (pucBodyBuffer == NULL)
{
return MQ_ERROR_INSUFFICIENT_RESOURCES;
}
memset(pucBodyBuffer, 0, ulBufferSize);
aMsgPropId[cPropId] =
分享到:
相关推荐
java连接MSMQ的类库,找了好久的东西
自己整理写的JAVA操作微软消息队列(MSMQ),发送和接收消息,使用MyEclipse、jdk1.7,内含示例、jar、dll,我博文里有教程(已更新远程教程,请正确使用dll和jar包,有同学说有异常,经调试已发现是错误引用,按照...
微软消息队列java发送读取jar和dll文件,msmqJava.jar和msmqJava.dll
msqc#使用消息队列来完成通信,如何创建消息队列进行了详细说明.
MicroSoft Message Queuing(微软消息队列)是在多个不同的应用之间实现相互通信的一种异步传输模式,相互通信的应用可以分布于同一台机器上,也可以分布于相连的网络空间中的任一位置。它的实现原理是:消息的发送者...
必须安装 Microsoft Message Queuing 工具。 计算机必须在网络(LAN)中可用。 鉴于将安装 WinCC Fileserver V6.0 版本,因此不能安装 WinCC V6.0 版本。 安装 若想将计算机安装成为一个长期归档服务器,则在计算机上...
1.Introducing the Microsoft .NET Platform 2.Introducing C# Programming ...7.Message Queuing Using MSMQ 8.ADO.NET 9.Working with XML 10.ASP.NET 11.Web Services 12.Building a Jokes Web Service
自从在微软提出.NET战略以来,就针对建立企业级的分布式应用先后推出了一系列产品和技术,包括:ASP.NET Web服务、.NET Remoting、Message Queuing以及Enterprise Service等。这些技术为基于微软技术的软件研发人员...
常见的MQ中间件有IBM websphere message queue service,Oracle Advanced Queuing,Microsoft Message Queue(MSMQ),Apache ActiveMQ等。 其中apache ActiveMQ是基于apache协议的,和tomcat一样,你可以在项目中免费...
and how to optimize the application as a whole Work with caching and message queuing tools, including patterns and strategies Discover the dark side of performance improvement and find out how to ...
调通sina33下的AP6212A0版本的BT 大文实验室/大文哥 壹捌陆捌零陆捌捌陆捌贰 ...完成时间:2017/6/26 11:01 版本:V1.0 本文参照: 《A33 wifi移植说明书.pdf》 还有就是全志R16的parrotv1.1的官方SDK(Android...
28. Trudy can pretend to be Bob to Alice (and vice-versa) and partially or completely modify the message(s) being sent from Bob to Alice. For example, she can easily change the phrase “Alice, I owe ...