Feeds

Programming message services in Java

Asynchronous interactions seem to be the way of the future

The Power of One Brief: Top reasons to choose HP BladeSystem

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> 

Securing Web Applications Made Simple and Scalable

More from The Register

next story
HIDDEN packet sniffer spy tech in MILLIONS of iPhones, iPads – expert
Don't panic though – Apple's backdoor is not wide open to all, guru tells us
Apple fanbois SCREAM as update BRICKS their Macbook Airs
Ragegasm spills over as firmware upgrade kills machines
NO MORE ALL CAPS and other pleasures of Visual Studio 14
Unpicking a packed preview that breaks down ASP.NET
Captain Kirk sets phaser to SLAUGHTER after trying new Facebook app
William Shatner less-than-impressed by Zuck's celebrity-only app
Do YOU work at Microsoft? Um. Are you SURE about that?
Nokia and marketing types first to get the bullet, says report
Microsoft takes on Chromebook with low-cost Windows laptops
Redmond's chief salesman: We're taking 'hard' decisions
Cheer up, Nokia fans. It can start making mobes again in 18 months
The real winner of the Nokia sale is *drumroll* ... Nokia
EU dons gloves, pokes Google's deals with Android mobe makers
El Reg cops a squint at investigatory letters
prev story

Whitepapers

Designing a Defense for Mobile Applications
Learn about the various considerations for defending mobile applications - from the application architecture itself to the myriad testing technologies.
How modern custom applications can spur business growth
Learn how to create, deploy and manage custom applications without consuming or expanding the need for scarce, expensive IT resources.
Reducing security risks from open source software
Follow a few strategies and your organization can gain the full benefits of open source and the cloud without compromising the security of your applications.
Boost IT visibility and business value
How building a great service catalog relieves pressure points and demonstrates the value of IT service management.
Consolidation: the foundation for IT and business transformation
In this whitepaper learn how effective consolidation of IT and business resources can enable multiple, meaningful business benefits.