You need to create a Service Broker solution. Which object should you create first?
A. Contract
B. Dialog
C. Message Type
D. Services
Correct Answer: C
Explanation/Reference:
The basic steps involved in creating any Service Broker application include:
1. Create message types that define the messages your service will use to accomplish any required communication. A message type defines the name of a specific kind of message and the validation that Service Broker performs on that kind of message. To determine the message types that your application will use, you first plan out the tasks that your application must perform, and the data that is necessary to perform each task.
The most common approach for an application is to structure the messages so that each message includes the information required for one step of the task. When each message contains the information for one step of the task, the application can easily receive the message, complete the step, and send a response within a single transaction. Therefore, for many applications, the easiest way to determine the message types and the content for the message is to determine the transaction boundaries for the tasks performed by the application. Each distinct step is a transaction, and each transaction corresponds to a message type exchanged between the services. Status information, results, or output are also message types.
Example:
--Create the message types
CREATE MESSAGE TYPE
[//AWDB/1DBSample/RequestMessage]
VALIDATION = WELL_FORMED_XML;
CREATE MESSAGE TYPE
[//AWDB/1DBSample/ReplyMessage]
VALIDATION = WELL_FORMED_XML;
GO
2. Create one or more contracts that define the structure of the conversations in which this service may participate. Contracts define the name of a specific business task and list the message types used in that task. For each task that the service performs, create a contract that includes the message types for each step in the task.
Example:
--Create the contract
CREATE CONTRACT [//AWDB/1DBSample/SampleContract]
([//AWDB/1DBSample/RequestMessage]
SENT BY INITIATOR,
SENT BY TARGET
);
GO
3. Create a queue. Service Broker uses this queue to receive and store incoming messages for the service. To simplify processing, applications typically create one queue per service instead of using the same queue for multiple services.
Example:
--Create the target and initiator queues
CREATE QUEUE TargetQueue1DB;
CREATE QUEUE InitiatorQueue1DB;
GO
4. Create a service. The service definition specifies the queue that the service uses and the contracts for which this service is the target. A service represents a business process as a distinct set of tasks. Each contract within the service represents a specific task. A service can specify more than one contract, and a contract can be used by more than one service.
Each service uses a queue to store messages. Messages sent to the service are delivered to the queue. In general, applications are easiest to implement when only one service uses a given queue. However, for flexibility, Service Broker allows multiple services to specify the same queue. In this case, the application either treats all messages of the same type the same way, or inspects both the message type name and the service name to determine how to process the message. This strategy can be convenient when an application supports multiple versions of the same service.
Example:
--Create the target and initiator services
CREATE SERVICE
[//AWDB/1DBSample/TargetService]
ON QUEUE TargetQueue1DB
([//AWDB/1DBSample/SampleContract]);
GO
--Because no contract name is specified, no other services can use this service as a target service.
CREATE SERVICE
[//AWDB/1DBSample/InitiatorService]
ON QUEUE InitiatorQueue1DB;
GO
5. Send and receive messages
Example:
--Begin a conversation and send a request message
DECLARE @InitDlgHandle UNIQUEIDENTIFIER;
DECLARE @RequestMsg NVARCHAR(100);
BEGIN TRANSACTION;
BEGIN DIALOG @InitDlgHandle
FROM SERVICE
[//AWDB/1DBSample/InitiatorService]
TO SERVICE
N’//AWDB/1DBSample/TargetService’
ON CONTRACT
[//AWDB/1DBSample/SampleContract]
WITH
ENCRYPTION = OFF;
SELECT @RequestMsg =
N'<RequestMsg>Message for Target service.</RequestMsg>’;
SEND ON CONVERSATION @InitDlgHandle
MESSAGE TYPE
[//AWDB/1DBSample/RequestMessage]
(@RequestMsg);
--SELECT @InitDlgHandle AS InitDlgHandle;
--SELECT @RequestMsg AS SentRequestMsg;
COMMIT TRANSACTION;
GO
--Receive the request and send a reply
DECLARE @RecvReqDlgHandle UNIQUEIDENTIFIER;
DECLARE @RecvReqMsg NVARCHAR(100);
DECLARE @RecvReqMsgName sysname;
BEGIN TRANSACTION;
WAITFOR
( RECEIVE TOP(1)
@RecvReqDlgHandle = conversation_handle, @RecvReqMsg = message_body,
@RecvReqMsg = message_body, @RecvReqMsgName = message_type_name
FROM TargetQueue1DB
), TIMEOUT 1000;
--SELECT @RecvReqMsg AS ReceivedRequestMsg;
IF @RecvReqMsgName =
N’//AWDB/1DBSample/RequestMessage’
BEGIN
DECLARE @ReplyMsg NVARCHAR(100);
SELECT @ReplyMsg =
N'<ReplyMsg>Message for Initiator service.</ReplyMsg>’;
SEND ON CONVERSATION @RecvReqDlgHandle
MESSAGE TYPE
[//AWDB/1DBSample/ReplyMessage]
(@ReplyMsg);
END CONVERSATION @RecvReqDlgHandle;
--SELECT @ReplyMsg AS SentReplyMsg;
END
COMMIT TRANSACTION;
GO
--Receive the reply and end the conversation
DECLARE @RecvReplyMsg NVARCHAR(100);
DECLARE @RecvReplyDlgHandle UNIQUEIDENTIFIER;
BEGIN TRANSACTION;
WAITFOR
( RECEIVE TOP(1)
@RecvReplyDlgHandle = conversation_handle, @RecvReplyMsg = message_body
FROM InitiatorQueue1DB
), TIMEOUT 1000;
END CONVERSATION @RecvReplyDlgHandle;
--SELECT @RecvReplyMsg AS ReceivedReplyMsg;
COMMIT TRANSACTION;
GO
Explanation/Reference:
The basic steps involved in creating any Service Broker application include:
1. Create message types that define the messages your service will use to accomplish any required communication. A message type defines the name of a specific kind of message and the validation that Service Broker performs on that kind of message. To determine the message types that your application will use, you first plan out the tasks that your application must perform, and the data that is necessary to perform each task.
The most common approach for an application is to structure the messages so that each message includes the information required for one step of the task. When each message contains the information for one step of the task, the application can easily receive the message, complete the step, and send a response within a single transaction. Therefore, for many applications, the easiest way to determine the message types and the content for the message is to determine the transaction boundaries for the tasks performed by the application. Each distinct step is a transaction, and each transaction corresponds to a message type exchanged between the services. Status information, results, or output are also message types.
Example:
--Create the message types
CREATE MESSAGE TYPE
[//AWDB/1DBSample/RequestMessage]
VALIDATION = WELL_FORMED_XML;
CREATE MESSAGE TYPE
[//AWDB/1DBSample/ReplyMessage]
VALIDATION = WELL_FORMED_XML;
GO
2. Create one or more contracts that define the structure of the conversations in which this service may participate. Contracts define the name of a specific business task and list the message types used in that task. For each task that the service performs, create a contract that includes the message types for each step in the task.
Example:
--Create the contract
CREATE CONTRACT [//AWDB/1DBSample/SampleContract]
([//AWDB/1DBSample/RequestMessage]
SENT BY INITIATOR,
SENT BY TARGET
);
GO
3. Create a queue. Service Broker uses this queue to receive and store incoming messages for the service. To simplify processing, applications typically create one queue per service instead of using the same queue for multiple services.
Example:
--Create the target and initiator queues
CREATE QUEUE TargetQueue1DB;
CREATE QUEUE InitiatorQueue1DB;
GO
4. Create a service. The service definition specifies the queue that the service uses and the contracts for which this service is the target. A service represents a business process as a distinct set of tasks. Each contract within the service represents a specific task. A service can specify more than one contract, and a contract can be used by more than one service.
Each service uses a queue to store messages. Messages sent to the service are delivered to the queue. In general, applications are easiest to implement when only one service uses a given queue. However, for flexibility, Service Broker allows multiple services to specify the same queue. In this case, the application either treats all messages of the same type the same way, or inspects both the message type name and the service name to determine how to process the message. This strategy can be convenient when an application supports multiple versions of the same service.
Example:
--Create the target and initiator services
CREATE SERVICE
[//AWDB/1DBSample/TargetService]
ON QUEUE TargetQueue1DB
([//AWDB/1DBSample/SampleContract]);
GO
--Because no contract name is specified, no other services can use this service as a target service.
CREATE SERVICE
[//AWDB/1DBSample/InitiatorService]
ON QUEUE InitiatorQueue1DB;
GO
5. Send and receive messages
Example:
--Begin a conversation and send a request message
DECLARE @InitDlgHandle UNIQUEIDENTIFIER;
DECLARE @RequestMsg NVARCHAR(100);
BEGIN TRANSACTION;
BEGIN DIALOG @InitDlgHandle
FROM SERVICE
[//AWDB/1DBSample/InitiatorService]
TO SERVICE
N’//AWDB/1DBSample/TargetService’
ON CONTRACT
[//AWDB/1DBSample/SampleContract]
WITH
ENCRYPTION = OFF;
SELECT @RequestMsg =
N'<RequestMsg>Message for Target service.</RequestMsg>’;
SEND ON CONVERSATION @InitDlgHandle
MESSAGE TYPE
[//AWDB/1DBSample/RequestMessage]
(@RequestMsg);
--SELECT @InitDlgHandle AS InitDlgHandle;
--SELECT @RequestMsg AS SentRequestMsg;
COMMIT TRANSACTION;
GO
--Receive the request and send a reply
DECLARE @RecvReqDlgHandle UNIQUEIDENTIFIER;
DECLARE @RecvReqMsg NVARCHAR(100);
DECLARE @RecvReqMsgName sysname;
BEGIN TRANSACTION;
WAITFOR
( RECEIVE TOP(1)
@RecvReqDlgHandle = conversation_handle, @RecvReqMsg = message_body,
@RecvReqMsg = message_body, @RecvReqMsgName = message_type_name
FROM TargetQueue1DB
), TIMEOUT 1000;
--SELECT @RecvReqMsg AS ReceivedRequestMsg;
IF @RecvReqMsgName =
N’//AWDB/1DBSample/RequestMessage’
BEGIN
DECLARE @ReplyMsg NVARCHAR(100);
SELECT @ReplyMsg =
N'<ReplyMsg>Message for Initiator service.</ReplyMsg>’;
SEND ON CONVERSATION @RecvReqDlgHandle
MESSAGE TYPE
[//AWDB/1DBSample/ReplyMessage]
(@ReplyMsg);
END CONVERSATION @RecvReqDlgHandle;
--SELECT @ReplyMsg AS SentReplyMsg;
END
COMMIT TRANSACTION;
GO
--Receive the reply and end the conversation
DECLARE @RecvReplyMsg NVARCHAR(100);
DECLARE @RecvReplyDlgHandle UNIQUEIDENTIFIER;
BEGIN TRANSACTION;
WAITFOR
( RECEIVE TOP(1)
@RecvReplyDlgHandle = conversation_handle, @RecvReplyMsg = message_body
FROM InitiatorQueue1DB
), TIMEOUT 1000;
END CONVERSATION @RecvReplyDlgHandle;
--SELECT @RecvReplyMsg AS ReceivedReplyMsg;
COMMIT TRANSACTION;
GO