Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Service Broker (2005)
 Sample Code But not workng

Author  Topic 

sg2255551
Constraint Violating Yak Guru

274 Posts

Posted - 2007-02-21 : 11:15:22
hi

i have a sample code but when i query on the queue is empty and i do not know why?


IF NOT EXISTS
(SELECT * FROM sys.databases
WHERE name = 'AdventureWorks'
AND is_broker_enabled = 1)
BEGIN
ALTER DATABASE AdventureWorks SET ENABLE_BROKER ;
END



-- Create the XML SBSampleMessage message type
CREATE MESSAGE TYPE SBSampleMessage
VALIDATION = WELL_FORMED_XML ;
GO
-- Create the SBSampleContract contract
CREATE CONTRACT SBSampleContract
( SBSampleMessage SENT BY INITIATOR);
GO
-- Create the queue
CREATE QUEUE [dbo].[ReceiverQueue];
GO
-- Create the queue for the Sender service
CREATE QUEUE [dbo].[SenderQueue];
GO
-- Create the service
CREATE SERVICE SenderService
ON QUEUE [dbo].[SenderQueue];
GO
-- Create the target service
CREATE SERVICE ReceiverService
ON QUEUE [dbo].[ReceiverQueue]
(SBSampleContract);
GO

--
--SELECT * FROM dbo.ReceiverQueue
--Select * from SenderQueue
--Select * from dbo.ServiceBrokerQueue

USE AdventureWorks ;
GO
-- Begin a transaction
BEGIN TRANSACTION ;
GO
-- Declare a variable for the message
DECLARE @SBmessage XML ;
SET @SBmessage = N'<message>Service Broker is Cool</message>' ;
-- Declare a variable for the conversation ID
DECLARE @conversationID UNIQUEIDENTIFIER ;
-- Begin a dialog between the services
BEGIN DIALOG CONVERSATION @conversationID
FROM SERVICE SenderService
TO SERVICE 'ReceiverService'
ON CONTRACT SBSampleContract ;
-- Put the message on the queue
SEND ON CONVERSATION @conversationID
MESSAGE TYPE SBSampleMessage
(@SBmessage) ;
-- End the conversation
END CONVERSATION @conversationID ;
GO
-- Commit the transaction to send the message
COMMIT TRANSACTION ;
GO



USE AdventureWorks ;
GO
SELECT * FROM [dbo].[ReceiverQueue]

USE AdventureWorks ;
GO
SELECT CAST(message_body as nvarchar(MAX)) from ReceiverQueue
   

- Advertisement -