Feeds

Programming message services in Java

Asynchronous interactions seem to be the way of the future

Next gen security for virtualised datacentres

The consumer

In our case, our consumer will be a Message Driven Bean (MDB). This is a type of Enterprise Java Bean (or EJB) that can be deployed within a J2EE application server such as JBoss. However, unlike other EJBs it only has an implementation class. In addition, the lifecycle of a MDB is also simpler than that of either session beans or entity beans. This is because all communication with a MDB is via a JMS queue or topic, no direct synchronous interaction is allowed.

With the advent of EJB3, the creation and definition of all EJBS and of MDBs in particular has become a lot simpler. It is only now necessary to implement the MessageListener interface (a standard JMS interface) and to use some annotations to completely define your MDB. The MessageListener interface defines a single method, namely the onMessage method.

package com.regdeveloper.jms.mdb;

import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

@MessageDriven(name="NewsMessageDrivenBean", 
         activationConfig = {
            @ActivationConfigProperty(
                    propertyName="destinationType", 
                    propertyValue="javax.jms.Queue"),
            @ActivationConfigProperty(
                    propertyName="destination", 
                    propertyValue="queue/testQueue")
})
public class NewsMDB implements MessageListener {

   public void onMessage(Message message) {
      try {
         String text = ((TextMessage)message).getText();
         System.out.println("In the onMessage method(" + text + ")");  
      } catch (JMSException exp) {
         exp.printStackTrace();
      }
   }
   
}

The annotations on the class (the elements before the class definition starting with an “@” sign) are used to specify to the application server how to bind the message driven bean to an appropriate JMS queue. In this case, we give the MDB a name (“NewsMessageDrivenBean”) and a configuration to be used during deployment. This configuration indicates that the MDB should be bound to a queue and that the queue to bind it to has a JNDI name of “queue/testQueue”.

The MDB is packaged up in a jar file (news.jar) and placed within an EAR file using the following application.xml file:

<?xml version="1.0" encoding="UTF-8" ?> 
<application xmlns="http://java.sun.com/xml/ns/j2ee" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
http://java.sun.com/xml/ns/j2ee/application_1_4.xsd" 
        version="1.4"> 
        <display-name>News MDB App</display-name> 
        <module> 
                <ejb>news.jar</ejb> 
        </module> 
</application> 

Build a business case: developing custom apps

More from The Register

next story
Why has the web gone to hell? Market chaos and HUMAN NATURE
Tim Berners-Lee isn't happy, but we should be
Mozilla's 'Tiles' ads debut in new Firefox nightlies
You can try turning them off and on again
Microsoft boots 1,500 dodgy apps from the Windows Store
DEVELOPERS! DEVELOPERS! DEVELOPERS! Naughty, misleading developers!
'Stop dissing Google or quit': OK, I quit, says Code Club co-founder
And now a message from our sponsors: 'STFU or else'
Apple promises to lift Curse of the Drained iPhone 5 Battery
Have you tried turning it off and...? Never mind, here's a replacement
Uber, Lyft and cutting corners: The true face of the Sharing Economy
Casual labour and tired ideas = not really web-tastic
Linux turns 23 and Linus Torvalds celebrates as only he can
No, not with swearing, but by controlling the release cycle
prev story

Whitepapers

Gartner critical capabilities for enterprise endpoint backup
Learn why inSync received the highest overall rating from Druva and is the top choice for the mobile workforce.
Implementing global e-invoicing with guaranteed legal certainty
Explaining the role local tax compliance plays in successful supply chain management and e-business and how leading global brands are addressing this.
Rethinking backup and recovery in the modern data center
Combining intelligence, operational analytics, and automation to enable efficient, data-driven IT organizations using the HP ABR approach.
Consolidation: The Foundation for IT Business Transformation
In this whitepaper learn how effective consolidation of IT and business resources can enable multiple, meaningful business benefits.
Next gen security for virtualised datacentres
Legacy security solutions are inefficient due to the architectural differences between physical and virtual environments.