语言

MessageQueue.PeekById 方法

定义

返回具有指定消息标识符的消息的副本,而不从队列中删除该消息。

重载

名称 说明
PeekById(String)

查看消息标识符与参数匹配 id 的消息。

PeekById(String, TimeSpan)

查看消息标识符与参数匹配 id 的消息。 等待消息出现在队列中或发生超时。

PeekById(String)

查看消息标识符与参数匹配 id 的消息。

public:
 System::Messaging::Message ^ PeekById(System::String ^ id);
public System.Messaging.Message PeekById(string id);
member this.PeekById : string -> System.Messaging.Message
Public Function PeekById (id As String) As Message

参数

id
String

Id要查看的消息。

返回

Message其Id属性与参数匹配id。

例外

参数 id 为 null.

不存在具有指定 id 消息的消息。

访问消息队列方法时出错。

示例

下面的代码示例演示了如何使用 PeekById(String)。


// Connect to a queue on the local computer.
MessageQueue^ queue = gcnew MessageQueue(".\\exampleQueue");

// Create a new message.
Message^ msg = gcnew Message("Example Message Body");

// Send the message.
queue->Send(msg, "Example Message Label");

// Get the message's Id property value.
String^ id = msg->Id;

// Simulate doing other work so the message has time to arrive.
System::Threading::Thread::Sleep(TimeSpan::FromSeconds(10.0));

// Peek at the message.
msg = queue->PeekById(id);

queue->Close();

// Connect to a queue on the local computer.
MessageQueue queue = new MessageQueue(".\\exampleQueue");

// Create a new message.
Message msg = new Message("Example Message Body");

// Send the message.
queue.Send(msg, "Example Message Label");

// Get the message's Id property value.
string id = msg.Id;

// Simulate doing other work so the message has time to arrive.
System.Threading.Thread.Sleep(TimeSpan.FromSeconds(10.0));

// Peek at the message.
msg = queue.PeekById(id);

注解

用于 PeekById(String) 读取具有已知消息标识符的消息,无需从队列中删除。 消息的标识符在消息队列企业中是唯一的,因此队列中最多会有一条消息与给定 id 参数匹配。 如果队列当前不包含消息,则此重载将引发异常。

另外两种方法允许你查看队列中的消息: Peek 和 PeekByCorrelationId(String)。 该方法 Peek 返回队列中的第一条消息; PeekByCorrelationId(String) 返回确认、报告或应用程序生成的响应消息,该消息是发送到队列的消息所生成的。

下表显示了此方法在各种工作组模式下是否可用。

工作组模式 可用的
本地计算机 是
本地计算机和直接格式名称 是
远程计算机 否
远程计算机和直接格式名称 是

另请参阅

适用于

PeekById(String, TimeSpan)

查看消息标识符与参数匹配 id 的消息。 等待消息出现在队列中或发生超时。

public:
 System::Messaging::Message ^ PeekById(System::String ^ id, TimeSpan timeout);
public System.Messaging.Message PeekById(string id, TimeSpan timeout);
member this.PeekById : string * TimeSpan -> System.Messaging.Message
Public Function PeekById (id As String, timeout As TimeSpan) As Message

参数

id
String

Id要查看的消息。

timeout
TimeSpan

指示 TimeSpan 等待新消息可供检查的时间。

返回

Message其Id属性与参数匹配id。

例外

参数 id 为 null.

为 timeout 参数指定的值无效,可能 timeout 小于 Zero 或大于 InfiniteTimeout。

队列中不存在具有指定值 id 的消息,并且未在参数指定的 timeout 时间段过期之前到达。

访问消息队列方法时出错。

示例

下面的代码示例演示了如何使用 PeekById(String, TimeSpan)。


// Connect to a queue on the local computer.
MessageQueue^ queue = gcnew MessageQueue(".\\exampleQueue");

// Create a new message.
Message^ msg = gcnew Message("Example Message Body");

// Send the message.
queue->Send(msg, "Example Message Label");

// Get the message's Id property value.
String^ id = msg->Id;

// Peek at the message.
msg = queue->PeekById(id, TimeSpan::FromSeconds(10.0));

queue->Close();

// Connect to a queue on the local computer.
MessageQueue queue = new MessageQueue(".\\exampleQueue");

// Create a new message.
Message msg = new Message("Example Message Body");

// Send the message.
queue.Send(msg, "Example Message Label");

// Get the message's Id property value.
string id = msg.Id;

// Peek at the message.
msg = queue.PeekById(id, TimeSpan.FromSeconds(10.0));

注解

用于 PeekById(String) 读取具有已知消息标识符的消息,无需从队列中删除。 消息的标识符在消息队列企业中是唯一的,因此队列中最多会有一条消息与给定 id 参数匹配。 如果队列当前不包含消息,并且新消息在超时发生之前未到达,则此重载将引发异常。

该 timeout 参数未指定此方法的总运行时间。 而是指定等待新消息到达队列的时间。 每次新消息到达时,此方法都会检查 Id 新消息,以查看它是否与参数匹配 id 。 否则,此方法将启动超时期限,并等待另一条新消息到达。 因此,如果新消息在超时期限内继续到达,则此方法可能会无限期运行,直到超时期限过期且未收到任何新消息,或者直到消息到达其 Id 与参数匹配 id 的消息。

另外两种方法允许你查看队列中的消息: Peek 和 PeekByCorrelationId(String)。 该方法 Peek 返回队列中的第一条消息; PeekByCorrelationId(String) 返回确认、报告或应用程序生成的响应消息,该消息是发送到队列的消息所生成的。

下表显示了此方法在各种工作组模式下是否可用。

工作组模式 可用的
本地计算机 是
本地计算机和直接格式名称 是
远程计算机 否
远程计算机和直接格式名称 是

另请参阅

适用于