来自消息队列激活的过程是否应该设置在循环中?

Should a procedure from message queue activation set on a loop?

我正在尝试开始使用 SQL Server Service Broker 来异步审核 Intranet 应用程序的用户正在做什么。

我已经创建了消息、合同、队列和服务。设置程序以在激活此队列时触发。

到目前为止,还不错。正在发送和接收消息。

该过程从该队列接收到 Top 1 消息并执行它需要执行的操作(基本上是插入 table)并退出。

我的问题:接收消息的程序是否需要无限循环? Queue 的 MAX_QUEUE_READERS 设置为 2。这是否意味着过程 运行 总是有 2 个实例,而不管队列中的消息数量?

是的,最好在你的激活过程中有一个循环。简单地说,Service Broker 仅在新消息到达队列时调用您的激活过程。但是,如果过程已经 运行,并且 MAX_QUEUE_READERS 池已用完,则它无法产生任何额外的处理线程。

因此,如果您的队列填充速度快于您的过程完成其工作的速度,您将开始看到未处理的消息开始在队列中累积。

另一件事是,调用过程会产生额外的成本,无论多么小。如果您尝试通过增加 MAX_QUEUE_READERS 值来缓解此问题,最终您可能会开始注意到这种开销。那,它仍然不能保证所有的消息都会被处理,并且 none 的消息会被遗忘。

下面是此类过程的典型骨架结构,如果您要构建可靠、有弹性的系统,则应遵循此方法:

create procedure [dbo].[ssb_QProcessor_MyQueue]
with execute as owner as

set nocount, ansi_nulls, ansi_padding, ansi_warnings, concat_null_yields_null, quoted_identifier, arithabort on;
set numeric_roundabort, xact_abort, implicit_transactions off;

declare @Handle uniqueidentifier, @MessageType sysname, @Body xml;
declare @Error int, @ErrorMessage nvarchar(2048), @ProcId int = @@procid;


-- Fast entry check for queue contents
if not exists (select 0 from dbo.MySBQueueName with (nolock))
    return;

while exists (select 0 from sys.service_queues where name = 'MySBQueueName' and is_receive_enabled = 1) begin

    begin try
    begin tran;

    -- Receive something, if any
    waitfor (
        receive top (1) @Handle = conversation_handle,
            @MessageType = message_type_name,
            @Body = message_body
        from dbo.MySBQueueName
    ), timeout 3000;

    if @Handle is null begin
        -- Empty, get out
        rollback;
        break;
    end;

    -- Whatever processing logic you have should be put here



    commit;
    end try
    begin catch

    if nullif(@Error, 0) is null
        select @Error = error_number(), @ErrorMessage = error_message();

    -- Check commitability of the transaction
    if xact_state() = -1
        rollback;
    else if xact_state() = 1
        commit;

    -- Try to resend the message again (up to you)
    exec dbo.[ssb_Poison_Retry] @MessageType = @MessageType, @MessageBody = @Body,
        @ProcId = @ProcId, @ErrorNumber = @Error, @ErrorMessage = @ErrorMessage;

    end catch;

    -- Reset dialog handle
    select @Handle = null, @Error = null, @ErrorMessage = null;

end;

-- Done!
return;