Thursday 16 July 2009

Simple Queuing Processing - TSQL


-- Create Objects
------------------------------------------------------------
CREATE MESSAGE TYPE [mt_HelloWorld] VALIDATION = WELL_FORMED_XML

Create CONTRACT [c_HelloWorld]
(
[mt_HelloWorld] SENT BY INITIATOR
)

CREATE QUEUE [q_HelloWorld]

CREATE SERVICE [s_HelloWorld] ON QUEUE [q_HelloWorld]
(
[c_HelloWorld]
)


--Queue Message
--------------------------------------------------------------
BEGIN TRANSACTION
DECLARE @conversationHandle uniqueidentifier

BEGIN DIALOG @conversationHandle
FROM SERVICE [s_HelloWorld]
TO SERVICE 's_HelloWorld'
ON CONTRACT [c_HelloWorld]
WITH ENCRYPTION = OFF;

SEND ON CONVERSATION @conversationHandle
MESSAGE TYPE [mt_HelloWorld]
(
CAST(N'Hello world request' AS XML)
)
--END CONVERSATION @conversationHandle;
COMMIT


-- Check the Queue
--------------------------------------------------------------
select CAST(message_body as xml) Message, * from q_helloworld



--Recieve from Queue
--------------------------------------------------------------
DECLARE @message_body nvarchar(MAX)
DECLARE @message_type_name sysname;
--Declare @conversationHandle uniqueIdentifier
BEGIN TRANSACTION;

RECEIVE TOP(1)
--@conversationHandle = conversation_handle,
@message_type_name = message_type_name,
@message_body = message_body
FROM [q_HelloWorld]
--if (@conversationHandle is not null) END CONVERSATION @conversationHandle;
Commit

select @message_type_name MessageType, cast(@message_body as xml) Message
-- Check the Queue
--------------------------------------------------------------
select CAST(message_body as xml) Message, * from q_helloworld

No comments:

Post a Comment