我读到的每个地方都说服务代理处理的消息按照它们到达的顺序进行处理,然而如果你创建了一个表,消息类型,合同,服务等,并且在激活时有一个等待2的存储过程秒并将msg插入表中,将最大队列读取器设置为5或10,并发送20条奇怪的消息,即使我将它们插入队列并查看队列的内容我可以看到消息都是正确的顺序。
是否由于延迟等待等待最近的第二个并且每个线程具有不同的亚秒时间然后争取锁定等等?
我有延迟的原因是模拟连接延迟等
由于
演示代码:
--create the table and service broker
CREATE TABLE test
(
id int identity(1,1),
contents varchar(100)
)
CREATE MESSAGE TYPE test
CREATE CONTRACT mycontract
(
test sent by initiator
)
GO
CREATE PROCEDURE dostuff
AS
BEGIN
DECLARE @msg varchar(100);
RECEIVE TOP (1) @msg = message_body FROM myQueue
IF @msg IS NOT NULL
BEGIN
WAITFOR DELAY '00:00:02'
INSERT INTO test(contents)values(@msg)
END
END
GO
ALTER QUEUE myQueue
WITH STATUS = ON,
ACTIVATION (
STATUS = ON,
PROCEDURE_NAME = dostuff,
MAX_QUEUE_READERS = 10,
EXECUTE AS SELF
)
create service senderService
on queue myQueue
(
mycontract
)
create service receiverService
on queue myQueue
(
mycontract
)
GO
--**********************************************************
--now insert lots of messages to the queue
DECLARE @dialog_handle uniqueidentifier
BEGIN DIALOG @dialog_handle
FROM SERVICE senderService
TO SERVICE 'receiverService'
ON CONTRACT mycontract;
SEND
ON CONVERSATION @dialog_handle
MESSAGE TYPE test
('<test>1</test>');
BEGIN DIALOG @dialog_handle
FROM SERVICE senderService
TO SERVICE 'receiverService'
ON CONTRACT mycontract;
SEND
ON CONVERSATION @dialog_handle
MESSAGE TYPE test
('<test>2</test>')
BEGIN DIALOG @dialog_handle
FROM SERVICE senderService
TO SERVICE 'receiverService'
ON CONTRACT mycontract;
SEND
ON CONVERSATION @dialog_handle
MESSAGE TYPE test
('<test>3</test>')
BEGIN DIALOG @dialog_handle
FROM SERVICE senderService
TO SERVICE 'receiverService'
ON CONTRACT mycontract;
SEND
ON CONVERSATION @dialog_handle
MESSAGE TYPE test
('<test>4</test>')
BEGIN DIALOG @dialog_handle
FROM SERVICE senderService
TO SERVICE 'receiverService'
ON CONTRACT mycontract;
SEND
ON CONVERSATION @dialog_handle
MESSAGE TYPE test
('<test>5</test>')
BEGIN DIALOG @dialog_handle
FROM SERVICE senderService
TO SERVICE 'receiverService'
ON CONTRACT mycontract;
SEND
ON CONVERSATION @dialog_handle
MESSAGE TYPE test
('<test>6</test>')
BEGIN DIALOG @dialog_handle
FROM SERVICE senderService
TO SERVICE 'receiverService'
ON CONTRACT mycontract;
SEND
ON CONVERSATION @dialog_handle
MESSAGE TYPE test
('<test>7</test>')
答案 0 :(得分:3)
要保持相同的顺序,必须在同一个会话中发送消息。如果您为每条消息生成一个对话(如大多数示例所示),那么无法保证它们的交付顺序。
每次使用BEGIN DIALOG语句时,您都会创建一个新会话。只做一次,然后在同一个对话中发送所有消息,你就会得到你期望的序列。