人妖在线一区,国产日韩欧美一区二区综合在线,国产啪精品视频网站免费,欧美内射深插日本少妇

新聞動態(tài)

SSB(SQLservice Service Broker) 入門實例介紹

發(fā)布日期:2022-01-03 19:12 | 文章來源:CSDN

step 1:perpar database & data

復(fù)制代碼 代碼如下:

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)建整個SSB的基礎(chǔ)框架.

首先創(chuàng)建信息類型

復(fù)制代碼 代碼如下:

Create Message Type [//mark/v10/types/inventory]
Create Message Type [//mark/v10/types/inventoryresponse]

創(chuàng)建合約
復(fù)制代碼 代碼如下:

Create contracts [//mark/v10/contracts/inventorycontract]
(
[//mark/v10/types/inventory] send by initiator,
[//mark/v10/types/inventoryresponse] send by target
);
創(chuàng)建隊列與服務(wù)
復(fù)制代碼 代碼如下:

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ā)送消息到消息隊列中
復(fù)制代碼 代碼如下:

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
復(fù)制代碼 代碼如下:

select conversation_handle, cast(message_body as xml) as conversation_body from [Inventory queue]

這里只是查詢了兩列。

其中conversation_body 就是 我們發(fā)出的信息了。

setp 4: 從 inventory queue 中獲取信息并更新數(shù)據(jù)

復(fù)制代碼 代碼如下:

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 出來 就為空了。

數(shù)據(jù)同時被更新了

step 5: 接下來做個自動監(jiān)聽功能,只要有信息發(fā)送到隊列,相關(guān)隊列,就可以自動執(zhí)行功能.

首先建立個存儲過程,(把receive 中的那段代碼拿過來就可以了)

復(fù)制代碼 代碼如下:

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


隊列監(jiān)聽
復(fù)制代碼 代碼如下:

alter queue [inventory queue]
with activation(
status = on,
procedure_name= InventoryProc,
max_queue_readers=2,
execute as self
); go

完成后,只要發(fā)送隊列信息后,就可以了

PS: 我們定義了 兩個隊列 還有個 inventory client queue

這個隊列保存的是成功或者error 提示。用receive 方法也可以提取出來。

復(fù)制代碼 代碼如下:

select message_type_name from [Inventory client queue]

以上的 message type service 等信息 如圖所示,都在這里.

以上是SSB的入門實例,以后會再寫一篇實際應(yīng)用上的(以上的sql code 都是純手工輸入,可能還存在錯誤(雖然校驗了兩遍),如果發(fā)生錯誤,對不起大家了。)

版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。

實時開通

自選配置、實時開通

免備案

全球線路精選!

全天候客戶服務(wù)

7x24全年不間斷在線

專屬顧問服務(wù)

1對1客戶咨詢顧問

在線
客服

在線客服:7*24小時在線

客服
熱線

400-630-3752
7*24小時客服服務(wù)熱線

關(guān)注
微信

關(guān)注官方微信
頂部