使用 Service Broker 异步调用存储的 Proc
在我的应用程序中,我需要异步调用存储过程。 为此,我使用 Sql Service Broker。 这些是创建异步调用所涉及的步骤。
1)我创建了消息、合约、队列、服务。 并发送消息。我可以在“ReceiveQueue1”中看到我的消息。
2)我创建了一个存储过程和一个队列 当我执行存储过程(proc_AddRecord)时,它只执行一次。 它读取队列中的所有记录并将这些记录添加到表中。 到目前为止,它工作正常。 但是当我向“ReceiveQueue1”添加一些新消息时,我的存储过程没有添加这些消息 自动记录到表中。我必须重新执行存储过程(proc_AddRecord) 以便添加新消息。为什么存储过程没有被执行。 我应该做什么才能异步调用存储过程。 使用 Service Broker 的全部目的是异步调用存储过程。 我对 SQL Server Service Broker 完全陌生。 感谢任何帮助。 这是我的存储过程的代码
#--exec proc_AddRecord
ALTER PROCEDURE proc_AddRecord
AS
Declare
@Conversation UniqueIdentifier,
@msgTypeName nvarchar(200),
@msg varbinary(max)
While (1=1)
Begin
Begin Transaction;
WAITFOR
(
Receive Top (1)
@Conversation = conversation_handle,
@msgTypeName = message_type_name,
@msg = message_body
from dbo.ReceiveQueue1
), TIMEOUT 5000
IF @@Rowcount = 0
Begin
Rollback Transaction
Break
End
PRINT @msg
If @msg = 'Sales'
BEGIN
insert into TableCity(deptNo,Manager,Group,EmpCount) VALUES(101,'Reeves',51, 29)
COMMIT Transaction
Continue
End
If @msg = 'HR'
BEGIN
insert into TableCity(deptNo,Manager,Group,EmpCount) VALUES(102,'Cussac',55, 14)
COMMIT Transaction
Continue
End
Begin
Print 'Process end of dialog messages here.'
End Conversation @Conversation
Commit Transaction
Continue
End
Rollback Transaction
END
ALTER QUEUE AddRecorQueue
WITH ACTIVATION (
PROCEDURE_NAME=proc_AddRecord,
MAX_QUEUE_READERS = 1,
STATUS = ON,
EXECUTE AS 'dbo');
In my application I need to call a Stored Proc Asynchronously.
For this I am using Sql Service Broker.
These are the steps Involved in creating the asynchronous calling.
1) I created Message,Contract,Queue,Service.
And Sending messages.I can see my messages in 'ReceiveQueue1'.
2) I created a stored Proc and a Queue
When I execute the Stored Proc(proc_AddRecord) its executing only once.
Its reading all the records in the Queues and adding those records to the table.
Upto this point its working fine.
But when I add some new messages to 'ReceiveQueue1' my stored proc is not adding those
records automatically to the table. I have to re execute the Stored Proc(proc_AddRecord)
inorder to add the new messages. Why is the Stored proc is not getting executed.
What I am supposed to do in order to call the Stored Proc Asynchronously.
The whole point of using Service Broker is to call stored procs asynchronously.
I am totally new to SQL Server Service Broker.
Appreciate any help.
Here is my code for the stored Proc
#
--exec proc_AddRecord
ALTER PROCEDURE proc_AddRecord
AS
Declare
@Conversation UniqueIdentifier,
@msgTypeName nvarchar(200),
@msg varbinary(max)
While (1=1)
Begin
Begin Transaction;
WAITFOR
(
Receive Top (1)
@Conversation = conversation_handle,
@msgTypeName = message_type_name,
@msg = message_body
from dbo.ReceiveQueue1
), TIMEOUT 5000
IF @@Rowcount = 0
Begin
Rollback Transaction
Break
End
PRINT @msg
If @msg = 'Sales'
BEGIN
insert into TableCity(deptNo,Manager,Group,EmpCount) VALUES(101,'Reeves',51, 29)
COMMIT Transaction
Continue
End
If @msg = 'HR'
BEGIN
insert into TableCity(deptNo,Manager,Group,EmpCount) VALUES(102,'Cussac',55, 14)
COMMIT Transaction
Continue
End
Begin
Print 'Process end of dialog messages here.'
End Conversation @Conversation
Commit Transaction
Continue
End
Rollback Transaction
END
ALTER QUEUE AddRecorQueue
WITH ACTIVATION (
PROCEDURE_NAME=proc_AddRecord,
MAX_QUEUE_READERS = 1,
STATUS = ON,
EXECUTE AS 'dbo');
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您说您正在执行存储过程,您不需要这样做,甚至一次都不需要,它应该始终通过激活来完成。
如果您的激活位于“ReceiveQueue1”而不是“AddRecorQueue”上,我看不到其余的代码,但名称表明了这一点。
您的存储过程从哪里开始和结束?一般来说,我会将 BEGIN 放在 AS 语句之后,将 END 放在存储过程应该结束的地方,如果没有这些,那么您需要一个 GO 语句将其分开。否则,您的 ALTER QUEUE 语句将成为存储过程的一部分
您还有“回滚事务”,因此即使激活正常工作,它也会全部回滚,或者引发错误,指出没有事务触发其中一个 IF 语句。
我建议您总体上遵循此服务代理教程,并且这一关于内部激活的内容。他们应该让你开始。
You say you are executing the stored procedure, you shouldn't need to do that, not even once, it should always be done with the activation.
Should your activation be on your 'ReceiveQueue1' instead of your 'AddRecorQueue' I can't see the rest of your code, but the names suggest it.
Where does your stored procedure begin and end? Generally I'd put BEGIN just after the AS statement and END where the stored procedure should end, If you don't have these then you'd need a GO statement to separate it off. Otherwise your ALTER QUEUE statement would be part of the stored procedure
You also have "Rollback Transaction" so even if the activation was working it would all get rolled back, or raise an error saying there was no transaction had one of the IF statements been triggered.
I suggest you follow this tutorial for service broker in general and this one about internal activation. They should get you started.