服务代理:通过TRANSPORT路由在两个实例上进行内部激活

时间:2013-07-24 11:40:08

标签: sql-server service-broker

服务器A和B包含SQL Server实例。有通过TRANSPORT的路线创建服务。 已成功收到消息。我正在尝试在B上创建内部激活,但队列停止。我做错了什么?已修复。对于从A发送到B的每条消息,返回5份。

服务器A

      USE master
    GO

    CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'PAROLPAROL123'

    CREATE CERTIFICATE [GA_SERVER]
    WITH SUBJECT = 'GA_SERVER';
    GO

    CREATE ENDPOINT BROKER
       STATE=STARTED
       AS TCP (LISTENER_PORT = 4022)
       FOR SERVICE_BROKER 
       (
         AUTHENTICATION = CERTIFICATE [GA_SERVER]
       );

    GRANT CONNECT ON ENDPOINT::BROKER TO [PUBLIC];
    GO

    CREATE DATABASE DB_A
    GO

    ALTER DATABASE DB_A
     SET ENABLE_BROKER
     WITH ROLLBACK IMMEDIATE;

    GO

    USE DB_A;
    CREATE MESSAGE TYPE RequestMT
     validation=NONE;

    CREATE MESSAGE TYPE ResponseMT 
     validation=NONE;

    CREATE CONTRACT GAContract
    (
      RequestMT 
       SENT BY INITIATOR,
      ResponseMT
       SENT BY TARGET
    );

    CREATE QUEUE GAQueue
     WITH status = ON;

    CREATE SERVICE [tcp://10.10.100.56:4022/GAservice]
     ON QUEUE GAQueue (GAContract)
    GO

    GRANT SEND ON SERVICE::[tcp://10.10.100.56:4022/GAservice] TO [PUBLIC]
    CREATE ROUTE transport WITH ADDRESS = 'TRANSPORT';

    GO
CREATE TABLE [dbo.t1](
    [mes] [nvarchar](max) NULL
) ON [PRIMARY];

GO

CREATE PROCEDURE ActivProcA
AS
BEGIN
SET NOCOUNT ON
  DECLARE @RecvReqDlgHandle UNIQUEIDENTIFIER;
  DECLARE @RecvReqMsg NVARCHAR(1000);
  DECLARE @RecvReqMsgName sysname;

BEGIN TRANSACTION;  
WHILE (1=1)
BEGIN

WAITFOR
( RECEIVE TOP(1)
        @RecvReqDlgHandle = conversation_handle,
        @RecvReqMsg = message_body,
        @RecvReqMsgName = message_type_name
    FROM GAQueue
), TIMEOUT 5000;
    IF (@@ROWCOUNT = 0)
    BEGIN
      ROLLBACK TRANSACTION;
--      END CONVERSATION @RecvReqDlgHandle;
      BREAK;
    END

    IF @RecvReqMsgName =
       N'ResponseMT'
    BEGIN
     INSERT INTO [dbo.t1] (mes) VALUES (N'back to A from B ('+ CAST(CURRENT_TIMESTAMP AS NVARCHAR)+N'):'+@RecvReqMsg);  
    END 
     ELSE   
      END CONVERSATION @RecvReqDlgHandle;   
    COMMIT TRANSACTION;     
SET NOCOUNT OFF;
END;
END;  

GO

ALTER QUEUE GAQueue
    WITH 
    STATUS = ON,
    ACTIVATION
    ( STATUS = ON,
      PROCEDURE_NAME = ActivProcA,
      MAX_QUEUE_READERS = 10,
      EXECUTE AS SELF 
    );
GO

服务器B

USE MASTER
GO

CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'PAROLPAROL123'

CREATE CERTIFICATE [GOS_SERVER]
WITH SUBJECT = 'GOS_SERVER';
GO

CREATE ENDPOINT BROKER
   STATE=STARTED
   AS TCP (LISTENER_PORT = 4022)
   FOR SERVICE_BROKER 
   (
     AUTHENTICATION = CERTIFICATE [GOS_SERVER],
   );

GRANT CONNECT ON ENDPOINT::BROKER TO [PUBLIC];
GO

CREATE DATABASE DB_B_1
GO

ALTER DATABASE DB_B_1
 SET ENABLE_BROKER
 WITH ROLLBACK IMMEDIATE;
GO

USE DB_B_1;
CREATE MESSAGE TYPE RequestMT
 validation=NONE;

CREATE MESSAGE TYPE ResponseMT 
 validation=NONE;

CREATE CONTRACT GAContract
(
  RequestMT 
   SENT BY INITIATOR,
  ResponseMT
   SENT BY TARGET
);

CREATE QUEUE GAQueue
 WITH status = OFF; 

GO

CREATE SERVICE [tcp://10.10.100.78:4022/GOS_DB_B_1_service]
 ON QUEUE GAQueue (GAContract)
GO

GRANT SEND ON SERVICE::[tcp://10.10.100.78:4022/GOS_DB_B_1_service] TO [PUBLIC]

CREATE ROUTE transport WITH ADDRESS = 'TRANSPORT';

GO

CREATE TABLE [dbo.t1](
    [mes] [nvarchar](max) NULL
) ON [PRIMARY];
GO

CREATE PROCEDURE ActivProcB
AS
 BEGIN
  SET NOCOUNT ON; 
  DECLARE @RecvReqDlgHandle UNIQUEIDENTIFIER;
  DECLARE @RecvReqMsg NVARCHAR(100);
  DECLARE @RecvReqMsgName sysname;

  WHILE (1=1)
  BEGIN
    BEGIN TRANSACTION;

    WAITFOR
    ( RECEIVE TOP(1)
        @RecvReqDlgHandle = conversation_handle,
        @RecvReqMsg = message_body,
        @RecvReqMsgName = message_type_name
      FROM GAQueue
    ), TIMEOUT 5000;

    IF (@@ROWCOUNT = 0)
    BEGIN
      ROLLBACK TRANSACTION;
      BREAK;
    END

    IF @RecvReqMsgName =
       N'RequestMT'
    BEGIN
        INSERT INTO [dbo.t1] (mes) VALUES (N'('+ CAST(CURRENT_TIMESTAMP AS NVARCHAR)+N'):'+@RecvReqMsg);             
        DECLARE @Msg NVARCHAR(100);
        DECLARE @I INT;
        SET @I = 1;
        WHILE @I<6 
        BEGIN
         SET @Msg = N'<MsgFromB1>'+CAST (@I AS NVARCHAR(1))+N' ('+CAST(CURRENT_TIMESTAMP AS NVARCHAR)+N')</MsgFromB1>';
         SEND ON CONVERSATION @RecvReqDlgHandle
               MESSAGE TYPE 
                ResponseMT
               (@Msg);       
         SET @I = @I+1;     
        END                 
    END;      
    COMMIT TRANSACTION;     
  END

SET NOCOUNT OFF;  
END;
GO

ALTER QUEUE GAQueue
    WITH 
     STATUS = ON,
     ACTIVATION
    ( STATUS = ON,
      PROCEDURE_NAME = ActivProcB,
      MAX_QUEUE_READERS = 10,
      EXECUTE AS SELF
    );
GO

服务器A

USE DB_A

DECLARE @ConversationHandle UNIQUEIDENTIFIER;
DECLARE @RequestMsg NVARCHAR(4000);

BEGIN TRANSACTION

BEGIN DIALOG @ConversationHandle
 FROM SERVICE [tcp://10.10.100.56:4022/GAservice]
 TO SERVICE 'tcp://10.10.100.78:4022/GOS_DB_B_1_service'
 ON CONTRACT GAContract
 WITH ENCRYPTION = OFF;

 SELECT @RequestMsg =
       N'<RequestMsg>test1</RequestMsg>';

SEND 
 ON CONVERSATION @ConversationHandle
     MESSAGE TYPE RequestMT
  (@RequestMsg);

COMMIT TRANSACTION;

GO

1 个答案:

答案 0 :(得分:1)

  

我正在尝试在B上创建内部激活,但队列停止

这很有可能是毒药消息检测。这意味着您的激活过程会引发异常并导致回滚。发生这种情况时,程序的输出(包括错误消息)将记录到ERRORLOG中。这就是你需要看的地方。

您还可以尝试通过禁用激活并从查询窗口手动调用该过程来解决该过程。这样您就可以直接看到任何错误消息/异常。要在与激活过程相同的上下文中执行,请在调用过程之前发出EXECUTE AS USER = 'dbo'

请参阅Troubleshooting Activation Stored Procedures