Everywhere I read, it says that messages processed by the service broker are processed in the order in which they are received, and yet, if you create a table, message type, contract, service, etc., and upon activation have a saved process that waits 2 seconds and inserts msg into the table, set the maximum queue readers to 5 or 10 and send 20 odd messages that I see in the table, which they are inserted out of order, even if I insert them into the queue and look at the contents queue I see that all messages are in the correct order.
Is this due to a delay in waiting for the next second, and each thread has different time intervals, and then fights for a lock or something else?
The reason I have a delay is to simulate delays with joins, etc.
thank
demo code:
--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>')
source
share