SSB(SQLservice Service Broker) 入門實例介建站知識
導讀:1建站知識前兩天用了 MSsql里的 SSB委托機制,做了一個消息分發的小功能,在這里簡單跟大家分享一下方法跟實例建設網站網站建設。
step 1:perpar database & data
復制代碼 代碼如下:
use mastergoCreate database Inventorygouse InventorygoCreate 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 Inventorygo--send messagebegin 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 inventorygobegin 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;
聲明: 本文由我的SEOUC技術文章主頁發布于:2023-05-23 ,文章SSB(SQLservice Service Broker) 入門實例介建站知識主要講述實例,入門,SQL網站建設源碼以及服務器配置搭建相關技術文章。轉載請保留鏈接: http://www.bifwcx.com/article/web_6034.html