SQL Server Service Broker sample

Source: Internet
Author: User

1. Defining data types, protocols, and services (sending and receiving services)

 UseMaster;GOALTER DATABASETarget DatabaseSETEnable_broker;GO--If the above operation is executed for a long time without reaction, there is a suspicion of freezing, try the following statement. ALTER DATABASETarget DatabaseSETNew_broker with ROLLBACKIMMEDIATE;GOALTER DATABASETarget DatabaseSETEnable_broker;GO
--Create the Sayhellomessage message type.--the message type does not do the processing of data validation.CREATEMESSAGE TYPE sayhellomessage VALIDATION=None;GO--Create a contract sayhellocontract--defined, send/Receive side.--This message type is used.CREATEcontract sayhellocontract (sayhellomessage SENT by  any);GO--Create a Send/Receive queueCREATEQUEUE Sayhellosendqueue;CREATEQUEUE Sayhelloreceivequeue;GO--Create initiator service Sayhellosendservice--The service uses the Sayhellosendqueue queue--because the contract name is not specified, other services cannot use the service as the target service. CREATESERVICE Sayhellosendservice onQUEUE Sayhellosendqueue;GO--Create a target service Sayhelloreceiveservice--The service uses the Sayhelloreceivequeue queue--Using Sayhellocontract ConventionsCREATESERVICE Sayhelloreceiveservice onQUEUE Sayhelloreceivequeue ([sayhellocontract]);GO

2. Send a message

--defines the handle of the send.  DECLARE @InitDlgHandle uniqueidentifier; --defines a variable.  DECLARE @MyMessage NVARCHAR( -); --sets the content of the sending message.  SET @MyMessage =N'Hello world!'  --begins a transaction.  BEGIN TRANSACTION; --defines message sending processing.  BEGINDIALOG@InitDlgHandle     fromSERVICE--define the Send service.Sayhellosendservice toSERVICE--define the receive service.N'Sayhelloreceiveservice'     onContract--define the conventions usedsayhellocontract with  --not encrypted.Encryption= OFF; --send a message.SEND onConversation@InitDlgHandleMESSAGE TYPE[Sayhellomessage]        ( @MyMessage );--outputs the received message.PRINT 'I sent a:' + @MyMessage; --commits the transaction.  COMMIT TRANSACTION;

3. Receiving Messages

--The receive handle.  DECLARE @RecvReqDlgHandle uniqueidentifier; --the data received.  DECLARE @RecvReqMsg NVARCHAR( -); --the name of the data type received.  DECLARE @RecvReqMsgNamesysname; --begins a transaction.  BEGIN TRANSACTION; --attempts to receive messages from the Sayhelloreceivequeue queue.  WAITFOR(RECEIVETOP(1)      @RecvReqDlgHandle =Conversation_handle,@RecvReqMsg       =Message_body,@RecvReqMsgName   =Message_type_name fromsayhelloreceivequeue), TIMEOUT +; --if the received message type is named Sayhellomessage  --then proceed with the processing.  IF @RecvReqMsgName =N'Sayhellomessage'  BEGIN    --defines the message to prepare for return.    DECLARE @ReplyMsg NVARCHAR( -); --simple setup.    SELECT @ReplyMsg = '~' + @RecvReqMsg + '~';--debug output.Declare @t nvarchar(Max)PRINT 'I received:' + @RecvReqMsg + '; I will feedback:' + @ReplyMsg; --send a feedback message.    --SEND on conversation @RecvReqDlgHandle    --MESSAGE TYPE    --[Sayhellomessage]    --(@ReplyMsg);    --END conversation @RecvReqDlgHandle;  END; --commits the transaction.  COMMIT TRANSACTION;

SQL Server Service Broker example (GO)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.