Friday, February 10, 2012

Clearing all messages from a queue - for automated unit test

Hi,

I am developing automated .Net Unit Tests, and as a prerequisite of each test, I would like to clear the service broker queues of any messages. Executing the

RECEIVE * FROM statement appears to only return a message at a time, and not all as I expected. Any ideas on how to make this happen, besides not having to delete the queues and then having to rebuild them?

Thanks,Eugen

You can loop through and end end all your conversations with cleanup. If you do this it will clear down all the messages from the all queues regardless as to their state - it will also clearout your conversation endpoints and transmission queue. It will clear out all messages on all queues (on that database) so be a bit careful. An example to clear all messages on all queues is:

declare @.conversationHandle uniqueidentifier

select top 1 @.conversationHandle = conversation_handle from sys.conversation_endpoints

while @.@.rowcount = 1

begin

end conversation @.conversationHandle with cleanup

select top 1 @.conversationHandle = conversation_handle from sys.conversation_endpoints

end

You can constrain the select statement on any of the columns from sys.sonversation_endpoints (say service_id or far_service) to give yourself some discretion on what is removed - and as I said earlier, be careful. Though if it is a test system you might be ok.|||Thank you, Eugen

No comments:

Post a Comment