Presentation is loading. Please wait.

Presentation is loading. Please wait.

DAT340 实现异步消息队列 ---SQL Server 2005 Service Broker.

Similar presentations


Presentation on theme: "DAT340 实现异步消息队列 ---SQL Server 2005 Service Broker."— Presentation transcript:

1

2 DAT340 实现异步消息队列 ---SQL Server 2005 Service Broker

3 异步式编程环境和队列功能 – Service Broker 回答三个问题: 什么时候需要异步队列操作 ? Service Broker 是什么 ? 为什么要在数据库中处理消息 ? 具体实现与演示

4 当数据库项目开始的时候 VBSQL.OXCVBSQL.OXC Database Application ADOADO OLEDBOLEDB DB-LibraryDB-Library SQLNCLISQLNCLI Client Network Library.NET Framework Data Provider for SQL Server.NET Framework Data Provider for SQL Server Storage Engine Relational Engine TSQL Endpoint Server Network Library SQL Server Architecture of Data Access Technologies

5 重新考虑软件构架中的因素 性能 吞吐量 容量 功能性 可用性 恢复力 失效安全 容错能力 Have an architecture that makes sense before you write 3.5 million lines of code. 技术因素 区别 - 没有运动的部分 - 可以创建新材料 - 可以改变物理现象 避免失败 - 将关键部分分散开来 - 语义上的一致性 - 职责分散

6 在超市里排队 

7 在机场里排队 在机场里排队

8 异步消息队列的使用场景 股票交易系统中的结算活动订单输入系统中的发货信息 旅行预订系统在客户填写完路线后再进行实际的 预订, 并在预订完成后发达确认电子邮件

9 Q2 : Services Broker 是什么? 为什么排队应用程序难以编写?

10 消息的完整性分布事务性消息传送

11 The Architecture of SQL Server 2005 SQL Server 2005 Database Engine Replication Full-Text Search Service Broker Analysis Services Integration Services Reporting Services Notification Services SQL Server Agent

12 Service Broker 是什么 ? Platform for building asynchronous queued database applications Queues as first class database objects Queue manipulation built into TSQL Transactional message processing Reliable distributed queuing

13 Use Service Broker when you need to: Scale out the system Improve reliability and availability Improve application response time Build an auditing system Maintain up-to-date cached data Scale out the system Improve reliability and availability Improve application response time Build an auditing system Maintain up-to-date cached data Factors to consider Large messages Reliable messaging Heterogeneous data Security 考虑使用 Service Broker 的因素

14 Service Broker 消息处理事务的步骤 1. 开始事务。 2. 从会话组中接收一条或多条消息。 3. 从状态表中检索会话的状态。 4. 处理消息并根据消息内容对应用程序数据进行一项或多 项更新。 5. 发送一些 Service Broker 消息:即,将响应发送到传入 的消息或将消息发送到处理传入消息所需的其他服务。 6. 如果此会话组包含其他消息,则读取和处理此会话组中 的其他消息。 7. 使用新的会话状态更新会话状态表。 8. 提交事务

15 Q3 :为什么要在数据库中处理消息 ?

16 为什么要在数据库中进行消息传送?消息和数据的单客户端连接。事务性消息传送不需要分布式事务或两阶段提交。备份和恢复数据库还可以备份和恢复消息队列。群集或数据库镜像确定队列中的操作非常简单在提交事务时只需写入单个日志可靠的消息传送通常将消息从传送队列传送到接收队列

17 队列读取器管理

18 Activation

19 具体实现与演示

20 Service Broker 系统架构 SQL Server Object Description Message Type Defines valid messages for exchange between services Contract Specifies type of messages and their direction in a conversation, initiator or target Queue Stores messages before sending and after receipt as result set Service Program The part of a service broker application that reads messages from a queue and processes them. Service Addressable endpoint for service communication Service program Contract Service Queue Message type

21 如何实现 Service Broker How to Enable Service Broker in a Database Implementing Services  How to Create Contracts  How to Create Queues  How to Create Services  How to Send a Message  How to Receive a Message

22 How to Enable Service Broker in a Database Check Service Broker status 1 1 Enable Service Broker 2 2 SELECT is_broker_enabled FROM sys.databases WHERE database_id = db_id() SELECT is_broker_enabled FROM sys.databases WHERE database_id = db_id() ALTER DATABASE AdventureWorks SET ENABLE_BROKER ALTER DATABASE AdventureWorks SET ENABLE_BROKER

23 Implementing Services Design: Message types Contracts Queues Services Service program logic Message types Contracts Queues Services Service program logic

24 1.How to Create Contracts Create message types 1 1 Create contract 2 2 CREATE CONTRACT [//Adventure-Works.com/Expenses/ProcessExpense] ( [//Adventure-Works.com/Expenses/ExpenseClaim] SENT BY INITIATOR, SENT BY INITIATOR, [// Adventure-Works.com/Expenses/ClaimResponse] [// Adventure-Works.com/Expenses/ClaimResponse] SENT BY TARGET ) SENT BY TARGET ) CREATE CONTRACT [//Adventure-Works.com/Expenses/ProcessExpense] ( [//Adventure-Works.com/Expenses/ExpenseClaim] SENT BY INITIATOR, SENT BY INITIATOR, [// Adventure-Works.com/Expenses/ClaimResponse] [// Adventure-Works.com/Expenses/ClaimResponse] SENT BY TARGET ) SENT BY TARGET ) CREATE MESSAGE TYPE [//Adventure-Works.com/Expenses/ExpenseClaim] VALIDATION = WELL_FORMED_XML CREATE MESSAGE TYPE [//Adventure-Works.com/Expenses/ClaimResponse] VALIDATION = VALID_XML WITH SCHEMA COLLECTION awschemas CREATE MESSAGE TYPE [//Adventure-Works.com/Expenses/ExpenseClaim] VALIDATION = WELL_FORMED_XML CREATE MESSAGE TYPE [//Adventure-Works.com/Expenses/ClaimResponse] VALIDATION = VALID_XML WITH SCHEMA COLLECTION awschemas

25 2.How to Create Queues Create queue name 1 1 Choose whether queue is available 2 2 Specify any activation parameters 3 3 CREATE QUEUE ExpenseQueue CREATE QUEUE ExpenseQueueWithActivation WITH STATUS = OFF, ACTIVATION ( PROCEDURE_NAME = ProcessExpense, MAX_QUEUE_READERS = 5, EXECUTE AS SELF) CREATE QUEUE ExpenseQueue CREATE QUEUE ExpenseQueueWithActivation WITH STATUS = OFF, ACTIVATION ( PROCEDURE_NAME = ProcessExpense, MAX_QUEUE_READERS = 5, EXECUTE AS SELF)

26 Create service name 3.How to Create Services 1 1 Choose storage queue List contracts for which the service is a target Choose whether to retain messages for conversation lifetime 2 2 3 3 4 4 CREATE SERVICE [//Adventure-Works.com/SubmitExpense] ON QUEUE ExpenseQueue ( [//Adventure-Works.com/Expenses/ProcessExpense] ) CREATE SERVICE [//Adventure-Works.com/SubmitExpense] ON QUEUE ExpenseQueue ( [//Adventure-Works.com/Expenses/ProcessExpense] )

27 4.How to Send a Message Declare a dialog handle variable 1 1 Begin a dialog conversation 2 2 Send the message 3 3 DECLARE @dialog_handle uniqueidentifier BEGIN DIALOG CONVERSATION @dialog_handle FROM SERVICE [//Adventure-Works.com/SubmitExpense] TO SERVICE '//Adventure-Works.com/ProcessExpense' ON CONTRACT [//Adventure-Works.com/Expenses/ProcessExpense] DECLARE @dialog_handle uniqueidentifier BEGIN DIALOG CONVERSATION @dialog_handle FROM SERVICE [//Adventure-Works.com/SubmitExpense] TO SERVICE '//Adventure-Works.com/ProcessExpense' ON CONTRACT [//Adventure-Works.com/Expenses/ProcessExpense] DECLARE @dialog_handle uniqueidentifier BEGIN DIALOG CONVERSATION @dialog_handle FROM SERVICE [//Adventure-Works.com/SubmitExpense] TO SERVICE '//Adventure-Works.com/ProcessExpense' ON CONTRACT [//Adventure-Works.com/Expenses/ProcessExpense] SEND ON CONVERSATION @dialog_handle MESSAGE TYPE [//Adventure-Works.com/Expenses/ExpenseClaim] (@msgString) DECLARE @dialog_handle uniqueidentifier BEGIN DIALOG CONVERSATION @dialog_handle FROM SERVICE [//Adventure-Works.com/SubmitExpense] TO SERVICE '//Adventure-Works.com/ProcessExpense' ON CONTRACT [//Adventure-Works.com/Expenses/ProcessExpense] SEND ON CONVERSATION @dialog_handle MESSAGE TYPE [//Adventure-Works.com/Expenses/ExpenseClaim] (@msgString)

28 5.How to Receive a Message Declare variables for storing message details 1 1 Call RECEIVE statement 2 2 Check the message type, and process accordingly 3 3 If finished conversation, call END CONVERSATION 4 4 DECLARE @conversation UNIQUEIDENTIFIER DECLARE @msg NVARCHAR(MAX), @msgType NVARCHAR(256) DECLARE @conversation UNIQUEIDENTIFIER DECLARE @msg NVARCHAR(MAX), @msgType NVARCHAR(256) DECLARE @conversation UNIQUEIDENTIFIER DECLARE @msg NVARCHAR(MAX), @msgType NVARCHAR(256) ;RECEIVE TOP(1) @conversation = conversation_handle, @msgType = message_type_name, @msg = message_body FROM ExpenseQueue DECLARE @conversation UNIQUEIDENTIFIER DECLARE @msg NVARCHAR(MAX), @msgType NVARCHAR(256) ;RECEIVE TOP(1) @conversation = conversation_handle, @msgType = message_type_name, @msg = message_body FROM ExpenseQueue DECLARE @conversation UNIQUEIDENTIFIER DECLARE @msg NVARCHAR(MAX), @msgType NVARCHAR(256) ;RECEIVE TOP(1) @conversation = conversation_handle, @msgType = message_type_name, @msg = message_body FROM ExpenseQueue IF (@msgType = '//Adventure-Works.com/Expenses/ExpenseClaim') -- process @msg … -- process @msg … DECLARE @conversation UNIQUEIDENTIFIER DECLARE @msg NVARCHAR(MAX), @msgType NVARCHAR(256) ;RECEIVE TOP(1) @conversation = conversation_handle, @msgType = message_type_name, @msg = message_body FROM ExpenseQueue IF (@msgType = '//Adventure-Works.com/Expenses/ExpenseClaim') -- process @msg … -- process @msg … DECLARE @conversation UNIQUEIDENTIFIER DECLARE @msg NVARCHAR(MAX), @msgType NVARCHAR(256) RECEIVE TOP(1) @conversation = conversation_handle, @msgType = message_type_name, @msg = message_body FROM ExpenseQueue IF (@msgType = '//Adventure-Works.com/Expenses/ExpenseClaim') -- process @msg … -- process @msg … END CONVERSATION @conversation DECLARE @conversation UNIQUEIDENTIFIER DECLARE @msg NVARCHAR(MAX), @msgType NVARCHAR(256) RECEIVE TOP(1) @conversation = conversation_handle, @msgType = message_type_name, @msg = message_body FROM ExpenseQueue IF (@msgType = '//Adventure-Works.com/Expenses/ExpenseClaim') -- process @msg … -- process @msg … END CONVERSATION @conversation

29

30 Resources Technical Chats and Webcasts http://www.microsoft.com/communities/chats/default.mspx http://www.microsoft.com/usa/webcasts/default.asp Microsoft Learning and Certification http://www.microsoft.com/learning/default.mspx MSDN & TechNet http://microsoft.com/msdn http://microsoft.com/technet Virtual Labs http://www.microsoft.com/technet/traincert/virtuallab/rms.mspx Newsgroups http://communities2.microsoft.com/ communities/newsgroups/en-us/default.aspx Technical Community Sites http://www.microsoft.com/communities/default.mspx User Groups http://www.microsoft.com/communities/usergroups/default.mspx

31 Resources Blogs http://blogs.msdn.com/remusrusanu/ http://rushi.desai.name/Blog/tabid/54/BlogID/1/Default.aspx http://blogs.msdn.com/rogerwolterblog/default.aspx Samples http://www.gotdotnet.com/codegallery/codegallery.aspx?id=9f7ae2af- 31aa-44dd-9ee8-6b6b6d3d6319 http://www.gotdotnet.com/codegallery/codegallery.aspx?id=9f7ae2af- 31aa-44dd-9ee8-6b6b6d3d6319Forum http://forums.microsoft.com/msdn/ShowForum.aspx?ForumID=91

32 © 2006 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

33 附 : 为什么要在数据库中处理消息 ? Integrated API for Messages and Data Single connection for messaging and other database operations Transactional receives/sends from remote systems Integrated management, deployment, and operations Backup, restore, configuration, monitoring, security Startup and shutdown start, recover and stop Service Broker Queued messages and application session state may be queried Trace Events for monitoring and debugging Messages have database integrity and recoverability

34 附 : 为什么要在数据库中处理消息 ? Performance advantages for database applications No two-phase commits for transactional messaging Single log write on commit No process boundary crossing to external messaging software Optimized “in-instance” delivery


Download ppt "DAT340 实现异步消息队列 ---SQL Server 2005 Service Broker."

Similar presentations


Ads by Google