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:
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); } }
public class MusicBean extends GenericSessionBean implements SessionBean { private BandHome bandHome; private RecordingHome recordingHome;
...
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:
* @ejbgen:ejb-local-ref link="Recording"
To Add Component Method getRating
/** * @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:
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); } }
import javax.jms.Queue; import javax.jms.QueueConnectionFactory;
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:
* @ejbgen:resource-ref * auth="Container" * jndi-name = "weblogic.jws.jms.QueueConnectionFactory" * name = "jms/QueueConnectionFactory" * type="javax.jms.QueueConnectionFactory"
* @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
/** * @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.
import javax.jms.JMSException; import javax.jms.MapMessage; import javax.jms.QueueConnection; import javax.jms.QueueSender; import javax.jms.QueueSession; import javax.jms.Session;
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: