SQL Server Service Borker 1

1、消息类型定义:

  消息类型,是信息交换的模板、create message type message_type_name validattion = well_formed_xml;

2、约定定义:

  约定,指示任务使用的消息 create contract contract_name (message_type_name sent by initiator |target | all [....]);

3、队列定义:

  队列是信息的集合: create queue queue_name with status = on;

4、服务定义:

  服务定义端口用来把消息绑定到一个或多个约定上 create service service_name on queue queue_name(contract_name_list);

例子:

create database bookstore;
create database bookdistribution;

----时间 执行者 目的

----2014-10-30 蒋乐 创建两个数据库用于测试 service broker!
go

use bookstore;

alter database bookstore
set enable_broker;

alter database bookstore
set trustworthy on;

create master key
encryption by password ='123456';

----时间 执行者 目的

----2012-10-30 蒋乐 配制数据库的 3(enable_broker rustworthymaster key) 个选项使它支持 service borker
go

use bookdistribution;

alter database bookdistribution
set enable_broker;

alter database bookdistribution
set trustworthy on;

create master key
encryption by password = '123456';


----时间 执行者 目的

----2012-10-30 蒋乐 配制数据库的 3(enable_broker rustworthymaster key) 个选项使它支持 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);

----时间 执行者 目的

----2012-10-30 蒋乐 定义消息类型与约定、(注意在两个相互通信的数据库中
--要有一样的消息类型与约定才可以通信。
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);

----时间 执行者 目的

----2012-10-30 蒋乐 定义消息类型与约定。
go


use bookstore;

create queue [send_to_bookdistribution_queue]
with
status = on;


----时间 执行者 目的

----2012-10-30 蒋乐 定义队列
go

use bookdistribution;

create queue [send_to_bookdistribution_queueBookDistribution]
with
status = on;

----时间 执行者 目的

----2012-10-30 蒋乐 定义队列
go


use bookstore;

create service [send_to_distribution_service_bookstore]
on queue send_to_bookdistribution_queue(send_to_distribution_contract);

----时间 执行者 目的

----2012-10-30 蒋乐 定义服务
go


use bookdistribution

create service [send_to_distribution_service_bookdistribution]
on queue send_to_bookdistribution_queueBookDistribution(send_to_distribution_contract);


----时间 执行者 目的

----2012-10-30 蒋乐 定义服务
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' -- ! _ ! -- :因为它定义在别的数据库中、所以不可以用名字。要用字符串。
on contract send_to_distribution_contract;

set @message = '<Person>11436101</Person>';

send on conversation @handle message type send_to_distribution
(@message);


----时间 执行者 目的

----2012-10-30 蒋乐 发送一个异步的消息
go


use bookdistribution;

select * from dbo.send_to_bookdistribution_queueBookDistribution;

----时间 执行者 目的

----2012-10-30 蒋乐 查看发送过来的消息
go

原文地址:https://www.cnblogs.com/JiangLe/p/4063504.html