Step 10: Create a Message-Driven Bean

In this step you will create the message-driven bean Statistics. The tasks in this step are:


To Create a JMS Queue

Messages for a message-driven bean are not sent directly to the bean but are delivered via the Java Message Service (JMS). To send and receive a JMS message you need to set up a new JMS Queue on the domain's JMS Server:

  1. From the Tools menu, select WebLogic Server-->WebLogic Console.
  2. Sign in using weblogic as username and password.
  3. Under Services Configurations, JMS, click Servers.
  4. Click cgJMSServer.
  5. Click Configure Destinations.
  6. Click Configure a new JMS Queue.
  7. In the Name field, enter Recording Ratings Queue.
  8. In the JNDI field, enter RecordingRatingsQueue.
  9. Click the Create button.

Note. To learn more about JMS, see your favorite J2EE book or the Java documentation at http://java.sun.com.

To Create a Message-Driven Bean

  1. Right-click the bands folder in the Application tab, and select New-->Message-driven Bean.
  2. Enter the file name StatisticsBean.ejb.

  3. Click Create. Notice that a message-driven bean only appears in Source View.

Message-driven beans do not have a Design View. Consequently, the StatisticsBean opens in Source View.

To Listen to the JMS Queue

Now you will ensure that the message-driven bean listens to the JMS queue you created above:

  1. Place the cursor in the @ejbgen:message-driven tag.
  2. In the Property Editor, locate destination-jndi-name and change the entry to RecordingRatingsQueue.
  3. Verify that the destination-type is javax.jms.Queue.


To Reference the Recording Bean

The Statistics EJB will be using the Recording bean to store a recording's statistics. To do so, the Statistics bean must first locate and obtain a reference to the Recording bean's home interface. In this step you modify the Statistics bean's ejbCreate method to locate and obtain that reference:

  1. Go to the beginning of the Statistics bean class definition and define recordingHome as shown in red below:
    public class StatisticsBean
      extends GenericMessageDrivenBean
      implements MessageDrivenBean, MessageListener
    {
      private RecordingHome recordingHome;
    
      public void onMessage(Message msg) {
      ...
  2. Add the ejbCreate method as shown in red below:
    public class StatisticsBean
      extends GenericMessageDrivenBean
      implements MessageDrivenBean, MessageListener
    {
      private RecordingHome recordingHome;
    
      public void ejbCreate() {
        try {
            javax.naming.Context ic = new InitialContext();
            recordingHome = (RecordingHome)ic.lookup("java:/comp/env/ejb/Recording"); 
        }
        catch(NamingException ne) {
            System.out.println("Encountered the following naming exception: " + ne.getMessage());        
        }
      }
      ...
  3. Locate the import section at the top of the file and add the following import statements, or use Alt+Enter when prompted by the IDE to automatically add these import statements:
    import javax.naming.InitialContext;
    import javax.naming.NamingException;


To Add an ejb-local-ref Tag

In the above ejbCreate method you used ejb/Recording to reference the Recording EJB. To map the reference in the Statistics bean to the actual location of the Recording bean at deployment time (via the deployment descriptor), you need to insert an ejbgen:ejb-local-ref tag as shown in bold below:

 * ...
 * @ejbgen:ejb-local-ref link="Recording" 
 */
public class StatisticsBean
... 

To Define the onMessage Method

A message-driven bean's onMessage method is invoked when a message arrives for processing. In this method you implement the business logic of the bean:

  1. Modify the onMessage method as shown in red below:
      public void onMessage(Message msg) {
         try {
            // read the message
            MapMessage recordingMsg = (MapMessage)msg;
            String bandName = recordingMsg.getString("bandName");
            String recordingTitle = recordingMsg.getString("recordingTitle");
            // placeholder logic for the rating
            Random randomGenerator = new Random();
            String rating = new Integer(randomGenerator.nextInt(5)).toString();
            // save the rating with the recording
            Recording album = recordingHome.findByPrimaryKey(new RecordingBeanPK(bandName, recordingTitle));
            album.setRating(rating);
         }
         catch(Exception e) {
            System.out.println("Encountered the following exception: " + e.getMessage());
         }
      }

    Notice that the method uses the Recording bean's (currently undefined) setRating method to store the average rating for this recording. In a later step you will add this accessor method to the Recording bean. Computing the rating information for an album is done by randomly selecting a number between 0 and 4.

  2. Locate the import section at the top of the file and add the following import statement, or use Alt+Enter when prompted by the IDE to automatically add the import statement:
    import java.util.Random;

Because the Recording bean's setRating method is not defined yet, building the EJBs would fail. You first need to update the Recording bean before you can build without generating errors.

Click one of the following arrows to navigate through the tutorial: