Service Broker Activation in SQL Server 2005 Express EditionJanuary 26, 2009 In the recent installments our series dedicated to the most prominent features of SQL Server 2005 Express Edition, we have been discussing Service Broker functionality, focusing in particular on methods of securing its conversations. In the examples demonstrating their implementation, we took a somewhat simplified approach, which relied on interactive invocation of individual T-SQL statements in order to carry out each step of a dialog between an initiator and its target. While such simplification, introduced for the purpose of clarity, still allowed us to properly illustrate the security-related topics we were interested in at the time, it prevented us from exploring fully automated, functionally equivalent solutions. We will address this shortcoming in this article by presenting a mechanism, known as activation, which eliminates the need for manual intervention into communication between Service Broker partners. Activation involves associating a Service Broker queue with an arbitrary
code (this can be either a stored procedure in the local database or a separate
application, resulting in either internal or external activation). Its launch
is triggered by an arrival of messages into the queue (or, more accurately,
detection of their presence by a built-in process, known as queue monitor),
ensuring their automatic processing (which is considerably more efficient than
traditional techniques that required developing a custom polling procedure). In
addition, in order to accommodate higher rates of incoming messages, Service
Broker has the ability to initiate additional instances of the same stored
procedure or external program, operating within the context of dedicated queue
reader processes. Analogically, their number is reduced (down to zero, if
appropriate) as the volume of messages waiting in the queue decreases.
Activations and deactivations are handled by queue monitors (created
automatically for each activated queue - as you can confirm by querying As mentioned earlier, depending on the type of action triggered by messages
arriving into a queue, activation can be categorized as either external or
internal. In the case of the former, the implementation requires writing a
custom code, which relies on SQL Server event notifications (alerting about
-- on srvEnt01
USE dbSBEnt01
GO
CREATE PROCEDURE dbo.cspProcessqRecv
AS
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
tbMsgRecv(convHandle, msgTypeName, status, srvName, srvConName, msgTypeValidation, msgBody)
VALUES(@convHandle, @msgTypeName, @status, @srvName, @srvConName, @msgTypeValidation, @msgbody)
END CONVERSATION @convHandle;
END
GO
Once the stored procedure is created, you can reconfigure existing queue
-- on srvEnt01
USE dbSBEnt01
GO
ALTER QUEUE qRecv
WITH ACTIVATION (
STATUS = ON,
PROCEDURE_NAME = dbo.cspProcessqRecv,
MAX_QUEUE_READERS = 1,
EXECUTE AS 'udbSBEnt01'
)
GO
The outcome of this step can be verified by examining values in a row
representing our queue in the In the next article of our series, we will look at additional topics related to Service Broker activation, including specifics regarding security context in which stored procedures invoked in the manner described above are executing. |