我正在服务代理队列激活过程中测试带毒消息处理。我设置了一个事件通知以报告BROKER_QUEUE_DISABLED
事件。但是,禁用队列后,此事件将始终触发两次。这是事件通知的工作方式上的错误还是我错过了什么?
CREATE EVENT NOTIFICATION DisabledTargetQueueNotification
ON QUEUE dbo.ProcessingQueue
FOR BROKER_QUEUE_DISABLED
TO SERVICE 'DisabledQueueNotificationService', 'current database';
GO
ALTER QUEUE DisabledQueueNotificationQueue
WITH ACTIVATION
(
PROCEDURE_NAME = dbo.[HandleBrokerQueueDisabled],
STATUS = ON,
MAX_QUEUE_READERS = 1,
EXECUTE AS OWNER
)
GO
CREATE OR ALTER PROCEDURE [dbo].[HandleBrokerQueueDisabled]
WITH EXECUTE AS OWNER
AS
BEGIN
DECLARE @ch UNIQUEIDENTIFIER
DECLARE @messagetypename NVARCHAR(256)
DECLARE @messagebody XML
DECLARE @queueName varchar(100)
DECLARE @message NVARCHAR(100);
WHILE (1=1)
BEGIN
BEGIN TRY
BEGIN TRANSACTION
WAITFOR (
RECEIVE TOP(1)
@ch = conversation_handle,
@messagetypename = message_type_name,
@messagebody = CAST(message_body AS XML)
FROM DisabledQueueNotificationQueue
), TIMEOUT 60000
IF (@@ROWCOUNT = 0)
BEGIN
ROLLBACK TRANSACTION
BREAK
END
IF (@messagetypename = 'http://schemas.microsoft.com/SQL/Notifications/EventNotification')
BEGIN
SET @queueName = @messagebody.value('/EVENT_INSTANCE[1]/ObjectName[1]', 'VARCHAR(100)');
SET @message = 'Disabled queue: ' + @queueName;
-- Log queue is disabled
-- this message is printed twice
PRINT CAST(@messagebody AS NVARCHAR(4000));
END
IF (@messagetypename = 'http://schemas.microsoft.com/SQL/ServiceBroker/Error')
BEGIN
END CONVERSATION @ch;
END
IF (@messagetypename = 'http://schemas.microsoft.com/SQL/ServiceBroker/EndDialog')
BEGIN
END CONVERSATION @ch;
END
COMMIT TRANSACTION
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION
BREAK
END CATCH
END
END
GO