设为首页 收藏本站
查看: 616|回复: 0

[经验分享] A simple tutorial on SQL Server 2005 Beta 2 Service Broker

[复制链接]

尚未签到

发表于 2015-7-1 13:10:34 | 显示全部楼层 |阅读模式
A simple tutorial on SQL Server 2005 Beta 2 Service Broker


Posted by: Rickie Lee (www.iyunv.com/rickie)

Date: April 6, 2005

Service Broker is a new technology in Microsoft SQL Server 2005 Beta 2 that helps database developers build secure, reliable, and scalable applications. Service Broker provides queuing and reliable messaging as part of the Database Engine. These features provide the infrastructure necessary to build high-performance applications that easily scale-up or scale-out.

1. Service Broker is part of each database and is enabled by default.
Disable service broker for a given database:
Alter Database [DatabaseName] Set Disable_Broker
Enable service broker for a given database:
Alter Database [DatabaseName] Set Enable_Broker

2. Service Broker Objects
Let's take a look at the objects that Service Broker makes use of.

  • Message Types
Message types define the type of data a message must contain (or not contain). Four are available: None, Empty, Well Formed XML and Valid XML with Schema Collection.
For example:
Create Message Type XMLMessage
Validation = WELL_FORMED_XML


  • Contracts
A contract defines the message types used in a conversation and also determines which side of the conversation can send message of that type. Each conversation follows a contract that the initiating service (initiator) specifies when the conversation begins. Both sides of a conversation must define the same contract.
Each contract must have at least one Message Type associated with it.
For example:
Create Contract XMLContract
(XMLMessage Sent by ANY)


  • Queues
Queues store messages. When a message arrives for a service, Service Broker places the message on the queue associated with the service.
You can optionally configure a queue to automatically execute a stored procedure (Service Program) when messages arrive (called Activation).
For example:
Create Queue SendingQueue With Status=ON, Retention=OFF
Create Queue ReceivingQueue With Status=ON, Retention=OFF


  • Service Programs
Service Broker Service Programs are the applications that perform the work of sending (to Services) and receving messages (from Queues). A Service Program can be in the form of a Stored Procedure and/or external application (Windows applications in C# or others)


  • Services
A Service Broker is the interface through which messages can be sent. A service is bound to exactly one queue; however, a queue can hold messages for more than one service.
For example:
Create Service SendingService On Queue SendingQueue
(XMLContract)
Create Service ReceivingService On Queue ReceivingQueue
(XMLContract)


  • Dialogs
A Dialog is a conversation between two Service Broker Services. A dialog is used to send messages from one Service to another using a compatible Contract.
Remember that, you don’t send messages to a Queue directly. Rather, you send them to a Service.
For example:
Declare @handle uniqueidentifier
Begin Dialog Conversation @handle
From Service SendingService
To Service 'ReceivingService'
On Contract XMLContract;

Send on Conversation @handle
Message Type XMLMessage
('Welcome to Rickie Lee''s blog, www.iyunv.com/rickie');

End Conversation @handle with cleanup;

3. Relationships along with Service Broker Objects

DSC0000.gif
Each message sent must conform to one Message Type and that Message Type must be part of matching Contract. Contracts are tied to dialogs and each message sent is done so through the means of a Dialog Conversation.
    Dialog conversations are made between two Service Broker services, or Conversation Endpoints. Dialog conversations between two services are uniquely tracked by means of a conversation_id – a UNIQUEIDENTIFIER value. This identifier exists at both Conversation Endpoints of a Dialog Conversation. Each Message is held in a Queue, which is essentially a table in the database.
  • A message sent using a message type in one service must have the same name and type in the target service.

4. Service Broker Application Demo
For simplicity, all the following statements will be run in one database.
-- Create Message Type
Create Message Type XMLMessage
Validation = WELL_FORMED_XML

-- Create Contract
Create Contract XMLContract
(XMLMessage Sent by ANY)

-- Create Two Queues
Create Queue SendingQueue With Status=ON, Retention=OFF

Create Queue ReceivingQueue With Status=ON, Retention=OFF

-- Create two Service Endpoints
Create Service SendingService On Queue SendingQueue
(XMLContract)

Create Service ReceivingService On Queue ReceivingQueue
(XMLContract)

-- Begin Dialog Conversation
Declare @handle uniqueidentifier
Begin Dialog Conversation @handle
From Service SendingService
To Service 'ReceivingService'
On Contract XMLContract;

Send on Conversation @handle
Message Type XMLMessage
('Welcome to Rickie Lee''s blog, www.iyunv.com/rickie');

End Conversation @handle with cleanup;

It’s very simple. I think no exception will occur. Next let’s check if the message is being transmitted to the target queue.
Select * From ReceivingQueue
Result: ……………..

Select Cast(message_body as XML) From ReceivingQueue
Result: Welcome to Rickie Lee's blog, www.iyunv.com/rickie

Once the message is there, then we can pick it up with the following script:
Receive Cast(message_body as XML) From ReceivingQueue
Result: Welcome to Rickie Lee's blog, www.iyunv.com/rickie


References:
1. A First Look at SQL Server 2005 Service Broker, Roger Wolter, http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnsql90/html/sqlsvcbroker.asp?frame=true
2. Michael G., Service Broker Architecture, http://www.dotnetfun.com/articles/sql/sql2005/SQL2005ServiceBrokerBasicArchitecture.aspx
3. Mike Taulty’s Weblog, SQL Server 2005 Service Broker & WSE 2.0, http://mtaulty.com/blog/archive/2005/02/14/1484.aspx

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-82241-1-1.html 上篇帖子: SQL Server 2005 Beta 2 Service Broker: State of conversation endpoint 下篇帖子: delphi与SQL Server存储过程编程详解
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表