SQL Server 2005 Express Edition - Part 32 - Distributed Service Broker Environment - Conducting DialogsOctober 10, 2008 In the recent articles of our series covering features available in SQL
Server 2005 Express Edition, we have introduced asynchronous, queue-based
messaging functionality offered by the Service Broker component. So far, in
addition to providing a general overview of its characteristics, we have
described a simplified example of a conversation between two services residing
in the same database. We also have listed the initial steps necessary to
facilitate an equivalent setup in a distributed environment, including creation
of endpoints and routes (in addition to message types, queues, and services,
identical to the ones we have used earlier), as well as assigning required
permissions to As you might recall from our earlier discussion, a Service Broker
conversation starts with the -- on srvExp01 USE dbSBExp01 GO DECLARE @convHandle uniqueidentifier BEGIN DIALOG CONVERSATION @convHandle FROM SERVICE [//databaseJournal.com/SQL2005EX/ServiceBroker/svcSend] TO SERVICE '//databaseJournal.com/SQL2005EX/ServiceBroker/svcRecv' ON CONTRACT [//databaseJournal.com/SQL2005EX/ServiceBroker/contAnymsgNV] WITH ENCRYPTION=OFF; Once the conversation is established and identifiable via its handle (whose value
you can determine by querying the content of the -- on srvExp01 USE dbSBExp01 GO DECLARE @convHandle uniqueidentifier SELECT @convHandle = conversation_handle FROM sys.conversation_endpoints WHERE state = 'SO' AND far_service = '//databaseJournal.com/SQL2005EX/ServiceBroker/svcRecv'; SEND ON CONVERSATION @convHandle MESSAGE TYPE [//databaseJournal.com/SQL2005EX/ServiceBroker/msgNV] (N'Aloha bruddah') Assuming that you have successfully implemented all previous steps, the
message should arrive at the destination queue -- on srvEnt01 USE dbSBEnt01 GO DECLARE @convHandle UNIQUEIDENTIFIER; DECLARE @msgTypeName SYSNAME; DECLARE @status TINYINT; DECLARE @srvName NVARCHAR(512); DECLARE @srvConName NVARCHAR(256); DECLARE @msgTypeValidation AS NCHAR(2); DECLARE @msgBody NVARCHAR(50); DECLARE @cmd AS NVARCHAR(50); WAITFOR(RECEIVE TOP(1) @convHandle = conversation_handle, @msgTypeName = message_type_name, @status = status, @srvName = service_name, @srvConName = service_contract_name, @msgTypeValidation = validation, @msgBody = CAST(message_body AS NVARCHAR(50)) FROM qRecv), TIMEOUT 1000; IF(@@rowcount != 0) BEGIN INSERT INTO tbMsgs(convHandle, msgTypeName, status, srvName, srvConName, msgTypeValidation, msgBody) VALUES(@convHandle, @msgTypeName, @status, @srvName, @srvConName, @msgTypeValidation, @msgbody) END CONVERSATION @convHandle; END END If you check the content of the -- back on srvExp01
USE dbSBExp01
GO
DECLARE @convHandle UNIQUEIDENTIFIER;
DECLARE @msgTypeName SYSNAME;
DECLARE @status TINYINT;
DECLARE @srvName NVARCHAR(512);
DECLARE @srvConName NVARCHAR(256);
DECLARE @msgTypeValidation AS NCHAR(2);
DECLARE @msgBody NVARCHAR(50);
DECLARE @cmd AS NVARCHAR(50);
WAITFOR(RECEIVE TOP(1)
@convHandle = conversation_handle,
@msgTypeName = message_type_name,
@status = status,
@srvName = service_name,
@srvConName = service_contract_name,
@msgTypeValidation = validation,
@msgBody = CAST(message_body AS NVARCHAR(50))
FROM qSend),
TIMEOUT 1000;
IF(@@rowcount != 0)
BEGIN
INSERT INTO
tbMsgs(convHandle, msgTypeName, status, srvName, srvConName, msgTypeValidation, msgBody)
VALUES(@convHandle, @msgTypeName, @status, @srvName, @srvConName, @msgTypeValidation, @msgbody)
END CONVERSATION @convHandle;
END
As the result of this process, both messages should be removed from their
respective queues (and inserted into our auxiliary tables). However, you should
still be able to locate an entry associated with the conversation we just
completed in the In the next article of our series, we will start an overview of more elaborate examples of Service Broker dialogs, involving such features as certificate-based encryption and authentication. |