SSB(SQLservice Service Broker) 入门实例介绍
step 1:perpar database & data
use master
go
create database inventory
go
use inventory
go
create table inventory
(
titleid nvarchar(6) not null,
quantity int not null,
primary key (titleid,quantity)
);
go
insert inventory values ('pc1001',100);
insert inventory values ('pc1002',200);
insert inventory values ('pc1003',300);
insert inventory values ('pc1004',400);
step 2: 创建整个ssb的基础框架.
首先创建信息类型
create message type [//mark/v10/types/inventory]
create message type [//mark/v10/types/inventoryresponse]
创建合约
create contracts [//mark/v10/contracts/inventorycontract]
(
[//mark/v10/types/inventory] send by initiator,
[//mark/v10/types/inventoryresponse] send by target
);
创建队列与服务
create queue [inventory client queue];
create service [//mark/v10/services/inventory client]
on queue [inventory client queue]
go
--create inventory service
create queue [inventory queue];
create service [//mark/v10/services/inventory]
on queue [inventory queue]
(
[//mark/v10/contracts/inventorycontract]
);
step 3:发送消息到消息队列中
use inventory
go
--send message
begin transaction
declare @dialog_id uniqueidentifier
begin dialog conversation @dialog_id
from service [//mark/v10/services/inventory client]
to service '//mark/v10/services/inventory'
on contract [//mark/v10/contracts/inventorycontract]
with encryption = off;
send on conversation @dialog_id message type [//mark/v10/types/inventory]
(
'<inventoryupdate>
<titleid>pc1001</titleid>
<quantity>102</quantity>
</inventoryupdate>'
);
commit transaction;
发送成功后,可以查询 inventory_queue
select conversation_handle, cast(message_body as xml) as conversation_body from [inventory queue]
这里只是查询了两列。
其中conversation_body 就是 我们发出的信息了。
setp 4: 从 inventory queue 中获取信息并更新数据
use inventory
go
begin transaction
declare @dialog_id uniqueidentifier
declare @message_body xml
declare @amount int;
declare @title nvarchar(128);
receive
@dialog_id = conversation_handle,
@message_body = message_body
from [dbo].[inventory queue]
print 'handle is' + cast(@dialog_id as nvarchar(90))
print 'message body is' + cast(@message_body as nvarchar(max) )
if(@dialog_id is not null)
begin
set @amount = @message_body.value('(/inventoryupdate/quantity)[1]','int');
set @title = @message_body.value('(/inventoryupdate/titleid)[1]','nvarchar(100)');
print 'a:'+cast(@amount as nvarchar(100))
print 'b:'+@title;
update inventory set quantity = @amount where titleid = @title;
end
end conversation @dialog_id;
commit transaction;
操作成功后再去查询inventory queue的表 犹豫里面的数据已经被receive 出来 就为空了。
数据同时被更新了
step 5: 接下来做个自动监听功能,只要有信息发送到队列,相关队列,就可以自动执行功能.
首先建立个存储过程,(把receive 中的那段代码拿过来就可以了)
use inventory
go
--create procedure to receive message and update table
create proc inventoryproc as
begin transaction
declare @dialog_id uniqueidentifier
declare @message_body xml
declare @amount int;
declare @title nvarchar(128);
waitfor(
receive
@dialog_id = conversation_handle,
@message_body = message_body
from [dbo].[inventory queue]),timeout 5000;
print 'handle is' + cast(@dialog_id as nvarchar(90))
print 'message body is' + cast(@message_body as nvarchar(max) )
if(@dialog_id is not null)
begin
set @amount = @message_body.value('(/inventoryupdate/quantity)[1]','int');
set @title = @message_body.value('(/inventoryupdate/titleid)[1]','nvarchar(100)');
print 'a:'+cast(@amount as nvarchar(100))
print 'b:'+@title;
update inventory set quantity = @amount where titleid = @title;
end
end conversation @dialog_id;
commit transaction;
go
队列监听
alter queue [inventory queue]
with activation(
status = on,
procedure_name= inventoryproc,
max_queue_readers=2,
execute as self
);
go
完成后,只要发送队列信息后,就可以了
ps: 我们定义了 两个队列 还有个 inventory client queue
这个队列保存的是成功或者error 提示。用receive 方法也可以提取出来。
select message_type_name from [inventory client queue]
以上的 message type service 等信息 如图所示,都在这里.
以上是ssb的入门实例,以后会再写一篇实际应用上的(以上的sql code 都是纯手工输入,可能还存在错误(虽然校验了两遍),如果发生错误,对不起大家了。)