DATABASE

SQL Server 2008 : Service Broker - Message Types

1/22/2011 4:06:16 PM
Message Types are the first of the types of objects that we will be looking at. The Message Type is simply a definition of the type of data that will be contained within the message. When you create a Message Type you define the type of validation that the SQL SERVICE BROKER will be performing on the body of the message. You have four options for this validation. They are NONE, EMPTY, WELL_FORMED_XML, and VALID_XML WITH SCHEMA COLLECTION, with NONE being the default. As with all other database objects Message Types are created under the ownership of a specific database user or database role. Although it is most common to create the objects under the ownership of dbo, any valid database user or role can be specified.

Tip

Make sure that you know the rules for each of the validation options of the MESSAGE TYPES.


Message Types are created using the CREATE MESSAGE TYPE command. Like all the other objects within the SQL SERVICE BROKER Message Type, objects are database specific. The name of the Message Type can be up to 128 characters in length. Although you can use any object name for any SQL SERVICE BROKER objects, since the messages can be sent between systems great care should be used when naming objects so as to ensure that there are no naming collisions when sending messages between servers. One technique is to use a UNC style naming convention, which helps keep SERVICE BROKER object names from colliding with each other when you start sending messages between servers.

The validation type NONE is used when you wish to put any type of data within the message body. This includes text, numbers, XML, or binary data. When using this validation type, you can send any data you like within the message body, except for a NULL value. If you want to ensure that an empty message is sent, you have to specify the validation type EMPTY.

The validation type WELL_FORMED_XML requires that the data you are inserting into the message is correctly formed XML. This saves you from having to write your own validation logic at the receiving side of the message to ensure that the data is a valid XML document.

The validation type VALID_XML WITH SCHEMA COLLECTION requires not only that your XML data be valid, but that it meets the requirements of the predefined XML schema collection. The XML schema collection should be an already defined XML schema definition created within the SQL Server database. Although the creation of XML schemas is outside the scope of this article, you can read more about it by referencing Books OnLine under the index heading “CREATE XML SCHEMA COLLECTION statement.” When using this validation type, before sending the message SQL SERVICE BROKER will first check that the XML document you are sending fits the required XML schema definition; if it does the message will be sent. If the message does not fit the XML schema definition the message will not be sent, and an error message will be returned to the calling code.

The actual syntax for creating a Message Type is very simple and straightforward.

CREATE MESSAGE TYPE [YourApplication/YourMessageType]
AUTHORIZATION dbo
VALIDATION = NONE;

As you can see in the sample code, the name of the Message Type is YourApplication/YourMessageType. The Message Type will be owned by dbo, and there will be no validation performed. I most often use the validation of NONE or EMPTY because I prefer to have my own logic on the receiving side handle checking if the XML is correct. I also do not want the SQL Server engine to have to spend extra time checking that the XML fits the official definition of “well-formed XML.”

If you wish to change the validation after you create the Message Type you can alter the Message Type by using the ALTER MESSAGE TYPE command. The authorization cannot be changed after the Message Type has been created.

Exercise . Creating Message Types

Create four message types within the AdventureWorks database. Use the validation NONE for two of them and WELL_FORMED_XML for the other two. Create the Message Types based on the data shown in Table 1.

Table 1. Message Types and Their Validations
Message Type NameMessage Type Validation
MT _NoneNONE
MT _XMLWELL_FORMED_XML
Other  
  •  SQL Server 2008 : Service Broker Defined
  •  SQL Azure : Building Two OData Consumer Applications (part 2) - Windows Mobile 7 Application
  •  SQL Azure : Building Two OData Consumer Applications (part 1) - Simple Demo App
  •  SQL Server 2008 : Explaining Advanced Query Techniques - Creating CTEs
  •  SQL Server 2008 : Multiple Sources with SQL Server Integration Services
  •  SQL Server 2008 : Working with Multiple-Source Queries - OpenQuery, OpenRowSet, and OpenDataSource Explained
  •  SQL Server 2008 : Working with Multiple-Source Queries - Using Four-Part Database Names & The DTC Explained
  •  SQL Server 2008 : Working with Multiple-Source Queries - Using Linked Servers
  •  OData with SQL Azure - Enabling OData on an Azure Database
  •  OData with SQL Azure - OData Overview
  •  SQL Server 2008 : Performing Nonlogged DML Operations
  •  SQL Server 2008 : Using the OUTPUT Clause with the MERGE Statement
  •  SQL Server 2008 : Returning Data from DML Operations Using the OUTPUT Clause
  •  SQL Server 2008: Working with System Databases
  •  SQL Server 2008 : Using @@IDENTITY and NEWID Functions in DML Statements
  •  SQL Server 2008 : Using Advanced Functionality with DML - Introduction
  •  Defensive Database Programming with SQL Server: The Ticket-Tracking System (part 2) - Removing the performance hit of ON UPDATE CASCADE
  •  Defensive Database Programming with SQL Server: The Ticket-Tracking System (part 1) - Enforcing business rules using constraints only
  •  SQL Server 2008 : Working with DML Queries - Using the MERGE Statement
  •  Defensive Database Programming with SQL Server : Client-side Error Handling
  •  
    Most View
    CES 2013: What You Can Expect from The World’s Biggest Tech Show
    Surviving Changes to Columns
    Ditch Your Laptop For Your Phone (Part 4)
    Which is the right router for you? (Part 2) - Budget Routers
    How To Buy…Network-Attached Storage (Part 2)
    CorelDraw 10 : Printing - Preflight and Print Preview
    Will Apple Be The Next Big Name in Gaming? (Part 3)
    Configuring Local Area Connections
    Transact-SQL in SQL Server 2008 : Spatial Data Types (part 1) - Representing Spatial Data, Working with Geometry Data
    Corsair Vengeance K60 Keyboard - Pro typing with 'murder' design
    Top 10
    Nvidia GeForce GTX Titan 6 GB Graphics Card Review (Part 6)
    Nvidia GeForce GTX Titan 6 GB Graphics Card Review (Part 5)
    Nvidia GeForce GTX Titan 6 GB Graphics Card Review (Part 4)
    Nvidia GeForce GTX Titan 6 GB Graphics Card Review (Part 3)
    Nvidia GeForce GTX Titan 6 GB Graphics Card Review (Part 2)
    Nvidia GeForce GTX Titan 6 GB Graphics Card Review (Part 1)
    Nook HD - A High-Definition Tablet With The Heart Of A Reader (Part 4)
    Nook HD - A High-Definition Tablet With The Heart Of A Reader (Part 3)
    Nook HD - A High-Definition Tablet With The Heart Of A Reader (Part 2)
    Nook HD - A High-Definition Tablet With The Heart Of A Reader (Part 1)