Consuming JMS messages using MDBs

Message-driven beans (MDBs) make consuming JMS messages a lot easier. With just a couple of annotations and implementing the onMessage method, you can make any Java object a consumer of JMS messages. In this section, we will implement an MDB to consume messages from the Course queue. To implement MDBs, we need to create an EJB project. Select File | New | EJB Project from the main menu:

Figure 10.9: Create a EJB project to implement MDBs

Enter Project name as CourseManagementEJB. Click Next. Accept the default values on the subsequent pages and click Finish on the last page.

Right-click on the project and select the New | Message-Driven Bean option. This opens the MDB creation wizard:

Figure 10.10: MDB creation wizard – class file information

Enter packt.jee.eclipse.jms.mdb as Java package and CourseMDB as Class name. Keep Destination type as Queue.

Destination name is the physical destination name that we specified when creating the queue and is not the JNDI name:

Figure 10.11: JMS queue physical destination name in the GlassFish admin console

Enter CourseManagementQueue as Destination type. Click Next. Accept the default values on the second page and click Finish. The wizard generates the following code:

@MessageDriven( 
    activationConfig = { 
      @ActivationConfigProperty(propertyName = "destinationType", 
            propertyValue = "javax.jms.Queue"), 
      @ActivationConfigProperty(propertyName = "destination", 
            propertyValue = "CourseManagementQueue") 
    }, 
    mappedName = "jms/courseManagementQueue") 
public class CourseMDB implements MessageListener { 
 
    /** 
     * Default constructor. 
     */ 
    public CourseMDB() { 
        // TODO Auto-generated constructor stub 
    } 
 
  /** 
     * @see MessageListener#onMessage(Message) 
     */ 
    public void onMessage(Message message) { 
        System.out.println("addCourse message received in 
CourseMDB"); } }

The class is annotated with @MessageDriven with activationConfig and the JMS destination parameters specified in the wizard. It also creates the onMessage method. In this method, we just print the message that the MDB received for adding a course. To process ObjectMessage in this class, we will have to refactor the CourseDTO class to a shared .jar between EJB and the web project. This is left to the readers as an exercise.

The JEE container creates a pool of MDB objects for a single MDB class. An incoming message can be handled by any one of the instances of MDB in the pool. This can help in building a scalable message processing application.

If you want to test the MDB, add the project to the GlassFish Server configured in Eclipse. To do this, right-click on the configured server in the Servers view of Eclipse and select the Add and Remove... option. Select the CourseManagementEJB project that we created and click Finish. Make sure that the server is started and the status is [Started, Synchronized]. You also need to add the CourseManagementJMSWeb project to the server, because we have JSF and JSP pages to add a course in that project. Run addCourse.xhtml or addCourse.jsp from the CourseManagementJMSWeb project, add a course, and check the GlassFish console in Eclipse for messages printed from message receivers and the MDB we created in this section. However, note that either the MDB or one of the queue listeners we developed in CourseManagementJMSWeb will be receiving the message, and not all of the receivers.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.12.157