Step 12: Update the Music Bean

The Music bean will send a message to the JMS queue whenever a new recording is added by the user. In this step you will you will modify the component method addRecording to send the message. Also you will add the component method getRating to retrieve the rating information of a recording from the Recording bean.

The tasks in this step are:


To Reference the Recording Bean

The Music EJB will be invoking a method of the Recording EJB. To do so, it must first locate and obtain a reference to the Recording bean's home. In this step, you modify the ejbCreate method in MusicBean.ejb to locate and obtain a reference:

  1. In the Application pane, double-click MusicBean.ejb and select the Source View tab.
  2. Locate the ejbCreate method and modify its definition as shown in red below:
       public void ejbCreate() {
          try {
             javax.naming.Context ic = new InitialContext();
             bandHome = (BandHome)ic.lookup("java:comp/env/ejb/BandEJB");
             recordingHome = (RecordingHome) ic.lookup("java:comp/env/ejb/Recording");
    	  } 
          catch (NamingException ne) {
             throw new EJBException(ne);
          }
       }
  3. Go to the beginning of the class definition and define recordingHome as shown in red below:
    public class MusicBean extends GenericSessionBean implements SessionBean {
       private BandHome bandHome;
       private RecordingHome recordingHome;
    ...

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 Music 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:

  1. Ensure that the Music EJB is displayed in Design View.
  2. Right-click the Design View, and select Insert EJB Gentag-->ejb-local-ref. A new ejbgen:ejb-local-ref section appears in the Property Editor.
  3. Use the Property Editor to enter the link property, or go to Source View to add this property:
     * @ejbgen:ejb-local-ref link="Recording"

To Add Component Method getRating

  1. Ensure that the Music EJB is displayed in Design View.
  2. Right-click the MusicBean and choose Add Component Method. A component method called void method() appears in the Component Methods section.
  3. Rename the method String getRating(String band, String recordingTitle).
  4. Right-click the arrow to the left of the component method and select Local. The business method is now defined in the local interface.
  5. Click the component method to go to Source View and modify the method as shown in red below:
       /**
        * @ejbgen:local-method
        */
       public String getRating(String band, String recordingTitle)
       {
          try {        
             Recording theRecording = recordingHome.findByPrimaryKey(new RecordingBeanPK(band, recordingTitle));        
             return theRecording.getRating();     
          }     
          catch(FinderException fe) {        
             throw new EJBException(fe);     
          } 
       }

    Notice that the method uses the Recording bean's findByPrimaryKey method to locate the recording and uses the recording bean's method getRating to retrieve the rating.

To Reference the JMS Provider and Destination

In order to send a JMS message, the Music bean will need to connect to the JMS provider and it will need to have a destination queue for the message. Next you will modify the ejbCreate method to locate and obtain a reference to a QueueConnectionFactory that is defined by default in the workshop domain, and the JMS Queue you defined in step 10 of the tutorial:

  1. In the Application pane, double-click MusicBean.ejb and ensure you are in Source View.
  2. Locate the ejbCreate method in MusicBean.ejb, and modify its definition as shown in red below:
       public void ejbCreate() {
          try {
             javax.naming.Context ic = new InitialContext();
             bandHome = (BandHome)ic.lookup("java:comp/env/ejb/BandEJB");
             recordingHome = (RecordingHome) ic.lookup("java:comp/env/ejb/Recording");
             factory = (QueueConnectionFactory) ic.lookup("java:comp/env/jms/QueueConnectionFactory");
             queue = (Queue) ic.lookup("java:comp/env/jms/RecordingRatingsQueue");
          } 
          catch (NamingException ne) {
             throw new EJBException(ne);
          }
       }
  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.jms.Queue;
    import javax.jms.QueueConnectionFactory;
  4. Go to the beginning of the class definition and define QueueConnectionFactory and Queue as shown in red below:
    public class MusicBean
      extends GenericSessionBean
      implements SessionBean
    {
      private BandHome bandHome;
      private RecordingHome recordingHome;
      private QueueConnectionFactory factory;
      private Queue queue;
      ...

To Add resource-ref and resource-env-ref Tags

In the above step you used jms/QueueConnectionFactory to reference the JMS provider and you used jms/RecordingRatingsQueue to locate the JMS queue. To map these reference to the actual locations at deployment time (via the deployment descriptor), you need to insert resource-ref and resource-env-ref tags:

  1. Ensure that the Music EJB is displayed in Design View.
  2. Right-click the Music bean and choose Insert EJBGentag-->resource-ref. A new ejbgen:resource-ref section appears in the Property Editor.
  3. Define the reference to the JMS provider in the Property Editor by entering the following properties in the respective fields, or go to Source View to add these properties:
     * @ejbgen:resource-ref
     *   auth="Container"
     *   jndi-name = "weblogic.jws.jms.QueueConnectionFactory"
     *   name = "jms/QueueConnectionFactory"
     *   type="javax.jms.QueueConnectionFactory"
  4. In Design View, right-click the Music bean and choose Insert EJBGentag-->resource-env-ref. A new ejbgen:resource-env-ref section appears in the Property Editor.
  5. Define the local link in the Property Editor by entering the following properties in the respective fields, or go to Source View to add these properties:
     * @ejbgen:resource-env-ref
     *  jndi-name="RecordingRatingsQueue" 
     *  name="jms/RecordingRatingsQueue" 
     *  type = "javax.jms.Queue" 

Note. You can verify the JNDI name of the QueueConnectionFactory via the WebLogic console. You will find the link Connection Factories under Services Configurations, JMS. For more information, see step 10 of the tutorial. To learn more about these tags, place your cursor inside the tag in Source View, and press F1.

To Modify Component Method addRecording

  1. Ensure that the Music bean is displayed in Design View.
  2. Click the method void addRecording(String band, String recording) to go to Source View and modify the method as shown in red below:
        /**
         * @ejbgen:local-method
         */
        public void addRecording(String band, String recording)
        {
          try {
             Band bandBean = bandHome.findByPrimaryKey(new BandPK(band));
             if(bandBean != null) {
                bandBean.addRecording(recording);
                //send a message
                QueueConnection connect = factory.createQueueConnection();
                QueueSession session = connect.createQueueSession(true,Session.AUTO_ACKNOWLEDGE);
                QueueSender sender = session.createSender(queue);
                MapMessage recordingMsg = session.createMapMessage();
                recordingMsg.setString("bandName", band);
                recordingMsg.setString("recordingTitle", recording);
                sender.send(recordingMsg);
                connect.close(); 
           }
          }
          catch(CreateException ce) {
             throw new EJBException(ce);
          }
          catch(FinderException fe) {
             throw new EJBException(fe);
          }
          catch(JMSException ne) {
             throw new EJBException(ne);
          }
        }
    

    Notice that the method creates a message containing the name of the band and the recording title, and sends this to the JMS queue.

  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.jms.JMSException;
    import javax.jms.MapMessage;
    import javax.jms.QueueConnection;
    import javax.jms.QueueSender;
    import javax.jms.QueueSession;
    import javax.jms.Session;
  4. Save your results.

To Build the EJBs

  1. Locate the Build window in the IDE. If the Build window is not there, select View-->Windows-->Build.
  2. In the Application pane, right-click MyEJBProject, and select Build MyEJBProject.
  3. Monitor the Build window and verify that the build completes correctly, and that the EJB JAR MyEJBProject.jar is created.
  4. After the build completes, the EJBs are deployed: Watch the green ball in the WebLogic Server status bar at the bottom of WebLogic Workshop turn yellow with the message Updating Server. Wait until the ball turns green again. The two EJBs are now deployed on the server. In addition, the various web applications that were predefined are deployed to the web container. We will turn to these in the next step.
  5. (Optional.) Verify the contents of the EJB JAR. In the Application pane, expand Modules, expand MyEJBProject.jar, and expand the directory structure reflecting the package name. Verify that the bean classes and the various interfaces have been created.

If you encounter build errors, verify that the above instructions have been followed. A build error might be followed by a deployment error, in particular for WebAppOne_Standard and related to an unresolved ejb-link. The link cannot be resolved because the EJB JAR was not created. Fixing the build problem should resolve this deployment error. If you encounter deployment errors that seem to be related to one of the web applications, make sure that you build the EJB project and not the entire application (because the application contains page flow applications that have different EJB dependencies from what you have created in this step).

Also, you can compare your code for Music Bean with the solution source code located at [BEA_HOME]\weblogic81\samples\platform\tutorial_resources\EJBTutorial\solutions.

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