Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8383409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:09:27+00:00 2026-06-09T17:09:27+00:00

One of the basics of building a distributed application that uses the asynchronous communication

  • 0

One of the basics of building a distributed application that uses the asynchronous communication can be expressed as Do not wait actively for any event! This way, the natural solution based on SQL Service Broker is to use the activation of a stored procedure by the message that arrived to the queue.

The Lesson 2: Creating an Internal Activation Procedure from the official Microsoft tutorial shows how to bind the stored procedure to the message queue. It also suggests the way how the sp should be implemented.

(I am new to SQL. But should not be there one more BEGIN after the CREATE PROCEDURE... AS, and one more END before the GO?)

Do I understand it corectly? See my questions below the code…

CREATE PROCEDURE TargetActivProc
AS
  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 TargetQueueIntAct
    ), TIMEOUT 5000;

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

    IF @RecvReqMsgName =
       N'//AWDB/InternalAct/RequestMessage'
    BEGIN
       DECLARE @ReplyMsg NVARCHAR(100);
       SELECT @ReplyMsg =
       N'<ReplyMsg>Message for Initiator service.</ReplyMsg>';
 
       SEND ON CONVERSATION @RecvReqDlgHandle
              MESSAGE TYPE 
              [//AWDB/InternalAct/ReplyMessage]
              (@ReplyMsg);
    END
    ELSE IF @RecvReqMsgName =
        N'http://schemas.microsoft.com/SQL/ServiceBroker/EndDialog'
    BEGIN
       END CONVERSATION @RecvReqDlgHandle;
    END
    ELSE IF @RecvReqMsgName =
        N'http://schemas.microsoft.com/SQL/ServiceBroker/Error'
    BEGIN
       END CONVERSATION @RecvReqDlgHandle;
    END
      
    COMMIT TRANSACTION;

  END
GO

When the message arrives, the procedure is called, and it enters to the "infinite" loop. Actually, the loop is not infinite because of the BREAK after the ROLLBACK when no data arrived (after the TIMEOUT).

If the data arrived, the BREAK is skipped. If the expected message arrived, the reply is sent back. If the ...EndDialog or ...Error message is received, the END CONVERSATION is executed. Can also some other kind of message be observed here?

As some message arrived (and was processed) the transaction is commited.

But why the loop now? Is the intention to process the other messages that got stuck in the queue because of the broken communication line in the past? Or because of more messages came at once and could not be processed so quickly?

What happens when another message is queued up, and the stored procedure is still running. Is another working process assigned for its processing? Can another stored procedure be launched in parallel? If yes, then why the loop?

Thanks for your help, Petr

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T17:09:28+00:00Added an answer on June 9, 2026 at 5:09 pm

    Internal activation is not like a trigger. Specifically, the activated procedure does not get launched for each message that arrived. Instead the procedure is launched when there is something to process and is supposed to dequeue messages contiguously (in a loop) while the SSB infrastructure is monitoring the progress and, if necessary, launches a second procedure to help, up to the max specified. See Understanding Queue Monitors.

    Having a loop in the activated procedure is not not strictly required, things should work fine even w/o the loop. The loop should perform better in a very busy environment. See also this old MSDN discussion.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm building very basic mvc application with Spring. It has one controller that should
I'm building my first basic tabbed, application with one of the views as a
We have started building an asp.net mvc application. Application will consist with one main
I'm building a Windows Store App that uses a GridView to show a list
I am building some basic HTML code for a CMS. One of the page-related
To learn the basics of OCaml, I'm solving one of the easy facebook engineering
I built a basic search form that queries one column in one table of
I'm building a web app that will (ideally) allow users to follow discussion threads
I'm building a basic web application in ASP.NET 4.0 with C# in Visual Studio
I'm building some AMIs from one of the basic ones on EC2. One of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.