Share via


Clearing all messages from a queue - for automated unit test

Question

Monday, June 4, 2007 3:13 PM

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

All replies (2)

Monday, June 4, 2007 4:13 PM ✅Answered | 3 votes

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.


Monday, June 4, 2007 10:33 PM

Thank you, Eugen