这几天在学SQL Server 2005 中新提供的Service Broker,关于servic broker我能找到的资料有以下一些
MSDN 里有一篇 <<SQL Server 2005 Service Broker 初探>> 推荐
手上那本 胡百敬的 <<SQL Server 2005 开发详解释>>
Rickie Lee's blog 分类Sql Server 2005 里也有几篇比较有价值的文章
接着就是微软的 那2个WebCast 视频,
其他的中文资料,在Google,Baidu里找来找去,基本上多是采集 MSDN那篇(搞的我很郁闷)
在看了以上的资料后,我开始动手按上面给出的代码做起来
开始一切多挺顺利的, MSDN那个Hello World示例代码 复制下来就可以运行了,但当时对里面的几行代码比较困或
在Hello World 里建立了两个队列 HelloWorldTargetQueue,HelloWorldInitiatorQueue,以及与他们对应的服务
HelloWorldRequestService,HelloWorldResponseService.当时很困惑为什么 HellowWorldTargetQueue不与HelloWorldResponseService对应,因为Response动作一般由服务端发出(ASP做多了,呵呵),后来发现,在 Service Broker里两端是对等的,并且一个队例可以对应多个服务. 反正一句话可能作者搞错了,但是不影响运行...
这个例子在一个数据库里运行很顺利,但很多时候,我们可能需要在2个不同的数据库里运行,一个做为客户,一个做为服务,当你这么想时,你可能就会像我一样马上去尝试,但麻烦接着就来了,以至于我郁闷了3天,去解决着个问题
这3天里很多时间化在找资料上, 我的环境大至是这样的,在一个SQL 实例里(一台电脑上,一个...什么你不知道SQL实例是什么,那算我没说) 有2个数据库 S与C 现在要求,由C发消息到S数据库的Service Broker Service里(我也不想说的这么麻烦 -_")S的Serveic Broker在收到消息后,启动一个写好的存储过程来处理这些消息..(为什么你一点反映多没?以上这些操作可以是异步的,难道你一点多不觉的激动???,现在不用MSMQ,可以直接在数据库级别实现了!!,晕,还是没反映-_"")
我的代码是这样的:
S 数据库
Create Message Type XMLMessage
Validation = WELL_FORMED_XML
Create Contract XMLContract
(XMLMessage Sent by ANY)
--可以在声明队列时指定处理程序,详细见MSDN帮助(哈哈)
Create Queue ReceivingQueue With Status=ON, Retention=OFF
Create Service ReceivingService On Queue ReceivingQueue
(XMLContract)
C 数据库
Create Message Type XMLMessage
Validation = WELL_FORMED_XML
Create Contract XMLContract
(XMLMessage Sent by ANY)
Create Queue SendingQueue With Status=ON, Retention=OFF
Create Service SendingService On Queue SendingQueue
(XMLContract)
接着我们在C数据库里发送消息
Declare @xml_msg xml
Set @xml_msg='<xml>这是消息</xml>'
Declare @handle uniqueidentifier
Begin Dialog Conversation @handle
From Service SendingService
To Service 'ReceivingService'
On Contract XMLContract
WITH ENCRYPTION = OFF, LIFETIME = 100;
Send on Conversation @handle
Message Type XMLMessage
(@xml_msg);
End Conversation @handle
好了,点了一次,发送了,没有任何提示
接着在S:数据库里检测一下有没有收到
Select Cast(Message_Body as xml),* From S.dbo.ReceivingQueue
这位同学,你不要急着去试,以上代码我试了几百遍, S.dbo.ReceivingQueure里是没用任何消息!
为什么?因为2个不同的数据库之间发消息需要"路由",(什么路由?天那!)不要紧张,这个路由并不是你没搞清楚的那个路由,这个路由简单的说就是这样的
=========Copy 自Rickie Lee's blog
To Service 'ReceivingService' --这里用'ReceivingService'
It is indeed part of the SQL 2005 security lockdown. The details are explained in BOL 'Extending database impersonation using EXECUTE AS', http://msdn2.microsoft.com/en-us/library/ms188304.aspx
The easy fix is to make the database A trustworthy: ALTER DATABASE ... SET TRUSTWORTHY ON. If you don't trust the dbo of database a (doesn't seem to be the case, since is 'sa' from your message), then you'd have to use certificate based dialog security.
HTH,
~ Remus
特别地:The easy fix is to make the database A trustworthy: ALTER DATABASE ... SET TRUSTWORTHY ON.
当然,上面还有'Extending database impersonation using EXECUTE AS'
反正管不了那么多了
Alter Database C Set Trustworthy ON
Alter Database C Set Trunstworthy ON
再 Send 一下,也不WC, 洗手了,...如果还不能用就拉到了...
结果可以了, 汗~~~~~~
==================
以上就是这三天捣鼓的一点东西,写出来,希望有用,也给自己当学习笔记
刚开始学,不知道的东西很多,要继续努力了