1. Message type definition:
Message type, which is a template for information exchange, create message type message_type_name validattion = well_formed_xml;
2, the agreement definition:
A contract that instructs the task to use the message Create contract contract_name (Message_type_name sent by initiator |target | all [...]);
3. Queue definition:
A queue is a collection of information: Create queue queue_name with status = on;
4. Service Definition:
The service definition port is used to bind messages to one or more conventions on the Create service service_name on queue queue_name (contract_name_list);
Example:
Create DATABASE Bookstore;
Create Database bookdistribution;
----Time Performer Purpose
----2014-10-30 Shang Create two databases to test service broker!
Go
Use bookstore;
ALTER DATABASE Bookstore
Set Enable_broker;
ALTER DATABASE Bookstore
Set trustworthy on;
Create Master Key
Encryption by Password = ' 123456 ';
----Time Performer Purpose
----2012-10-30 Shang Configuration Database 3 (Enable_broker\trustworthy\master key) option enables it to support service Borker
Go
Use bookdistribution;
ALTER DATABASE Bookdistribution
Set Enable_broker;
ALTER DATABASE Bookdistribution
Set trustworthy on;
Create Master Key
Encryption by Password = ' 123456 ';
----Time Performer Purpose
----2012-10-30 Shang Configuration Database 3 (Enable_broker\trustworthy\master key) option enables it to support service Borker
Go
Use bookstore;
Create message type [Send_to_distribution]
validation = Well_formed_xml;
Create message type [send_to_distribution_received]
validation = Well_formed_xml;
Create contract [Send_to_distribution_contract] (
[Send_to_distribution] sent by initiator,
[send_to_distribution_received] sent by target);
----Time Performer Purpose
----2012-10-30 Shang Define message types and conventions, (note in two databases that communicate with each other
--You must have the same message type and convention to communicate.
Go
Use bookdistribution;
Create message type [Send_to_distribution]
validation = Well_formed_xml;
Create message type [send_to_distribution_received]
validation = Well_formed_xml;
Create contract [Send_to_distribution_contract] (
[Send_to_distribution] sent by initiator,
[send_to_distribution_received] sent by target);
----Time Performer Purpose
----2012-10-30 Shang defines message types and conventions.
Go
Use bookstore;
Create queue [Send_to_bookdistribution_queue]
With
status = on;
----Time Performer Purpose
----2012-10-30 Shang definition Queue
Go
Use bookdistribution;
Create queue [Send_to_bookdistribution_queuebookdistribution]
With
status = on;
----Time Performer Purpose
----2012-10-30 Shang definition Queue
Go
Use bookstore;
Create service [Send_to_distribution_service_bookstore]
On queue send_to_bookdistribution_queue (send_to_distribution_contract);
----Time Performer Purpose
----2012-10-30 Shang Definition Service
Go
Use Bookdistribution
Create service [Send_to_distribution_service_bookdistribution]
On queue send_to_bookdistribution_queuebookdistribution (send_to_distribution_contract);
----Time Performer Purpose
----2012-10-30 Shang Definition Service
Go
Use bookstore;
declare @handle uniqueidentifier;
declare @message XML;
Begin dialog Conversation @handle
From service Send_to_distribution_service_bookstore
To service ' send_to_distribution_service_bookdistribution '--! _ ! --: Because it is defined in a different database, you cannot use a name. The string to use.
on contract send_to_distribution_contract;
Set @message = ' <Person>11436101</Person> ';
Send on Conversation @handle message type Send_to_distribution
(@message);
----Time Performer Purpose
----2012-10-30 Shang send an asynchronous message
Go
Use bookdistribution;
SELECT * from Dbo.send_to_bookdistribution_queuebookdistribution;
----Time Performer Purpose
----2012-10-30 Shang view messages sent over
Go
SQL Server Service Borker 1