gpt4 book ai didi

SQL Server 服务代理 - 消息超时

转载 作者:行者123 更新时间:2023-12-04 23:31:52 26 4
gpt4 key购买 nike

我正在研究 sql server service broker 作为我们应用程序的消息处理技术。

在我们的场景中,从一个客户端应用程序 (WPF) 发送的消息需要在服务代理中排队等待其他客户端应用程序 (android) 接收。消息是时间敏感的,需要在被发布到队列后的“X”分钟(例如 2 分钟)内被接收者接收,如果此时无法接收,消息需要过期并从队列中删除排队。

有没有办法告诉服务代理在“x”分钟后超时消息?

编辑:添加了我用来测试的脚本。

CREATE DATABASE ServiceBrokerTest
GO

ALTER DATABASE ServiceBrokerTest SET ENABLE_BROKER WITH ROLLBACK IMMEDIATE
GO

/****** Object: MessageType [SampleMsgType] ***/
CREATE MESSAGE TYPE [SampleMsgType] AUTHORIZATION [dbo] VALIDATION = NONE
GO

/****** Object: ServiceContract [MsgContract] ******/
CREATE CONTRACT [MsgContract] AUTHORIZATION [dbo] ([SampleMsgType] SENT BY INITIATOR)
GO

/****** Object: ServiceQueue [dbo].[Queue1] ******/
CREATE QUEUE [dbo].[Queue1] WITH STATUS = ON , RETENTION = OFF , POISON_MESSAGE_HANDLING (STATUS = ON) ON [PRIMARY]
GO

/****** Object: BrokerService [MsgService] ******/
CREATE SERVICE [MsgService] AUTHORIZATION [dbo] ON QUEUE [dbo].[Queue1] ([MsgContract])
GO

/****** Object: StoredProcedure [dbo].[SendMsg] ******/
CREATE PROC [dbo].[SendMsg]
@msg NVARCHAR(MAX)
AS

BEGIN
SET NOCOUNT ON
DECLARE @handle UNIQUEIDENTIFIER

BEGIN TRANSACTION
BEGIN dialog @handle
FROM SERVICE [MsgService]
TO SERVICE 'MsgService'
ON CONTRACT [MsgContract]
WITH ENCRYPTION = OFF, LIFETIME = 20

;SEND ON CONVERSATION @handle
MESSAGE TYPE [SampleMsgType] (@msg)
END CONVERSATION @handle
COMMIT TRANSACTION
END

/****** Object: StoredProcedure [dbo].[ReceiveMsg] ******/
CREATE PROC [dbo].[ReceiveMsg]
@msg NVARCHAR(MAX) OUT

AS

BEGIN
SET NOCOUNT ON
DECLARE @handle UNIQUEIDENTIFIER

DECLARE @msgTable TABLE (
handle UNIQUEIDENTIFIER,
msg NVARCHAR(MAX),
msgType VARCHAR(300));

SET @handle = NULL

WAITFOR (
RECEIVE [conversation_handle], message_body, message_type_name
FROM [dbo].[Queue1]
INTO @msgTable
), TIMEOUT 25000

SELECT @handle = handle
FROM @msgTable
WHERE msgType = 'http://schemas.microsoft.com/SQL/ServiceBroker/EndDialog'

IF @handle is not null
BEGIN
END CONVERSATION @handle
END

SELECT @msg = msg
FROM @msgTable
WHERE msgType = 'SampleMsgType'
END

GO

最佳答案

对话生命周期可以在 BEGIN DIALOG 期间指定。声明:

BEGIN DIALOG @handle
FROM SERVICE [...]
TO SERVICE '...'
ON CONTRACT [...]
, LIFETIME = <dialog lifetime>;

对话框必须在其生命周期内完成(双方都结束),否则会出错。因此,您可以启动一个生命周期为 2 分钟的对话,并在其上发送一条或多条消息。目标必须在这 2 分钟内接收并处理消息,否则对话框将出错。

您也可以使用conversation priorities并在更高优先级的 channel 上发送时间敏感消息。这将确保 Service Broker 传输优先考虑您的敏感消息,并且客户端应用程序 RECEIVE 语句“冒泡”优先级较高的时间敏感消息,并且它们首先被接收。

关于SQL Server 服务代理 - 消息超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7225067/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com