DAT340 实现异步消息队列 ---SQL Server 2005 Service Broker
异步式编程环境和队列功能 – Service Broker 回答三个问题: 什么时候需要异步队列操作 ? Service Broker 是什么 ? 为什么要在数据库中处理消息 ? 具体实现与演示
当数据库项目开始的时候 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
重新考虑软件构架中的因素 性能 吞吐量 容量 功能性 可用性 恢复力 失效安全 容错能力 Have an architecture that makes sense before you write 3.5 million lines of code. 技术因素 区别 - 没有运动的部分 - 可以创建新材料 - 可以改变物理现象 避免失败 - 将关键部分分散开来 - 语义上的一致性 - 职责分散
在超市里排队
在机场里排队 在机场里排队
异步消息队列的使用场景 股票交易系统中的结算活动订单输入系统中的发货信息 旅行预订系统在客户填写完路线后再进行实际的 预订, 并在预订完成后发达确认电子邮件
Q2 : Services Broker 是什么? 为什么排队应用程序难以编写?
消息的完整性分布事务性消息传送
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
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
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 的因素
Service Broker 消息处理事务的步骤 1. 开始事务。 2. 从会话组中接收一条或多条消息。 3. 从状态表中检索会话的状态。 4. 处理消息并根据消息内容对应用程序数据进行一项或多 项更新。 5. 发送一些 Service Broker 消息:即,将响应发送到传入 的消息或将消息发送到处理传入消息所需的其他服务。 6. 如果此会话组包含其他消息,则读取和处理此会话组中 的其他消息。 7. 使用新的会话状态更新会话状态表。 8. 提交事务
Q3 :为什么要在数据库中处理消息 ?
为什么要在数据库中进行消息传送?消息和数据的单客户端连接。事务性消息传送不需要分布式事务或两阶段提交。备份和恢复数据库还可以备份和恢复消息队列。群集或数据库镜像确定队列中的操作非常简单在提交事务时只需写入单个日志可靠的消息传送通常将消息从传送队列传送到接收队列
队列读取器管理
Activation
具体实现与演示
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
如何实现 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
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
Implementing Services Design: Message types Contracts Queues Services Service program logic Message types Contracts Queues Services Service program logic
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
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)
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 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] )
4.How to Send a Message Declare a dialog handle variable 1 1 Begin a dialog conversation 2 2 Send the message 3 3 uniqueidentifier BEGIN DIALOG FROM SERVICE [//Adventure-Works.com/SubmitExpense] TO SERVICE '//Adventure-Works.com/ProcessExpense' ON CONTRACT [//Adventure-Works.com/Expenses/ProcessExpense] uniqueidentifier BEGIN DIALOG FROM SERVICE [//Adventure-Works.com/SubmitExpense] TO SERVICE '//Adventure-Works.com/ProcessExpense' ON CONTRACT [//Adventure-Works.com/Expenses/ProcessExpense] uniqueidentifier BEGIN DIALOG FROM SERVICE [//Adventure-Works.com/SubmitExpense] TO SERVICE '//Adventure-Works.com/ProcessExpense' ON CONTRACT [//Adventure-Works.com/Expenses/ProcessExpense] SEND ON MESSAGE TYPE [//Adventure-Works.com/Expenses/ExpenseClaim] uniqueidentifier BEGIN DIALOG FROM SERVICE [//Adventure-Works.com/SubmitExpense] TO SERVICE '//Adventure-Works.com/ProcessExpense' ON CONTRACT [//Adventure-Works.com/Expenses/ProcessExpense] SEND ON MESSAGE TYPE [//Adventure-Works.com/Expenses/ExpenseClaim]
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 UNIQUEIDENTIFIER NVARCHAR(256) UNIQUEIDENTIFIER NVARCHAR(256) UNIQUEIDENTIFIER NVARCHAR(256) ;RECEIVE = = = message_body FROM ExpenseQueue UNIQUEIDENTIFIER NVARCHAR(256) ;RECEIVE = = = message_body FROM ExpenseQueue UNIQUEIDENTIFIER NVARCHAR(256) ;RECEIVE = = = message_body FROM ExpenseQueue IF = '//Adventure-Works.com/Expenses/ExpenseClaim') -- … -- … UNIQUEIDENTIFIER NVARCHAR(256) ;RECEIVE = = = message_body FROM ExpenseQueue IF = '//Adventure-Works.com/Expenses/ExpenseClaim') -- … -- … UNIQUEIDENTIFIER NVARCHAR(256) RECEIVE = = = message_body FROM ExpenseQueue IF = '//Adventure-Works.com/Expenses/ExpenseClaim') -- … -- … END UNIQUEIDENTIFIER NVARCHAR(256) RECEIVE = = = message_body FROM ExpenseQueue IF = '//Adventure-Works.com/Expenses/ExpenseClaim') -- … -- … END
Resources Technical Chats and Webcasts Microsoft Learning and Certification MSDN & TechNet Virtual Labs Newsgroups communities/newsgroups/en-us/default.aspx Technical Community Sites User Groups
Resources Blogs Samples 31aa-44dd-9ee8-6b6b6d3d aa-44dd-9ee8-6b6b6d3d6319Forum
© 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.
附 : 为什么要在数据库中处理消息 ? 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
附 : 为什么要在数据库中处理消息 ? 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