Skip to content

Latest commit

 

History

History
130 lines (86 loc) · 8.79 KB

sql-server-service-broker.md

File metadata and controls

130 lines (86 loc) · 8.79 KB
title description author ms.author ms.date ms.service ms.subservice ms.topic f1_keywords helpviewer_keywords monikerRange
SQL Server Service Broker
Learn about Service Broker. See how it provides native support for messaging in the SQL Server Database Engine and Azure SQL Managed Instance.
rwestMSFT
randolphwest
06/07/2021
sql
configuration
how-to
SQL13.SWB.SSBMSGTYPEPROPERTIES.GENERAL.F1
SQL13.SWB.SSBCONTRACTPROPERTIES.GENERAL.F1
SQL13.SWB.SSBQUEUEPROPERTIES.GENERAL.F1
SQL13.SWB.SSBREMSVCBINDPROPERTIES.GENERAL.F1
SQL13.SWB.SSBROUTEPROPERTIES.GENERAL.F1
SQL13.SWB.SSBPRIORITYPROPERTIES.GENERAL.F1
SQL13.SWB.SSBSERVICEPROPERTIES.GENERAL.F1
Broker See Service Broker
SQL Server Service Broker
Service Broker
=azuresqldb-mi-current||>=sql-server-2016||>=sql-server-linux-2017

Service Broker

[!INCLUDE SQL Server SQL MI]

[!INCLUDEssNoVersion] [!INCLUDEssSB] provide native support for messaging and queuing in the [!INCLUDEssDEnoversion] and Azure SQL Managed Instance. Developers can easily create sophisticated applications that use the [!INCLUDEssDE] components to communicate between disparate databases, and build distributed and reliable applications.

When to use Service Broker

Use Service Broker components to implement native in-database asynchronous message processing functionalities. Application developers who use [!INCLUDEssSB] can distribute data workloads across several databases without programming complex communication and messaging internals. Service Broker reduces development and test work because [!INCLUDEssSB] handles the communication paths in the context of a conversation. It also improves performance. For example, front-end databases supporting Web sites can record information and send process intensive tasks to queue in back-end databases. [!INCLUDEssSB] ensures that all tasks are managed in the context of transactions to assure reliability and technical consistency.

Overview

Service Broker is a message delivery framework that enables you to create native in-database service-oriented applications. Unlike classic query processing functionalities that constantly read data from the tables and process them during the query lifecycle, in service-oriented application you have database services that are exchanging the messages. Every service has a queue where the messages are placed until they are processed.

Service broker

The messages in the queues can be fetched using the Transact-SQL RECEIVE command or by the activation procedure that will be called whenever the message arrives in the queue.

Creating services

Database services are created by using the CREATE SERVICE Transact SQL statement. Service can be associated with the message queue create by using the CREATE QUEUE statement:

CREATE QUEUE dbo.ExpenseQueue;
GO
CREATE SERVICE ExpensesService
    ON QUEUE dbo.ExpenseQueue; 

Sending messages

Messages are sent on the conversation between the services using the SEND Transact-SQL statement. A conversation is a communication channel that is established between the services using the BEGIN DIALOG Transact-SQL statement.

DECLARE @dialog_handle UNIQUEIDENTIFIER;

BEGIN DIALOG @dialog_handle  
FROM SERVICE ExpensesClient  
TO SERVICE 'ExpensesService';  
  
SEND ON CONVERSATION @dialog_handle (@Message) ;  

The message will be sent to the ExpensesService and placed in dbo.ExpenseQueue. Because there is no activation procedure associated to this queue, the message will remain in the queue until someone reads it.

Processing messages

The messages that are placed in the queue can be selected by using a standard SELECT query. The SELECT statement will not modify the queue and remove the messages. To read and pull the messages from the queue, you can use the RECEIVE Transact-SQL statement.

RECEIVE conversation_handle, message_type_name, message_body  
FROM ExpenseQueue; 

Once you process all messages from the queue, you should close the conversation using the END CONVERSATION Transact-SQL statement.

Where is the documentation for Service Broker?

The reference documentation for [!INCLUDEssSB] is included in the [!INCLUDEssnoversion] documentation. This reference documentation includes the following sections:

See the previously published documentation for [!INCLUDEssSB] concepts and for development and management tasks. This documentation is not reproduced in the [!INCLUDEssnoversion] documentation due to the small number of changes in [!INCLUDEssSB] in recent versions of [!INCLUDEssnoversion].

What's new in Service Broker

Service broker and Azure SQL Managed Instance

Cross-instance service broker message exchange between instances of Azure SQL Managed Instance and message exchange between SQL Server and Azure SQL Manage Instance is currently in public preview:

  • CREATE ROUTE: Port specified must be 4022. See CREATE ROUTE.
  • ALTER ROUTE: Port specified must be 4022. See ALTER ROUTE.

Transport security is supported, dialog security is not:

  • CREATE REMOTE SERVICE BINDING is not supported.

Service broker is enabled by default and cannot be disabled. The following ALTER DATABASE options are not supported:

  • ENABLE_BROKER
  • DISABLE_BROKER

No significant changes were introduced in [!INCLUDE sssql19-md]. The following changes were introduced in [!INCLUDEssSQL11].

Messages can be sent to multiple target services (multicast)

The syntax of the SEND (Transact-SQL) statement has been extended to enable multicast by supporting multiple conversation handles.

Queues expose the message enqueued time

Queues have a new column, message_enqueue_time, that shows how long a message has been in the queue.

Poison message handling can be disabled

The CREATE QUEUE (Transact-SQL) and ALTER QUEUE (Transact-SQL) statements now have the ability to enable or disable poison message handling by adding the clause, POISON_MESSAGE_HANDLING (STATUS = ON | OFF). The catalog view sys.service_queues now has the column is_poison_message_handling_enabled to indicate whether poison message is enabled or disabled.

Always On support in Service Broker

For more information, see Service Broker with Always On Availability Groups (SQL Server).

Next steps

The most common use of Service Broker is for event notifications. Learn how to implement event notifications, configure dialog security, or get more information.