You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
sql-tools/sql 队列.sql

200 lines
5.1 KiB

1 year ago
use master
go
drop database Inventory
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);
---------
exec sp_executesql N'SELECT
mt.name AS [Name],
''Server[@Name='' + quotename(CAST(serverproperty(N''Servername'') AS sysname),'''''''') + '']'' + ''/Database[@Name='' + quotename(db_name(),'''''''') + '']'' + ''/ServiceBroker'' + ''/MessageType[@Name='' + quotename(mt.name,'''''''') + '']'' AS [Urn],
mt.message_type_id AS [ID]
FROM
sys.service_message_types mt
WHERE
(CAST(case when mt.message_type_id < 65536 then 1 else 0 end AS bit)=@_msparam_0)
ORDER BY
[Name] ASC',N'@_msparam_0 nvarchar(4000)',@_msparam_0=N'0'
IF NOT EXISTS(SELECT * FROM sys.service_message_types WHERE name = '//saidao/types/perf')
BEGIN
Create Message Type [//saidao/types/perf]
END
IF NOT EXISTS(SELECT * FROM sys.service_message_types WHERE name = '//saidao/types/perf_response')
BEGIN
Create Message Type [//saidao/types/perf_response]
END
---
--<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Լ
IF NOT EXISTS(SELECT * FROM sys.service_contracts WHERE name = '//saidao/contracts/perf_contract')
BEGIN
Create contract [//saidao/contracts/perf_contract]
(
[//saidao/types/perf] sent by initiator,
[//saidao/types/perf_response] sent by target
);
END
--<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
IF NOT EXISTS(SELECT * FROM sys.service_queues WHERE name = 'perf_queue')
BEGIN
create queue [perf_queue];
create service [//saidao/services/perf]
on queue [perf_queue]
(
[//saidao/contracts/perf_contract]
);
END
--<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
use Inventory
go
--send message
begin transaction
declare @dialog_id uniqueidentifier
begin dialog conversation @dialog_id
from service [//saidao/services/perf]
to service '//saidao/services/perf'
on contract [//saidao/contracts/perf_contract]
with encryption = off;
send on conversation @dialog_id message type [//saidao/types/perf]
(
'<PerfUpdate>
<FromDate>pc1001</FromDate>
<ToDate>pc1001</ToDate>
<CompId>102</CompId>
</PerfUpdate>'
);
commit transaction;
--<EFBFBD><EFBFBD><EFBFBD>ͳɹ<EFBFBD><EFBFBD>󣬿<EFBFBD><EFBFBD>Բ<EFBFBD>ѯ inventory_queue
select conversation_handle, cast(message_body as xml) as conversation_body from [perf_queue]
--<EFBFBD><EFBFBD><EFBFBD><EFBFBD>conversation_body <EFBFBD><EFBFBD><EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD>ˡ<EFBFBD>
-- <EFBFBD><EFBFBD> inventory queue <EFBFBD>л<EFBFBD>ȡ<EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
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].[perf_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 ;
--<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܣ<EFBFBD>ֻҪ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ض<EFBFBD><EFBFBD>У<EFBFBD><EFBFBD>Ϳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD>ִ<EFBFBD>й<EFBFBD><EFBFBD><EFBFBD>.
--<EFBFBD><EFBFBD><EFBFBD>Ƚ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̣<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>receive <EFBFBD>е<EFBFBD><EFBFBD>Ƕδ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ù<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ˣ<EFBFBD>
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].[perf_queue]),timeout 50000;
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
--<EFBFBD><EFBFBD><EFBFBD>м<EFBFBD><EFBFBD><EFBFBD>
alter queue [perf_queue]
with activation(
status = on,
procedure_name= InventoryProc,
max_queue_readers=2,
execute as self
);
go
<EFBFBD><EFBFBD><EFBFBD>ɺ<EFBFBD><EFBFBD><EFBFBD>ֻҪ<EFBFBD><EFBFBD><EFBFBD>Ͷ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD>󣬾Ϳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
PS<EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD>Ƕ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD>и<EFBFBD> inventory client queue
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>б<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>dzɹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>error <EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>receive <EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
<EFBFBD><EFBFBD><EFBFBD>ƴ<EFBFBD><EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>:
select message_type_name from [perf_queue]