Quantcast
Channel: SQL Service Broker forum
Viewing all articles
Browse latest Browse all 461

SQL Server Service Broker not woking

$
0
0

Dear All,

I've created one table and SP need to execute to insert the getdate using service broker.

first time it'e working after that Service broker not working in my code. Server broker and trustworthy enabled on my test database.

Service broker need to be work on my same database.

Could any one please review the code and give your valuable feedback.

 

 

---------------------------- New ServiceBroker Creation  ------------------------------------------------

 

---------------------------------------------------- Step 1. Create the SP -------------------------------------------------------

 

CreateTable Test_Service_Broker2

(

  Input_Text                 varchar (255),

  Execution_Time            DateTime

)

 

gO

 

CREATE Procedure Test_Service_Broker_Sp

as

Begin

       DECLARE @Input_Text                 varchar (255)

 

      Insertinto Test_Service_Broker2

      Select'1',  Getdate()

End

go

 

---------------------------------------------- Step 2. Create the below Configuration Script for Service broker ---------------------------------

 

 

-- -----------------------------------------------

-- Configuring Service Broker

-- -----------------------------------------------

 

--Delete  FROM sys.service_message_types  WHERE name ='Test_Service_BrokerSendMessageType'

--Delete  FROM sys.service_message_types  WHERE name ='Test_Service_BrokerReceiveMessageType'

--Delete  FROM sys.service_contracts              WHERE name ='Test_Service_Broker_Contract'

--Delete  FROM sys.service_queues                 WHERE name ='Test_Service_Broker_QUEUE'

--Delete  FROM sys.services                       WHERE name ='Test_Service_Broker_SERVICE'

 

 

--SELECT 'X' FROM sys.service_queues WHERE name ='Test_Service_Broker_QUEUE'

 

-- Create a Type for Send Message type

IFNOTEXISTS(SELECT'X'FROMsys.service_message_typesWHERE name ='Test_Service2_BrokerSendMessageType')

BEGIN

      CREATEMESSAGE TYPE Test_Service2_BrokerSendMessageType

             VALIDATION= WELL_FORMED_XML;

END

GO

-- SELECT * FROM sys.service_message_types WHERE name ='Test_Service_BrokerSendMessageType'

      

-- Create a Type for Receive Message Type

IFNOTEXISTS(SELECT'X'FROMsys.service_message_typesWHERE name ='Test_Service2_BrokerReceiveMessageType')

BEGIN

      CREATEMESSAGE TYPE Test_Service2_BrokerReceiveMessageType

             VALIDATION= WELL_FORMED_XML;

      -- SELECT * FROM sys.service_message_types WHERE name ='Test_Service_BrokerReceiveMessageType'

END

GO

 

-- Create Contract to be used

-- you can use SENT BY ANY and use a single message instead

-- however for the sake of showing the contract functionality

-- i created two messsage types that can be sent by each

IFNOTEXISTS(SELECT'X'FROMsys.service_contractsWHERE name ='Test_Service2_Broker_Contract')

BEGIN

      CREATECONTRACT Test_Service2_Broker_Contract

      (

              Test_Service2_BrokerSendMessageType   SENTBY INITIATOR,

              Test_Service2_BrokerReceiveMessageTypeSENTBY TARGET

      )

 

      -- SELECT * FROM sys.service_contracts WHERE name ='Test_Service_Broker_Contract'

END

GO

 

-- Create a QUEUE

IFNOTEXISTS(SELECT 'X'FROMsys.service_queuesWHERE name ='Test_Service2_Broker_QUEUE')

BEGIN

      CREATEQUEUE Test_Service2_Broker_QUEUE

             WITHSTATUS=ON,

             ACTIVATION (

                     PROCEDURE_NAME= Test_Service_Broker_Sp,

                     MAX_QUEUE_READERS= 5,

                     ExecuteAS 'dbo');

END

GO

 

-- Create a Service to be used

IFNOTEXISTS(SELECT 'X'FROMsys.servicesWHERE name ='Test_Service2_Broker_SERVICE')

BEGIN

 

      CREATESERVICE Test_Service2_Broker_SERVICE

      ONQUEUE Test_Service2_Broker_QUEUE

      (

              Test_Service2_Broker_Contract

      )

END

GO

 

-------------------------------------------------   Step 3 : QUEUE_INITIATOR_SP   ---------------------------------------------------

 

CREATEPROCEDURE Test_Service2_Broker_QUEUE_INITIATOR_SP 

@GUID      UDD_GUID  

AS 

BEGIN 

  

      DECLARE @dialog_handle uniqueidentifier 

       

      BEGIN DIALOG CONVERSATION @dialog_handle 

   FROMSERVICE Test_Service2_Broker_SERVICE 

   TOSERVICE'Test_Service2_Broker_SERVICE' 

   ONCONTRACT Test_Service2_Broker_Contract

   WITHENCRYPTION= OFF; 

  

-- Now Sending a message note we are using 

-- SendMessageType since it is defined in the contract 

-- that it is to be sent only by initiater 

  

      SENDON CONVERSATION @dialog_handle 

      MESSAGETYPE Test_Service2_BrokerSendMessageType(@GUID)

      

        

 

      ENDCONVERSATION @dialog_handle

      WITH CLEANUP;

 

END  

 

-------------------------------------------------   Step 4: Test   ---------------------------------------------------

 

  Exec Test_Service2_Broker_QUEUE_INITIATOR_SP'GGGG'

  GO

 

  select*from Test_Service_Broker2

 Not working after executing this SP more than one time.

Thanks in Advance..

 



Viewing all articles
Browse latest Browse all 461

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>