如果我的 ServiceBroker 目标 "listener" 调用结束对话,它会删除自从它获取当前消息后添加的任何消息吗?

If my ServiceBroker target "listener" calls end conversation, does it delete any messages added since it picked up the current message?

我有 SQL 服务器的 ServiceBroker。发件人 "initiator" 在 table 的 INSERT 上触发。它可以经常被调用,并且可以在目标 reading/handling 的一条消息中发生多次。

在 Microsoft 的文档中指出:

When a conversation ends, Service Broker removes all messages for the conversation from the service queue.

https://msdn.microsoft.com/en-us/library/ms177521.aspx

它认为 "conversation" 是什么?仅仅是那一条信息吗?或者,如果触发器在目标结束对话之前再次调用,并添加了另一条消息,那条消息是否也会被删除?

目标运行如下:

CREATE PROCEDURE [dbo].[MyTypeProcessed]
AS
DECLARE @ConversationHandle UNIQUEIDENTIFIER
DECLARE @MessageType NVARCHAR(256)
DECLARE @MessageBody XML
DECLARE @ResponseMessage XML
DECLARE @strMessageBody NVARCHAR(MAX)

WHILE(1=1)
    BEGIN
    BEGIN TRY
    WAITFOR(RECEIVE TOP(1)
    @ConversationHandle = conversation_handle,
    @MessageType = message_type_name,
    @MessageBody = CAST(message_body AS XML)
    FROM
    MyTypeTargetQueue
    ), TIMEOUT 1000
    IF(@@ROWCOUNT=0)
        BEGIN
        RETURN
        END
    SELECT @MessageType
    IF @MessageType = 'MyTypeRequest'
        BEGIN
        SET @strMessageBody = cast(@MessageBody as nvarchar(Max));
        EXEC SP_HandleMessage @strMessageBody
        --Close the conversation on the Payment Service
        END CONVERSATION @ConversationHandle
        END
    END TRY
    BEGIN CATCH
    SELECT ERROR_MESSAGE()
    END CATCH
    END
GO

Service Broker 中的对话是由同一对话句柄标识的任何内容。那么什么是会话句柄?当你开始一个对话时,你发出这样的声明:

declare @ch uniqueidentifier;
begin dialog @ch
   from service [foo]
   to service 'bar'
   on contract [YourContract];

在此示例中,@ch 是会话句柄。您在该对话的发起者和目标之间发送的任何消息(反之亦然)都将使用该标识符。发起者或目标都可以发送任意数量的消息(也就是说,它不限于 "one request/one response" 范例)。

将文档中的引用与同一文档中的下一行配对:

After a conversation ends, an application can no longer send or receive messages for that conversation.

表示对于这边的对话,activity都算完成了。因此,留在队列中的此对话的任何消息都将被丢弃。当发出 END CONVERSATION 语句时,一条消息被放入队列中以供另一方处理。但它会按收到的顺序处理它。也就是如果对话的另一边有消息积压,就会一直处理,直到处理完结束对话消息。