Code Snippet - Message Filtering (Message Driven Beans)

Message-Driven Beans

Date: 19/Jun/2002

Message Filtering

After completing this snippet you should be able to configure message-driven beans to be more selective about the messages it receives from a topic or queue.

Introduction

A message-driven bean (MDB), introduced in EJB 2.0, is an enterprise bean that allows J2EE applications to process messages asynchronously. Message-driven beans process messages that are delivered via the Java Message Service (JMS). MDBs are stateless, server-side, transaction-aware components used for processing asynchronous JMS messages. It acts as a JMS message listener which receives JMS messages. The messages may be sent by any J2EE component; an application client, another enterprise bean, or a web component; or by a JMS application or a system that does not use J2EE technology.

Unlike other enterprise beans, message-driven beans are not accessed through interfaces and have only a bean class. A single message-driven bean can process messages from multiple clients.

MDBs can be configured to select messages and consume only the messages that are relevant for the application. This is achieved by using
<message-selector> in ejb-jar.xml. Message selectors use Message properties as criteria in conditional expressions. These conditional expressions use Boolean logic to declare which messages should be delivered to a client.

Pre-requisites

Basic knowledge about message-driven beans.

Description

Consider the scenario where a client application sends messages about the details of a product to JMS queue.
The code snippet for this scenario looks as below :
Message message = session.createMessage();
message.setStringProperty("itemname", itemName);
message.setStringProperty("quantity", quantity);
message.setStringProperty("price", price);
message.setJMSExpiration(18000);
session.createSender(queue).send(message);

As with other enterprise beans, MDBs are also defined in ejb-jar.xml as follows :
<message-driven>
   <ejb-name>MessageBean</ejb-name>
   <ejb-class>MyQueue</ejb-class>
   <transaction-type>Container</transaction-type>
   <message-driven-destination>
     <destination-type>javax.jms.Queue</destination-type>

   </message-driven-destination>
</message-driven>
Suppose you would like to restrict your MDB to only receive messages where the itemname ends with 'CD' and
price ranges from 100 to 150. This can be achieved using the <message-selector> in ejb-jar.xml as follows :
<message-driven>
   <ejb-name>MessageBean</ejb-name>
   <ejb-class>MyQueue</ejb-class>

   <message-selector>
   <![CDATA[
      itemname LIKE '%CD' AND price BETWEEN 100 AND 150
   ]]>
   </message-selector>
   <transaction-type>Container</transaction-type>
   <message-driven-destination>
     <destination-type>javax.jms.Queue</destination-type>
   </message-driven-destination>
</message-driven>
Here the itemname, price refer to the property name that is set for the message using message.setStringProperty.

Note : CDATA sections are used to avoid XML parsing errors which might occur due to the use of characters such as
           greater than (>) or less than (<) in the selector declaration.

The message selectors are similar to the WHERE clause used in regular SQL statements and is unique to message-driven beans.


Copyright © 2002, Oracle Corporation. All rights reserved.

E-mail this page
Printer View Printer View
Oracle Is The Information Company About Oracle | Oracle RSS Feeds | Careers | Contact Us | Site Maps | Legal Notices | Terms of Use | Privacy