日期:2014-05-19  浏览次数:20521 次

Message Driven Bean in Java EE 5

In the past, I posted a few examples of implementing Messaging using J2EE and Spring. In this post, I will give an example of how to implement Message Driven beans using Java EE 5. I used Eclipse 3.2 and Glassfish for this example. Follow these steps to run the example:

  1. Download and Install Glassfish : You can download the latest build of Glassfish from the Glassfish Download site . To install follow these steps
    1. In the download directory, run the following command
      java -Xmx256m -jar glassfish-installer-version-build.jar
    2. The previous command will create a directory by the name glassfish. Go to the glassfish directory and run this command
      ant -f setup-cluster.xml
    3. The admin console for the default installation will be at http://localhost:4848/asadmin, and the default username and password are "admin" and "adminadmin" respectively.
  2. Download and Install the Glassfish Plugin for Eclipse from here.
  3. Create a Glassfish Server in Eclipse : (For some reason, Eclipse did not detect the Server Runtime without creating a Server, we'll worry about that later)
  4. Creating the EJB 3 Message Driven Bean:
    1. Create a "Java project" in Eclipse.
    2. Add the Glassfish runtime library as a dependency for the project.
    3. The following is the code for the Message Driven Bean that I used for the Example. This is in the jms package of the Java project.
      package jms;
      
      import javax.annotation.Resource;
      import javax.ejb.MessageDriven;
      import javax.ejb.MessageDrivenContext;
      import javax.jms.JMSException;
      import javax.jms.Message;
      import javax.jms.MessageListener;
      import javax.jms.TextMessage;
      
      @MessageDriven(mappedName = "jms/testQueue")
      
      public class Messaging3Mdb implements MessageListener {
      
        @Resource
      
        private MessageDrivenContext mdc
      ;
      
       public Messaging3Mdb() {
       }
       public void onMessage(Message inMessage) {
         TextMessage msg = null;
         try {
           msg = (TextMessage) inMessage;
           System.out.println("Message received : " + msg.getText());
         } catch (JMSException e) {
           e.printStackTrace();
           mdc.setRollbackOnly();
         }
       }
      }
      Messaging3Mdb.java
  5. Creating the Client: I used a Servlet for the client, so that I could also use JMS resource injection . To create the Client
    1. Create a "Dynamic Web Project" in Eclipse.
    2. Change the Web.xml file to Reflect Java EE 5 descriptor, as shown below
      <?xml version="1.0" encoding="UTF-8"?>
      <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
       <display-name>Messaging3Web</display-name>
       <servlet>
         <description></description>
         <display-name>MessagingClient</display-name>
         <servlet-name>MessagingClient</servlet-name>
         <servlet-class>servlets.MessagingClient</servlet-class>
       </servlet>
       <servlet-mapping>
         <servlet-name>MessagingClient</servlet-name>
         <url-pattern>/MessagingClient</url-pattern>
       </servlet-mapping>
       <welcome-file-list>
         <welcome-file>index.html</welcome-file>
         <welcome-file>index.htm</welcome-file>
         <welcome-file>index.jsp</welcome-file>
         <welcome-file>default.html</welcome-file>
         <welcome-file>default.htm</welcome-file>
         <welcome-file