Share via


Getting to know SQL 2005 Service Broker

Looks likes I've got to do a presentation on Service Broker for the SQL 2005 launch - so I've been doing some home work on the subject.

The best primer I've found is https://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnsql90/html/sqlsvcbroker.asp?frame=true this is refernenced in the webcast https://msevents.microsoft.com/cui/WebCastEventDetails.aspx?EventID=1032263311&EventCategory=5&culture=en-us&CountryCode=US

Once you've got your head round the basics you'll want to create your ow broker service and queues.  Best I've found is "Setting Up a Service Broker Service and Queue" from SQL Server 2005 Books on line at ms-help://MS.SQLCC.v9/MS.SQLSVR.v9.en/smo9/html/f6f1c8b5-393b-48f2-a2b0-184fef683b5c.htm.  Problem with this is there's a bug in the code.  I can't remember the delta between what's printed and what I have working, but my suggestion is to use the code below to create the TargetStoredProcedure and then comment out the sp.Create(); line in the sample code with //.

Hope you enjoy this new technology - if I discover some new usefulties (a new word I've just invented) in this area, I'll post them in my new 'Service Broker' category

USE [AdventureWorks]
GO
/****** Object:  StoredProcedure [dbo].[TargetStoredProcedure]    Script Date: 09/12/2005 23:17:28 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create procedure dbo.TargetStoredProcedure as
DECLARE @message_body varbinary(MAX);
DECLARE @message_type_name nvarchar(128);
DECLARE @conversation_handle uniqueidentifier;
DECLARE @message_iduniqueidentifier uniqueidentifier;
DECLARE @message nvarchar(max);
DECLARE @MessageType nvarchar(max);
Declare @message_id nvarchar(max);

WHILE (1 = 1)
BEGIN
BEGIN TRY
BEGIN TRANSACTION;
WAITFOR (

RECEIVE top(1)
@message_type_name = message_type_name,
@message = message_body,
@conversation_handle = conversation_handle,
@message_id = message_id

FROM TargetQueue
), TIMEOUT 500

IF (@@ROWCOUNT = 0)
BEGIN
ROLLBACK TRANSACTION
BREAK
END;
select @message = N'<Hello>Pong</Hello>';
set @MessageType = '//microsoft.com/ssbdemo/PongMessageType';
SEND ON CONVERSATION @conversation_handle
MESSAGE TYPE @MessageType
(@message);
END CONVERSATION @conversation_handle
COMMIT TRANSACTION
END TRY
BEGIN CATCH
print @@error;
ROLLBACK
CONTINUE
END CATCH
END;
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF