Sunday, May 18, 2008

USP_SendBrokerMessage

Procedure to tidy sending a service broker message -
CREATE PROCEDURE [dbo].[USP_SendBrokerMessage] 
 @SourceService NVARCHAR(200),
 @TargetService NVARCHAR(200),
 @MessageContract NVARCHAR(200),
 @MessageType NVARCHAR(200),
 @MessageContent NVARCHAR(4000)

AS

BEGIN

-- Sending a Service Broker Message
DECLARE @InitDlgHandle UNIQUEIDENTIFIER;
DECLARE @RequestMsg NVARCHAR(4000);

SET NOCOUNT ON

BEGIN TRANSACTION;

 BEGIN DIALOG @InitDlgHandle
   FROM SERVICE @SourceService
   TO SERVICE @TargetService 
   ON CONTRACT @MessageContract
   WITH ENCRYPTION = OFF;

 SELECT @RequestMsg = @MessageContent;

 SEND ON CONVERSATION @InitDlgHandle
   MESSAGE TYPE @MessageType
    (@RequestMsg);

 --Uncomment next line to debug >
 --SELECT @RequestMsg AS SentRequestMsg;

COMMIT TRANSACTION;

END
GO

Usage :
exec USP_SendBrokerMessage
@SourceService = 'AdventureWorksSourceQueueSourceService',
@TargetService = N'AdventureWorksTargetQueueTargetService',
@MessageContract = 'http://www.servicebroker.com/Contract',
@MessageType = 'http://www.servicebroker.com/SendType',
@MessageContent =  N'Initial Service Broker Message from AdventureWorks'

No comments: