6-28 Programming JMS for Oracle WebLogic Server
http:java.sun.comjavaee5docsapijavaxjmsQueueBrowser.htm l
.
6.9 Filtering Messages
In many cases, an application does not need to be notified of every message that is delivered to it. Message selectors can be used to filter unwanted messages, and
subsequently improve performance by minimizing their impact on network traffic.
Message selectors operate as follows:
■
The sending application sets message header or property fields to describe or classify a message in a standardized way.
■
The receiving applications specify a simple query string to filter the messages that they want to receive.
Because message selectors cannot reference the contents body of a message, some information may be duplicated in the message property fields except in the case of
XML messages.
You specify a selector when creating a queue receiver or topic subscriber, as an argument to the QueueSession.createReceiver or
TopicSession.createSubscriber methods, respectively. For information about creating queue receivers and topic subscribers, see
Section 5.2.5, Step 5: Create Message Producers and Message Consumers.
WebLogic JMS assigns a state or current processing condition to messages during processing. You can use these states as selectors. For information on valid message
states, see weblogic.jms.extensions.JMSMessageInfo in Oracle WebLogic Server API Reference.
The following sections describe how to define a message selector using SQL statements and XML selector methods, and how to update message selectors. For more
information about setting header and property fields, see Section 6.8, Setting and
Browsing Message Header and Property Fields and
Section 6.8.2, Setting Message Property Fields,
respectively.
6.9.1 Defining Message Selectors Using SQL Statements
A message selector is a boolean expression. It consists of a String with a syntax similar to the where clause of an SQL select statement.
The following excerpts provide examples of selector expressions. salary 64000 and dept in eng,qa
product like WebLogic or product like T3 and version 3.0
hireyear between 1990 and 1992 or fireyear is not null
fireyear - hireyear 4 The following example shows how to set a selector when creating a queue receiver
that filters out messages with a priority lower than 6. String selector = JMSPriority = 6;
qsession.createReceiverqueue, selector;
Managing Your Applications 6-29
The following example shows how to set the same selector when creating a topic subscriber.
String selector = JMSPriority = 6; qsession.createSubscribertopic, selector;
For more information about the message selector syntax, see the javax.jms.Message Javadoc at
http:java.sun.comjavaee5docsapijavaxjmsMessage.html .
6.9.2 Defining XML Message Selectors Using XML Selector Method
For XML message types, in addition to using the SQL selector expressions described in the previous section to define message selectors, you can use the following method:
String JMS_BEA_SELECTString type, String expression JMS_BEA_SELECT is a built-in function in WebLogic JMS SQL syntax. You specify the
syntax type, which must be set to xpath XML Path Language and an XPath expression. The XML path language is defined in the XML Path Language XPath
document, which is available at the XML Path Language Web site at:
http:www.w3.orgTRxpath .
The method returns a null value under the following circumstances:
■
The message does not parse.
■
The message parses, but the element is not present.
■
If a message parses and the element is present, but the message contains no value for example, orderorder.
For example, consider the following XML excerpt: order
item id007id
nameHand-held Power Drillname descriptionCompact, assorted colors.description
price34.99price item
item id123id
nameMitre Sawname descriptionThree blades sizes.description
price69.99price item
item id66id
nameSocket Wrench Setname descriptionSet of 10.description
price19.99price item
order
Note: Pay careful attention to your XML message syntax, since
malformed XML messages for example, a missing end tag will not match any XML selector.
6-30 Programming JMS for Oracle WebLogic Server
The following example shows how to retrieve the name of the second item in the previous example. This method call returns the string, Mitre Saw.
String sel = JMS_BEA_SELECTxpath, orderitem[2]nametext = Mitre Saw;
Pay careful attention to the use of double and single quotes and spaces. Note the use of single quotes around xpath, the XML tab, and the string value.
The following example shows how to retrieve the ID of the third item in the previous example. This method call returns the string, 66.
String sel = JMS_BEA_SELECTxpath, orderitem[3]idtext = 66;
6.9.3 Displaying Message Selectors