⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jmsj2eeex3.html

📁 j2eePDF格式的电子书
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<?xml version="1.0" encoding="ISO-8859-1"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">  <head>    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />    <meta http-equiv="Content-Style-Type" content="text/css" />    <title>A J2EE Application that Uses the JMS API with an Entity Bean</title>    <link rel="StyleSheet" href="document.css" type="text/css" media="all" />    <link rel="StyleSheet" href="catalog.css" type="text/css" media="all" />    <link rel="Table of Contents" href="J2EETutorialTOC.html" />    <link rel="Previous" href="JMSJ2EEex2.html" />    <link rel="Next" href="JMSJ2EEex4.html" />    <link rel="Index" href="J2EETutorialIX.html" />  </head>  <body>    <table width="550" summary="layout" id="SummaryNotReq1">      <tr>	<td align="left" valign="center">	<font size="-1">	<a href="http://java.sun.com/j2ee/1.4/download.html#tutorial" target="_blank">Download</a>	<br>	<a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/faq.html" target="_blank">FAQ</a>	<br>	<a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/history.html" target="_blank">History</a>	</td>        <td align="center" valign="center"><a accesskey="p" href="JMSJ2EEex2.html"><img id="LongDescNotReq1" src="images/PrevArrow.gif" width="26" height="26" border="0" alt="Prev" /></a><a accesskey="c" href="J2EETutorialFront.html"><img id="LongDescNotReq1" src="images/UpArrow.gif" width="26" height="26" border="0" alt="Home" /></a><a accesskey="n" href="JMSJ2EEex4.html"><img id="LongDescNotReq3" src="images/NextArrow.gif" width="26" height="26" border="0" alt="Next" /></a><a accesskey="i" href="J2EETutorialIX.html"></a>        </td>	<td align="right" valign="center">	<font size="-1">	<a href="http://java.sun.com/j2ee/1.4/docs/api/index.html" target="_blank">API</a>	<br>	<a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/search.html" target="_blank">Search</a>	<br>	<a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/sendusmail.html" target="_blank">Feedback</a></font>	</font>	</td>      </tr>    </table>    <img src="images/blueline.gif" width="550" height="8" ALIGN="BOTTOM" NATURALSIZEFLAG="3" ALT="Divider">    <blockquote><a name="wp79654"> </a><h2 class="pHeading1">A J2EE Application that Uses the JMS API with an Entity Bean</h2><a name="wp79660"> </a><p class="pBody">This section explains how to write, compile, package, deploy, and run a J2EE application that uses the JMS API with an entity bean. The application uses the following components:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp79661"> </a><div class="pSmartList1"><li>An application client that both sends and receives messages</li></div><a name="wp79662"> </a><div class="pSmartList1"><li>Two message-driven beans</li></div><a name="wp79663"> </a><div class="pSmartList1"><li>An entity bean that uses container-managed persistence</li></div></ul></div><a name="wp79664"> </a><p class="pBody">The section covers the following topics:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp79665"> </a><div class="pSmartList1"><li><a  href="JMSJ2EEex3.html#wp79670">Overview of the Human Resources Application</a></li></div><a name="wp79666"> </a><div class="pSmartList1"><li><a  href="JMSJ2EEex3.html#wp79696">Writing the Application Components</a></li></div><a name="wp79667"> </a><div class="pSmartList1"><li><a  href="JMSJ2EEex3.html#wp86199">Creating and Packaging the Application</a></li></div><a name="wp87106"> </a><div class="pSmartList1"><li><a  href="JMSJ2EEex3.html#wp92269">Deploying the Application</a></li></div><a name="wp87111"> </a><div class="pSmartList1"><li><a  href="JMSJ2EEex3.html#wp86857">Running the Application Client</a></li></div></ul></div><a name="wp79669"> </a><p class="pBody">You will find the source files for this section in the directory <code class="cCode">&lt;</code><code class="cVariable">INSTALL</code><code class="cCode">&gt;/j2eetutorial14/examples/jms/clientmdbentity/</code>. Path names in this section are relative to this directory.</p><a name="wp79670"> </a><h3 class="pHeading2">Overview of the Human Resources Application</h3><a name="wp79671"> </a><p class="pBody">This application simulates, in a simplified way, the work flow of a company's human resources (HR) department when it processes a new hire. This application also demonstrates how to use the J2EE platform to accomplish a task that many JMS client applications perform.</p><a name="wp79672"> </a><p class="pBody">A JMS client must often wait for several messages from various sources. It then uses the information in all these messages to assemble a message that it then sends to another destination. The common term for this process is<em class="cEmphasis"> joining messages</em>. Such a task must be transactional, with all the receives and the send as a single transaction. If all the messages are not received successfully, the transaction can be rolled back. For a client example that illustrates this task, see <a  href="JMS6.html#wp83801">A Local Transaction Example</a>.</p><a name="wp79679"> </a><p class="pBody">A message-driven bean can process only one message at a time in a transaction. To provide the ability to join messages, a J2EE application can have the message-driven bean store the interim information in an entity bean. The entity bean can then determine whether all the information has been received; when it has, the entity bean can create and send the message to the other destination. Once it has completed its task, the entity bean can remove itself.</p><a name="wp79680"> </a><p class="pBody">The basic steps of the application are as follows.</p><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp79681"> </a><div class="pSmartList1"><li>The HR department's application client generates an employee ID for each new hire and then publishes a message containing the new hire's name and employee ID. The client then creates a temporary queue with a message listener that waits for a reply to the message. (See <a  href="JMS6.html#wp81884">Creating Temporary Destinations</a>, for more information.)</li></div><a name="wp79682"> </a><div class="pSmartList1"><li>Two message-driven beans process each message: One bean assigns the new hire's office number, and one bean assigns the new hire's equipment. The first bean to process the message creates an entity bean to store the information it has generated. The second bean locates the existing entity bean and adds its information. </li></div><a name="wp79683"> </a><div class="pSmartList1"><li>When both the office and the equipment have been assigned, the entity bean sends to the reply queue a message describing the assignments, then removes itself. The application client's message listener retrieves the information. </li></div></ol></div><a name="wp79687"> </a><p class="pBody"><a  href="JMSJ2EEex3.html#wp79693">Figure 30-2</a> illustrates the structure of this application. An actual HR application would have more components, of course; other beans could set up payroll and benefits records, schedule orientation, and so on.</p><a name="wp79691"> </a><p class="pBody"></p><div align="left"><img src="images/JMS124.gif" height="289" width="394" alt="A J2EE Application: Client to Message-Driven Beans to Entity Bean" border="0" hspace="0" vspace="0"/></div><p class="pBody"></p><p>  <a name="79693"> </a><strong><font >Figure 30-2    A J2EE Application: Client to Message-Driven Beans to Entity Bean</font></strong></p><a name="wp79696"> </a><h3 class="pHeading2">Writing the Application Components</h3><a name="wp79697"> </a><p class="pBody">Writing the components of the application involves</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp79698"> </a><div class="pSmartList1"><li><a  href="JMSJ2EEex3.html#wp79731">Coding the Application Client: HumanResourceClient.java</a></li></div><a name="wp79699"> </a><div class="pSmartList1"><li><a  href="JMSJ2EEex3.html#wp79746">Coding the Message-Driven Beans</a></li></div><a name="wp79700"> </a><div class="pSmartList1"><li><a  href="JMSJ2EEex3.html#wp79757">Coding the Entity Bean</a></li></div></ul></div><a name="wp79731"> </a><h4 class="pHeading3">Coding the Application Client: HumanResourceClient.java</h4><a name="wp79732"> </a><p class="pBody">The application client program, <code class="cCode"><a  href="../examples/jms/clientmdbentity/src/HumanResourceClient.java" target="_blank">src/HumanResourceClient.java</a></code>, performs the following steps:</p><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp79733"> </a><div class="pSmartList1"><li>Uses the JNDI naming context <code class="cCode">java:comp/env</code> to look up a <code class="cCode">ConnectionFactory</code> and a topic</li></div><a name="wp79734"> </a><div class="pSmartList1"><li>Creates a <code class="cCode">TemporaryQueue</code> to receive notification of processing that occurs, based on new-hire events it has published</li></div><a name="wp79735"> </a><div class="pSmartList1"><li>Creates a <code class="cCode">MessageConsumer</code> for the <code class="cCode">TemporaryQueue</code>, sets the <code class="cCode">MessageConsumer</code>'s message listener, and starts the connection</li></div><a name="wp79736"> </a><div class="pSmartList1"><li>Creates a <code class="cCode">MessageProducer</code> and a <code class="cCode">MapMessage</code></li></div><a name="wp79737"> </a><div class="pSmartList1"><li>Creates five new employees with randomly generated names, positions, and ID numbers (in sequence) and publishes five messages containing this information</li></div></ol></div><a name="wp79738"> </a><p class="pBody">The message listener, <code class="cCode">HRListener</code>, waits for messages that contain the assigned office and equipment for each employee. When a message arrives, the message listener displays the information received and determines whether all five messages have arrived yet. When they have, the message listener notifies the main program, which then exits.</p><a name="wp79746"> </a><h4 class="pHeading3">Coding the Message-Driven Beans</h4><a name="wp79747"> </a><p class="pBody">This example uses two message-driven beans, <code class="cCode"><a  href="../examples/jms/clientmdbentity/src/ReserveEquipmentMsgBean.java" target="_blank">src/ReserveEquipmentMsgBean.java</a></code> and <code class="cCode"><a  href="../examples/jms/clientmdbentity/src/ReserveOfficeMsgBean.java" target="_blank">src/ReserveOfficeMsgBean.java. T</a></code>he beans take the following steps.</p><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp79748"> </a><div class="pSmartList1"><li>The <code class="cCode">ejbCreate</code> method gets a handle to the local home interface of the entity bean.</li></div><a name="wp79749"> </a><div class="pSmartList1"><li>The <code class="cCode">onMessage</code> method retrieves the information in the message. The <code class="cCode">ReserveEquipmentMsgBean</code>'s <code class="cCode">onMessage</code> method chooses equipment, based on the new hire's position; the <code class="cCode">ReserveOfficeMsgBean</code>'s <code class="cCode">onMessage</code> method randomly generates an office number.</li></div><a name="wp79750"> </a><div class="pSmartList1"><li>After a slight delay to simulate real-world processing hitches, the <code class="cCode">onMessage</code> method calls a helper method, <code class="cCode">compose</code>.</li></div><a name="wp79751"> </a><div class="pSmartList1"><li>The <code class="cCode">compose</code> method either creates or finds, by primary key, the <code class="cCode">SetupOffice</code> entity bean and uses it to store the equipment or the office information in the database.</li></div></ol></div><a name="wp79757"> </a><h4 class="pHeading3">Coding the Entity Bean</h4><a name="wp79758"> </a><p class="pBody">The <code class="cCode">SetupOffice</code> bean is an entity bean that uses a local interface. The local interface allows the entity bean and the message-driven beans to be packaged in the same EJB JAR file for maximum efficiency. The entity bean has these components:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp79759"> </a><div class="pSmartList1"><li>The local home interface, <code class="cCode">SetupOfficeLocalHome.java</code></li></div><a name="wp79760"> </a><div class="pSmartList1"><li>The local interface, <code class="cCode">SetupOfficeLocal.java</code></li></div><a name="wp79761"> </a><div class="pSmartList1"><li>The bean class, <code class="cCode">SetupOfficeBean.java</code></li></div></ul></div><a name="wp79771"> </a><p class="pBody">The local home interface source file is <code class="cCode"><a  href="../examples/jms/clientmdbentity/src/SetupOfficeLocalHome.java" target="_blank">src/SetupOfficeLocalHome.java</a></code>. It declares the create method, called <code class="cCode">createLocal</code> for a bean that uses a local interface, and one finder method, <code class="cCode">findByPrimaryKey</code>. </p><a name="wp79777"> </a><p class="pBody">The local interface, <code class="cCode"><a  href="../examples/jms/clientmdbentity/src/SetupOfficeLocal.java" target="_blank">src/SetupOfficeLocal.java</a></code>, declares several business methods that get and manipulate new-hire data.</p><a name="wp79784"> </a><p class="pBody">The bean class, <code class="cCode"><a  href="../examples/jms/clientmdbentity/src/SetupOfficeBean.java" target="_blank">src/SetupOfficeBean.java</a></code>, implements the business methods and their helper method, <code class="cCode">checkIfSetupComplete</code>. The bean class also implements the required methods <code class="cCode">ejbCreateLocal</code>, <code class="cCode">ejbPostCreateLocal</code>, <code class="cCode">setEntityContext</code>, <code class="cCode">unsetEntityContext</code>, <code class="cCode">ejbRemove</code>, <code class="cCode">ejbActivate</code>, <code class="cCode">ejbPassivate</code>, <code class="cCode">ejbLoad</code>, and <code class="cCode">ejbStore</code>. The <code class="cCode">ejbFindByPrimaryKey</code> method is generated automatically. </p><a name="wp79785"> </a><p class="pBody">The only methods called by the message-driven beans are the business methods declared in the local interface, the <code class="cCode">findByPrimaryKey</code> method, and the <code class="cCode">createLocal</code> method. The entity bean uses container-managed persistence, so all database calls are generated automatically.</p><a name="wp86199"> </a><h3 class="pHeading2">Creating and Packaging the Application</h3><a name="wp81655"> </a><p class="pBody">This example uses a connection factory named <code class="cCode">jms/TopicConnectionFactory</code> and a topic named <code class="cCode">jms/Topic</code>, both of which you created in the previous chapter. (See <a  href="JMS5.html#wp80290">Creating JMS Administered Objects</a>, for instructions.) It also uses a JDBC resource named <code class="cCode">jdbc/PointBase</code>, which is enabled by default when you run the Application Server.</p><a name="wp86058"> </a><p class="pBody">Creating and packaging this application involve several steps:</p><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp94537"> </a><div class="pSmartList1"><li>Starting the PointBase server</li></div><a name="wp94538"> </a><div class="pSmartList1"><li>Compiling the source files</li></div><a name="wp94539"> </a><div class="pSmartList1"><li>Creating the application</li></div><a name="wp94540"> </a><div class="pSmartList1"><li>Packaging the entity bean</li></div><a name="wp94541"> </a><div class="pSmartList1"><li>Packaging the message-driven beans</li></div><a name="wp94542"> </a><div class="pSmartList1"><li>Packaging the application client</li></div><a name="wp94543"> </a><div class="pSmartList1"><li>Updating the JNDI names</li></div></ol></div><a name="wp94443"> </a><p class="pBody">You may package the application yourself as an exercise. Use the <code class="cCode">asant</code> <code class="cCode">build</code> target to compile the source files.</p><a name="wp94808"> </a><p class="pBody">This section will use the prepackaged EAR file to show how to create and package the application.</p><a name="wp86080"> </a><h4 class="pHeading3">Examining the Application</h4><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp86182"> </a><div class="pSmartList1"><li>In <code class="cCode">deploytool</code>, open the <code class="cCode">ClientMDBEntityApp.ear</code> file, which resides in the directory <code class="cCode">&lt;</code><code class="cVariable">INSTALL</code><code class="cCode">&gt;/j2eetutorial14/examples/jms/provided-ears</code>.</li></div><a name="wp94482"> </a><div class="pSmartList1"><li>Expand the <code class="cCode">EBJAR</code> node and select the entity bean, <code class="cCode">SetupOffice</code>. </li></div><div class="pSmartList2"><ol type="a" class="pSmartList2"><a name="wp94485"> </a><div class="pSmartList2"><li>In the General tab, notice that the bean uses local interfaces.</li></div><a name="wp94683"> </a><div class="pSmartList2"><li>Click the Entity tab. The bean uses container-managed persistence. </li></div><a name="wp94486"> </a><div class="pSmartList2"><li>In the Entity screen, click the Sun-specific CMP Settings tab. The application uses the preconfigured <code class="cCode">jdbc/PointBase</code> JDBC resource.</li></div><a name="wp94487"> </a><div class="pSmartList2"><li>Click the Resource Refs tab. The bean uses the connection factory <code class="cCode">jms/TopicConnectionFactory</code> to send reply messages to the application client. The bean does not specify any message destination references, however, because it uses a temporary destination for the reply messages. Notice that it uses a <code class="cCode">TopicConnectionFactory</code> object, even though it is using the connection to send messages to a temporary queue.</li></div></ol></div><a name="wp94492"> </a><div class="pSmartList1"><li>Select either of the message-driven beans, <code class="cCode">EquipmentMDB</code> and <code class="cCode">OfficeMDB</code>. They are configured identically.</li></div><div class="pSmartList2"><ol type="a" class="pSmartList2"><a name="wp94493"> </a><div class="pSmartList2"><li>Click the Message-Driven tab. The beans use the <code class="cCode">PhysicalTopic</code> target destination and the connection factory <code class="cCode">jms/TopicConnectionFactory</code>.</li></div><a name="wp94494"> </a><div class="pSmartList2"><li>Click the EJB Refs tab. Both beans reference the entity bean using local references.</li></div></ol></div><a name="wp94498"> </a><div class="pSmartList1"><li>Select the <code class="cCode">HumanResourceClient</code> node.</li></div><div class="pSmartList2"><ol type="a" class="pSmartList2"><a name="wp94499"> </a><div class="pSmartList2"><li>Click the Resource Refs tab. The client uses the connection factory <code class="cCode">jms/TopicConnectionFactory</code> both to send messages to a topic and to receive messages from a temporary queue. The application looks up the coded name <code class="cCode">jms/MyConnectionFactory</code> and casts the object to an object of type <code class="cCode">javax.jms.ConnectionFactory</code>.</li></div><a name="wp94517"> </a><div class="pSmartList2"><li>Click the Msg Dest Refs tab. The coded name <code class="cCode">jms/NewHireTopic</code> refers to the target destination <code class="cCode">PhysicalTopic</code>.</li></div><a name="wp94502"> </a><div class="pSmartList2"><li>Click the Message Destinations tab, then click <code class="cCode">PhysicalTopic</code>. The client appears in the Producers area, and the message-driven beans appear in the Consumers area. All of them refer to the JNDI name <code class="cCode">jms/Topic</code>.</li></div></ol></div><a name="wp94530"> </a><div class="pSmartList1"><li>Notice that for all the components, the Transactions tab is set to Container-Managed.</li></div><a name="wp94577"> </a><div class="pSmartList1"><li>Select the <code class="cCode">ClientMDBEntityApp</code> node and click the JNDI Names tab. The JNDI name for the message-driven beans is the topic destination resource, <code class="cCode">jms/Topic</code>.</li></div></ol></div><a name="wp94571"> </a><p class="pBody">Verify that the JNDI names for the application components are correct. They should appear as shown in <a  href="JMSJ2EEex3.html#wp92319">Table 30-3</a> and <a  href="JMSJ2EEex3.html#wp92375">Table 30-4</a>.</p><div align="left"><table border="1" summary="Application Pane for ClientMDBEntityApp" id="wp92319">  <caption><a name="wp92319"> </a><div class="pTableTitle">Table 30-3   Application Pane for ClientMDBEntityApp</div></caption>  <tr align="center">    <th><a name="wp92325"> </a><div class="pCellHeading">

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -