Feeds

Programming message services in Java

Asynchronous interactions seem to be the way of the future

Providing a secure and efficient Helpdesk

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> 

Providing a secure and efficient Helpdesk

More from The Register

next story
New 'Cosmos' browser surfs the net by TXT alone
No data plan? No WiFi? No worries ... except sluggish download speed
iOS 8 release: WebGL now runs everywhere. Hurrah for 3D graphics!
HTML 5's pretty neat ... when your browser supports it
Mathematica hits the Web
Wolfram embraces the cloud, promies private cloud cut of its number-cruncher
Mozilla shutters Labs, tells nobody it's been dead for five months
Staffer's blog reveals all as projects languish on GitHub
'People have forgotten just how late the first iPhone arrived ...'
Plus: 'Google's IDEALISM is an injudicious justification for inappropriate biz practices'
SUSE Linux owner Attachmate gobbled by Micro Focus for $2.3bn
Merger will lead to mainframe and COBOL powerhouse
iOS 8 Healthkit gets a bug SO Apple KILLS it. That's real healthcare!
Not fit for purpose on day of launch, says Cupertino
prev story

Whitepapers

Providing a secure and efficient Helpdesk
A single remote control platform for user support is be key to providing an efficient helpdesk. Retain full control over the way in which screen and keystroke data is transmitted.
A strategic approach to identity relationship management
ForgeRock commissioned Forrester to evaluate companies’ IAM practices and requirements when it comes to customer-facing scenarios versus employee-facing ones.
Saudi Petroleum chooses Tegile storage solution
A storage solution that addresses company growth and performance for business-critical applications of caseware archive and search along with other key operational systems.
WIN a very cool portable ZX Spectrum
Win a one-off portable Spectrum built by legendary hardware hacker Ben Heck
The next step in data security
With recent increased privacy concerns and computers becoming more powerful, the chance of hackers being able to crack smaller-sized RSA keys increases.