Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Service Broker example not working

I have two databases on the same instance.

One called ICMS, one called CarePay_DEV1

When a change happens in ICMS (Source), it needs to send a message to CarePay_Dev1 (Destination).

I am new to Broker Services, and am trying to make a message go to the queue. Once that works, I will hopefully get the data into a table in the destination, and that will then be processed by .Net code. But I just want something to appear in the destination first.

So, step 1: I enable the service on the two databases

-- Enable Broker on CarePay
ALTER DATABASE CarePay_Dev1
SET ENABLE_BROKER; 

-- Enable Broker on Source
ALTER DATABASE ICMS_TRN
SET ENABLE_BROKER;

Step 2: Create the message type on the source and destination.

-- Create Message Type on Receiver:
USE CarePay_DEV1
GO
CREATE MESSAGE TYPE [IcmsCarePayMessage]
VALIDATION=WELL_FORMED_XML;

-- Create Message Type on Sender:
USE ICMS_TRN
GO
CREATE MESSAGE TYPE [IcmsCarePayMessage]
VALIDATION=WELL_FORMED_XML;

I then create the Contacts, on both databases:

-- Create Message Type on Receiver:
USE CarePay_DEV1
GO
CREATE MESSAGE TYPE [IcmsCarePayMessage]
VALIDATION=WELL_FORMED_XML;

-- Create Message Type on Sender:
USE ICMS_TRN
GO
CREATE MESSAGE TYPE [IcmsCarePayMessage]
VALIDATION=WELL_FORMED_XML;

I then create the message queues on both databases:

-- CREATE Sending Messagw Queue
USE ICMS_TRN
GO
CREATE QUEUE CarePayQueue


-- CREATE Receiving Messagw Queue
USE CarePay_Dev1
GO
CREATE QUEUE CarePayQueue

And finally, I create the services on both databases:

-- Create the message services
USE ICMS_TRN
GO
CREATE SERVICE [CarePayService]
ON QUEUE CarePayQueue


USE CarePay_DEV1
GO
CREATE SERVICE [CarePayService]
ON QUEUE CarePayQueue 

Now, the queues should be ready, so then I try and send something from the source to the destination:

-- SEND THE MESSAGE!
USE ICMS_TRN
GO

DECLARE @InitDlgHandle UNIQUEIDENTIFIER
DECLARE @RequestMessage VARCHAR(1000) 

BEGIN TRAN

    BEGIN DIALOG @InitDlgHandle
    FROM SERVICE [CarePayService]
    TO SERVICE 'CarePayService'
    ON CONTRACT [IcmsCarePayContract]

    SELECT @RequestMessage = N'<Message>The eagle has landed!</Message>';

    SEND ON CONVERSATION @InitDlgHandle
    MESSAGE TYPE [IcmsCarePayMessage] (@RequestMessage)

COMMIT TRAN

I get:

Command(s) completed successfully.

But then when I try select from the destination queue, it's empty.

/****** Script for SelectTopNRows command from SSMS  ******/
SELECT TOP 1000 *, casted_message_body = 
CASE message_type_name WHEN 'X' 
  THEN CAST(message_body AS NVARCHAR(MAX)) 
  ELSE message_body 
END 
FROM [CarePay_DEV1].[dbo].[CarePayQueue] WITH(NOLOCK)

Can anyone spot the issue? I can't see where I tell the destination which database to send the message to - which could be part of the issue?

like image 775
Craig Avatar asked Mar 27 '26 21:03

Craig


2 Answers

I highly recommend you read Adam Machanic's Service Broker Advanced Basics Workbench, specifically the section entitled "Routing and Cross-Database Messaging".

In addition, for future troubleshooting you may want to use SSBDiagnose or also read through Remus Rusanu's numerous articles on the topic

like image 169
etliens Avatar answered Mar 29 '26 10:03

etliens


I think the initiator service sent a message to yourself. Try to change the name of destination (terget) service.

like image 21
yuriy Avatar answered Mar 29 '26 10:03

yuriy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!