SQL Server Service Broker The South East Michigan SQL Server Users Group Tom Groszko...

23
SQL Server Service Broker The South East Michigan SQL Server Users Group http://semssug.sqlpass.org Tom Groszko [email protected] t

Transcript of SQL Server Service Broker The South East Michigan SQL Server Users Group Tom Groszko...

Page 1: SQL Server Service Broker The South East Michigan SQL Server Users Group  Tom Groszko tom.groszko@charter.net.

SQL ServerService Broker

The South East Michigan SQL Server Users Grouphttp://semssug.sqlpass.org

Tom [email protected]

Page 2: SQL Server Service Broker The South East Michigan SQL Server Users Group  Tom Groszko tom.groszko@charter.net.
Page 3: SQL Server Service Broker The South East Michigan SQL Server Users Group  Tom Groszko tom.groszko@charter.net.

• http://www.sqlpass.org/24hours/Fall2010/• September 15-16, 2010

Page 4: SQL Server Service Broker The South East Michigan SQL Server Users Group  Tom Groszko tom.groszko@charter.net.

`

• http://www.sqlpass.org/summit/na2010/• November 8-11, 2010• Seattle, Washington

Page 5: SQL Server Service Broker The South East Michigan SQL Server Users Group  Tom Groszko tom.groszko@charter.net.

Purpose and ObjectivesProvide an introduction to Service Broker

What Service Broker can do

Define Service Broker Terms

Sample code

Page 6: SQL Server Service Broker The South East Michigan SQL Server Users Group  Tom Groszko tom.groszko@charter.net.

What Service Broker Can Do• Native support for messaging and

queuing applications– Conversations– Message ordering and coordination

• Build distributed applications– Workload balancing– N-tier applications

• Transactional asynchronous processing

Page 7: SQL Server Service Broker The South East Michigan SQL Server Users Group  Tom Groszko tom.groszko@charter.net.

What Service Broker Can Do• Take advantage of Service Oriented

Architecture (SOA)• Workflow (WF)• BizTalk• Microsoft Message Queuing (MSMQ)

Page 8: SQL Server Service Broker The South East Michigan SQL Server Users Group  Tom Groszko tom.groszko@charter.net.

Service Broker Terms – Message Type• A MESSAGE TYPE defines the name

of a message and the validation that Service Broker performs on this message. Both sides of a conversation must define the same message types.– Validation

• NONEWhatever you want in the message body (stored as varbinary(max))

• EMPTY The message body must be null.

• WELL_FORMED_XML The body must contain well formed xml.

Page 9: SQL Server Service Broker The South East Michigan SQL Server Users Group  Tom Groszko tom.groszko@charter.net.

Service Broker Terms - Initiator• An Initiator is the service that starts

a conversation. (BEGIN DIALOG)• A service can be both an Initiator

and a Target but on different conversations.

Page 10: SQL Server Service Broker The South East Michigan SQL Server Users Group  Tom Groszko tom.groszko@charter.net.

Service Broker Terms - Target• A Target is a service that accepts a

conversation that was started by another service.

• A service can be both an Initiator and a Target but on different conversations.

Page 11: SQL Server Service Broker The South East Michigan SQL Server Users Group  Tom Groszko tom.groszko@charter.net.

Service Broker Terms - Contract

• A CONTRACT defines the message types used in a broker conversation and which party in the conversation can send them.

Page 12: SQL Server Service Broker The South East Michigan SQL Server Users Group  Tom Groszko tom.groszko@charter.net.

Service Broker Terms - Queue• A QUEUE is where Service Broker

stores messages. • A QUEUE is a ‘hidden table’ and is a

schema-owned object.• Like any other table it can be placed

on a file group.• A QUEUE has a status that is

– ON messages can be added and removed

– OFF messages cannot be added or removed.

Page 13: SQL Server Service Broker The South East Michigan SQL Server Users Group  Tom Groszko tom.groszko@charter.net.

Service Broker Terms - Queue• A QUEUE has an Activation Status

that may identify the stored procedure to process messages in the queue and how many instances of the procedure may run concurrently.

• When a QUEUE is unavailable messages are held in a transmission queue for the database, sys.transmission_queue.

Page 14: SQL Server Service Broker The South East Michigan SQL Server Users Group  Tom Groszko tom.groszko@charter.net.

Service Broker Terms - Queue• POISON_MESSAGE_HANDLING

– ON The default. The QUEUE will be disabled, STATUS = OFF, when there are five consecutive transaction rollbacks.

– OFF The QUEUE will not be disabled by service broker because of transaction rollbacks.

Page 15: SQL Server Service Broker The South East Michigan SQL Server Users Group  Tom Groszko tom.groszko@charter.net.

Service Broker Terms - Queue• A QUEUE can be queried with

SELECT.– To prevent locking the queue

SELECT * FROM schema.queue WITH(NOLOCK);

• It cannot be modified with INSERT, UPDATE or DELETE statements.

Page 16: SQL Server Service Broker The South East Michigan SQL Server Users Group  Tom Groszko tom.groszko@charter.net.

Service Broker Terms - Service• A SERVICE is a name for a specific

task or set of tasks and is used to route messages to the correct QUEUE and enforce CONTRACT constraints.

• A SERVICE identifies a QUEUE and a list of CONTRACTs that the service uses.

Page 17: SQL Server Service Broker The South East Michigan SQL Server Users Group  Tom Groszko tom.groszko@charter.net.

Service Broker Terms - Route• A ROUTE identifies which network

address a Service Broker service is located on.

• Whenever you want your messages to leave the database they originate in, you need to provide a ROUTE.

• ROUTE is not covered in this presentation.

Page 18: SQL Server Service Broker The South East Michigan SQL Server Users Group  Tom Groszko tom.groszko@charter.net.

Environment• Automatically installed beginning with SQL2005• Service Broker External Activator is available

with SQL Server 2008 Feature Pack– Lets you move logic to receive and process messages from

the server engine to an executable.http://www.microsoft.com/downloads/details.aspx?FamilyId=228DE03F-3B5A-428A-923F-58A033D316E1&displaylang=en

• The model database has Broker Enabled = false– CREATE DATABASE creates a database with Broker

Enabled = true despite the model setting.

Page 19: SQL Server Service Broker The South East Michigan SQL Server Users Group  Tom Groszko tom.groszko@charter.net.

Message Performance• The techniques you use for tuning other applications apply to

tuning Service Broker applications

Source Pro SQL Server 2008 Service Broker by Klause Aschenbrenner APress

Page 20: SQL Server Service Broker The South East Michigan SQL Server Users Group  Tom Groszko tom.groszko@charter.net.

Demo• SQL Server event notifications

– Sample code to process SQL Server event notificationsThe code will provide a history record for all DDL changes made on an SQL Server. (Quite a bit less than source code management)

– Demonstrate how to RECEIVE messages• Set processing• Row by agonizing row

• Sending and Receiving messages– A explanation of the basic code to SEND and RECEIVE a message.

• An order application that sends a message to three different services which process them asynchronously and send back a reply.

• The Service Broker Application Template.

Page 21: SQL Server Service Broker The South East Michigan SQL Server Users Group  Tom Groszko tom.groszko@charter.net.

Additional Information• Pro SQL Server 2008 Service Broker

Klaus Aschenbrennerhttp://www.apress.com/book/view/1590599993

• A brief guide to SQL Server Service Brokerhttp://blogs.msdn.com/b/kangmo/archive/2008/12/17/a-brief-guide-on-sql-server-service-broker.aspx

• SQL Server: Service Broker Team Bloghttp://blogs.msdn.com/b/sql_service_broker/rss.aspx

• A simple secure dialog with transport certificateshttp://blogs.msdn.com/b/sql_service_broker/archive/2008/06/10/a-simple-secure-dialog-with-certificates.aspx

• Announcing Service Broker External Activator http://social.msdn.microsoft.com/Forums/en-US/sqlservicebroker/thread/36a58004-dbef-46ad-85a2-93563f3f8f0e

• Sample activated application http://blogs.msdn.com/b/sql_service_broker/archive/2010/03/10/sample-activated-application.aspx

• Get Started With Using External Activatorhttp://blogs.msdn.com/b/sql_service_broker/archive/2009/05/18/get-started-with-using-external-activator.aspx

• Service Broker: Performance and Scalability Techniques http://technet.microsoft.com/en-us/library/dd576261(SQL.100).aspx

• Service Broker Tutorials http://msdn.microsoft.com/en-us/library/bb839489.aspx

• Building a Distributed Service Broker Application http://www.sqlservercentral.com/articles/Service+Broker/2797/

Page 22: SQL Server Service Broker The South East Michigan SQL Server Users Group  Tom Groszko tom.groszko@charter.net.

Additional Information• The Service Broker Alphabet Part 1

http://www.sqlservercentral.com/articles/Service+Broker/2807/• The Service Broker Alphabet Part 2

http://www.sqlservercentral.com/articles/Service+Broker/2808/• Async Lifestyle: Manage Your Tasks With Service Broker

http://technet.microsoft.com/en-us/magazine/2005.05.servicebroker.aspx• SQL Server Service Broker - An Introduction

http://www.databasejournal.com/features/mssql/article.php/3880191/SQL-Server-Service-Broker---An-Introduction.htm

• Writing an SSBS App When the Initiator and Target are on the Same Database http://www.databasejournal.com/features/mssql/article.php/3896201/Writing-an-SSBS-App-When-the-Initiator-and-Target-are-on-the-Same-Database.htm

• Service Broker Troubleshootinghttp://www.mssqltips.com/tip.asp?tip=1197

• Service Broker Wait Typeshttp://blogs.msdn.com/b/sql_service_broker/archive/2008/12/01/service-broker-wait-types.aspx

Page 23: SQL Server Service Broker The South East Michigan SQL Server Users Group  Tom Groszko tom.groszko@charter.net.

Questions