SSB(SQLservice Service Broker) 入門實(shí)例介紹
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: 創(chuàng)建整個(gè)SSB的基礎(chǔ)框架.
首先創(chuàng)建信息類型
Create Message Type [//mark/v10/types/inventory]
Create Message Type [//mark/v10/types/inventoryresponse]
創(chuàng)建合約
Create contracts [//mark/v10/contracts/inventorycontract]
(
[//mark/v10/types/inventory] send by initiator,
[//mark/v10/types/inventoryresponse] send by target
);
創(chuàng)建隊(duì)列與服務(wù)
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:發(fā)送消息到消息隊(duì)列中
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;
發(fā)送成功后,可以查詢 inventory_queue
select conversation_handle, cast(message_body as xml) as conversation_body from [Inventory queue]
這里只是查詢了兩列。

其中conversation_body 就是 我們發(fā)出的信息了。
setp 4: 從 inventory queue 中獲取信息并更新數(shù)據(jù)
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的表 猶豫里面的數(shù)據(jù)已經(jīng)被receive 出來(lái) 就為空了。
數(shù)據(jù)同時(shí)被更新了
step 5: 接下來(lái)做個(gè)自動(dòng)監(jiān)聽功能,只要有信息發(fā)送到隊(duì)列,相關(guān)隊(duì)列,就可以自動(dòng)執(zhí)行功能.
首先建立個(gè)存儲(chǔ)過程,(把receive 中的那段代碼拿過來(lái)就可以了)
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
隊(duì)列監(jiān)聽
alter queue [inventory queue]
with activation(
status = on,
procedure_name= InventoryProc,
max_queue_readers=2,
execute as self
);
go
完成后,只要發(fā)送隊(duì)列信息后,就可以了
PS: 我們定義了 兩個(gè)隊(duì)列 還有個(gè) inventory client queue
這個(gè)隊(duì)列保存的是成功或者error 提示。用receive 方法也可以提取出來(lái)。
select message_type_name from [Inventory client queue]
以上的 message type service 等信息 如圖所示,都在這里.
以上是SSB的入門實(shí)例,以后會(huì)再寫一篇實(shí)際應(yīng)用上的(以上的sql code 都是純手工輸入,可能還存在錯(cuò)誤(雖然校驗(yàn)了兩遍),如果發(fā)生錯(cuò)誤,對(duì)不起大家了。)
相關(guān)文章
SQL Server中執(zhí)行動(dòng)態(tài)SQL
本文詳細(xì)講解了SQL Server中執(zhí)行動(dòng)態(tài)SQL的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05Visual?Studio2022連接SQL?Server數(shù)據(jù)庫(kù)的詳細(xì)圖文教程
在visual studio中經(jīng)常會(huì)用到SQL的數(shù)據(jù),關(guān)于數(shù)據(jù)連接總是會(huì)出現(xiàn)一系列的問題,下面這篇文章主要給大家介紹了關(guān)于Visual?Studio2022連接SQL?Server數(shù)據(jù)庫(kù)的詳細(xì)圖文教程,需要的朋友可以參考下2023-06-06淺析SQL Server中的執(zhí)行計(jì)劃緩存(上)
這篇文章主要介紹了淺析SQL Server中的執(zhí)行計(jì)劃緩存(上)的相關(guān)資料,需要的朋友可以參考下2015-12-12Sql Server中存儲(chǔ)過程中輸入和輸出參數(shù)(簡(jiǎn)單實(shí)例 一看就懂)
Sql Server中存儲(chǔ)過程中輸入和輸出參數(shù)(簡(jiǎn)單實(shí)例,一看就懂),方便需要的朋友2012-10-10dbeaver配置SQL?server連接實(shí)現(xiàn)
本文主要介紹了dbeaver配置SQL?server連接實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07利用 SQL Server 過濾索引提高查詢語(yǔ)句的性能分析
本文就給大家介紹一下 Microsoft SQL Server 中的過濾索引功能,本文通過場(chǎng)景模擬分析給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2021-07-07Access 數(shù)據(jù)類型與 MS SQL 數(shù)據(jù)類型的相應(yīng)
Access 數(shù)據(jù)類型與 MS SQL 數(shù)據(jù)類型的相應(yīng)...2006-10-10