In this step you will create the message-driven bean Statistics. The tasks in this step are:
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:
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
Message-driven beans do not have a Design View. Consequently, the StatisticsBean opens in Source View.
Now you will ensure that the message-driven bean listens to the JMS queue you created above:
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:
public class StatisticsBean extends GenericMessageDrivenBean implements MessageDrivenBean, MessageListener { private RecordingHome recordingHome; public void onMessage(Message msg) { ...
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()); } } ...
import javax.naming.InitialContext; import javax.naming.NamingException;
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:
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.
import java.util.Random;