java left logo
java middle logo
java right logo
 

Home arrow Java EE Tips
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 4098
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
Filtering messages for a message-driven EJB E-mail
User Rating: / 14
PoorBest 

Sometimes there is a need for message-driven beans to receive only the messages that they are intended to process. To selectively deliver messages to a message-driven bean, the bean should be deployed with a message selector. The bean source needs no changes in order to use the message selector. This tip shows a simple message-driven bean that only wants messages that contain an attribute UserRole set to 'admin'. The bean prints out the role of the incoming message for verification. In the XML descriptor for the bean, we describe the message selector that filters undesired messages for the message-driven bean.

Here we show the partial XML descriptor that describes the simple EJB and its message selector:

A sample MDB code:


public class SimpleMDB implements MessageDrivenBean, MessageListener {

    ...
            
    public void onMessage(Message message) {
    
        MapMessage map = (MapMessagemessage;
        
        try {
            
            String role = map.getString("UserRole");
            System.out.println("Received Message for Role: " + role);
            
            // the process the message:
            ...
        catch (Exception e) {
            e.printStackTrace();
        }
        
    }
    
    ...
}

The filter statement in descriptor:

<ejb-jar>
	<enterprise-beans>
		<message-driven>
        
			<ejb-name>SimpleMDB</ejb-name>
			<ejb-class>test.SimpleMDB</ejb-class>
			<transaction-type>Container</transaction-type>
			
			<!-- statement filtering incoming messages -->
			<message-selector>
				<![CDATA[ UserRole = 'admin' ]]>
			</message-selector>

			<message-driven-destination>
				<destination-type>
                                    javax.jms.Topic
                                </destination-type>
			</message-driven-destination>

		</message-driven>
	</enterprise-beans>
<ejb-jar>


 Related Tips

 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.