I have an SSBS queue and a main process send M messages to the queue. There are N sub-processes which fetch the messages one by one and process them. The sub-processes will exit after all the messages are processed. Right now I am doing the following,
Approach 1:
The main process send N “EndOfData” after sent M messages. However it doesn’t work well since some sub-processes may receive more than one “EndOfData” messages so some sub-processes will never get the message.
Approach 2: (extend approach 1)
Assign each sub-process an ID, also embed an ID in the “EndOfData” message. The sub-process rollback if the ID in the message doesn’t match its ID. However, it causes “poise message” issue because of too many rollback and the queue get disable.
begin tran
begin try
WAITFOR(
RECEIVE TOP(1)
@MessageType = message_type_name,
@MessageBody = CAST(message_body AS xml)
FROM
TargetQueue
) , TIMEOUT 1000
if @MessageType = 'EndOfData' and
@MessageBody.value('(//ID/text())[1]', 'int') <> @ID
BEGIN
rollback tran
waitfor delay '00:00:02'
END
Is there a good way to implement it?
Update:
The sub-processes will do the following steps.
- Receive one message
- If “Work”,
- process the message (may take several minutes)
- Send a “Info” message to the queue to info main process the message has been processed
- If “EndOfData”,
- Send a message “Exit” to main process
- Receive the
https://..../EndDialogof “Exist” message and exit
- If
https://.../EndDialogof messages sent in 2.2, just receive it
Using the following code and put the working in the transaction. So the script block can exit after receive the
EndOfDatamessage.http://msdn.microsoft.com/en-us/library/microsoft.sqlserver.management.common.serverconnection.committransaction.aspx