24 - Message Based Communication

24.1 Introduction to Service Broker

Service Broker allows user to create services that converse with each other by exchanging messages. A service is object of database that provides user with the conversation endpoint. It sends the request message to another service to utilize the functionality.

The following diagram shows the asynchronous processing of message in the Service Broker.

      

Consider an example of the card system. In the card system, the client sends the message to the service on the database server. The message contains information to enter the details of transaction. The message is placed in queue by the server.

The server processes the messages in the order in which they are received. In case the server is unavailable, the message is returned to the original position in the queue. When the server processes the request, the reply is sent to the client. An asynchronous transaction is performed by the user. Service Broker is useful for large scale distributed applications. The applications need to process multiple database servers located at different locations.

The following list states the use of Service Broker. 

1) Asynchronous triggers

They are synchronously executed in the context of the transaction. A trigger invoking the service broker as an asynchronous process can be created. The asynchronous trigger queues a message to perform another job through the service broker.

2) Processing Queries

There is requirement that some applications process queries, discard the system interruptions, or power problems. The applications send the message to service broker service. The application reads the message, runs the query and returns the result. All these operations take place in a single transaction.

3) Reliable data collection

The applications that collect data from different servers make use of the service broker. The data is protected and messages are not misdirected.

4) Processing large batch data 

Applications that handle huge data volume and perform large scale batch processing use the parallel processing and querying offered by the service broker.

5) Distributed server side processing  

Service Broker can help the applications access multiple databases for information. It provides message queuing and hence the application can accept orders even if some databases are not available. 

Architecture of Service Broker

The Service Broker is dependent on the architecture of service broker. It consists of the following objects.

1) Message: It is the data that is exchanged between the services.

2) Service: It is used for addressing conversation endpoint. The messages are navigating between services. The processing and initiator are the two services that take part in the conversation.

3) Message Type: It defines the content of the messages exchanged between the participants in the conversation.

4) Contract: An agreement between two services about the messages exchanged between them.

5) Queue: A container that stores the messages. Each service is associated with the queue.

6) Service Program: Logic for the service is provided. One the message is received, the service program is initiated by the broker and message is forwarded to the program.

Service Broker Activation

The Service Broker is created by defining various components. User needs a mechanism to process the messages present in queue. The mechanism must continuously poll the queue to check if any message is present. Many resources are wasted if the constant polling is applied on the queue.

To resolve this problem, the activation process is used. The broker creates queue monitor for every queue, the activation status is set to ON for the QUEUE_ACTIVATION event notification.

Every queue monitor checks for the following conditions.

1) Messages ready to be received are present in the queue

2) When a statement RECEIVE on the queue returns an empty result set

3) The number of activation stored procedures currently running for the queue

Following two types of activation are provided by the Service Broker.

1) Internal Activation

2) External Activation

1) Internal Activation

With the use of the service broker in internal activation, user creates the service program that processes the messages present in queue as the stored procedure. The broker monitors and the stored procedure is executed.

The program should automatically terminate when no messages are present in the queue. If the queue is terminated when no more messages are present ensures that the resources are rationalized. 

2) External Activation

The external activation process is known as event based activation. It provides a queue activation event that notifies the external applications when queue receives the notification messages and starts the external application that processes the messages in the queue.

24.2 Introduction to Service Broker conservation process 

In the Service Broker architecture, services converse with each other by sending and receiving messages. The following diagram shows the conversation process between the two services.

The Service Broker applications send and receive messages across services. A message is sent from one service to another for processing. When a service receives a message, it checks that the message is of the same type as specified in the contract. The message is added to the queue once it is verified. The service program is attached to every service.

Once the processing is complete, a response or acknowledgement is sent back to the initiator service.

The conversation process between the two services in the Service Broker contains the following objects.

1) Dialog: A dialog is a two way conversation between the two Service Broker services. The sending message is the initiator service. The receiving message is the target service. The contract used for the conversation is defined. 

2) Conversation Group: The conversation group is known as collection of related dialog conversations. The messages must be processed in similar order at the sending and receiving end. 

3) End points: : SQL Server uses the endpoints to communicate with the service broker on different SQL Server instances. The communication is done through the HTTP, TCP, and SOAP protocols.

4) Routes: A Service Broker uses the routes to check the destination of a message. User needs to specify the network address, and the protocol for the route. 

Like us on Facebook