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

📄 jmsj2eeex5.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>An Application Example that Deploys a Message-Driven Bean on Two J2EE Servers</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="JMSJ2EEex4.html" />    <link rel="Next" href="CB.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="JMSJ2EEex4.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="CB.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="wp79839"> </a><h2 class="pHeading1">An Application Example that Deploys a Message-Driven Bean on Two J2EE Servers</h2><a name="wp79847"> </a><p class="pBody">This section, like the previous one, explains how to write, compile, package, deploy, and run a pair of J2EE applications that use the JMS API and run on two J2EE servers. The applications are slightly more complex than the ones in the first example.</p><a name="wp79848"> </a><p class="pBody">The applications use the following components:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp79849"> </a><div class="pSmartList1"><li>An application client that uses two connection factories--one ordinary one and one that is configured to communicate with the remote server--to create two publishers and two subscribers and to publish and to consume messages</li></div><a name="wp79850"> </a><div class="pSmartList1"><li>A message-driven bean that is deployed twice--once on the local server and once on the remote one--to process the messages and to send replies</li></div></ul></div><a name="wp79851"> </a><p class="pBody">In this section, the term <em class="cEmphasis">local server</em> means the server on which both the application client and the message-driven bean are deployed (<code class="cCode">mars</code> in the previous example). The term <em class="cEmphasis">remote server</em> means the server on which only the message-driven bean is deployed (<code class="cCode">earth</code> in the previous example).</p><a name="wp79853"> </a><p class="pBody">The section covers the following topics:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp79854"> </a><div class="pSmartList1"><li><a  href="JMSJ2EEex5.html#wp79860">Overview of the Applications</a></li></div><a name="wp79855"> </a><div class="pSmartList1"><li><a  href="JMSJ2EEex5.html#wp79883">Writing the Application Components</a></li></div><a name="wp79856"> </a><div class="pSmartList1"><li><a  href="JMSJ2EEex5.html#wp88050">Creating and Packaging the Applications</a></li></div><a name="wp91262"> </a><div class="pSmartList1"><li><a  href="JMSJ2EEex5.html#wp88520">Deploying the Applications</a></li></div><a name="wp91267"> </a><div class="pSmartList1"><li><a  href="JMSJ2EEex5.html#wp88567">Running the Application Client</a></li></div></ul></div><a name="wp79859"> </a><p class="pBody">You will find the source files for this section in <code class="cCode">&lt;</code><code class="cVariable">INSTALL</code><code class="cCode">&gt;/j2eetutorial14/examples/jms/sendremote/</code>. Path names in this section are relative to this directory.</p><a name="wp79860"> </a><h3 class="pHeading2">Overview of the Applications</h3><a name="wp79864"> </a><p class="pBody">This pair of applications is somewhat similar to the applications in <a  href="JMSJ2EEex4.html#wp83233">An Application Example that Consumes Messages from a Remote J2EE Server</a> in that the only components are a client and a message-driven bean. However, the applications here use these components in more complex ways. One application consists of the application client. The other application contains only the message-driven bean and is deployed twice, once on each server.</p><a name="wp79865"> </a><p class="pBody">The basic steps of the applications are as follows.</p><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp79866"> </a><div class="pSmartList1"><li>You start two J2EE servers, one on each system.</li></div><a name="wp79867"> </a><div class="pSmartList1"><li>On the local server (<code class="cCode">mars</code>), you create a connection factory to communicate with the remote server (<code class="cCode">earth</code>). On the remote server, you create a connection factory with the same name.</li></div><a name="wp79868"> </a><div class="pSmartList1"><li>The application client uses two connection factories--an ordinary one and the one that communicates with the remote server--to create two connections, sessions, publishers, and subscribers. Each publisher publishes five messages.</li></div><a name="wp79869"> </a><div class="pSmartList1"><li>The local and the remote message-driven beans each receive five messages and send replies.</li></div><a name="wp79870"> </a><div class="pSmartList1"><li>The client's message listener consumes the replies.</li></div></ol></div><a name="wp79874"> </a><p class="pBody"><a  href="JMSJ2EEex5.html#wp79880">Figure 30-4</a> illustrates the structure of this application.</p><a name="wp79878"> </a><p class="pBody"></p><div align="left"><img src="images/JMS132.gif" height="253" width="386" alt="A J2EE Application that Sends Messages to Two Servers" border="0" hspace="0" vspace="0"/></div><p class="pBody"></p><p>  <a name="79880"> </a><strong><font >Figure 30-4    A J2EE Application that Sends Messages to Two Servers</font></strong></p><a name="wp79883"> </a><h3 class="pHeading2">Writing the Application Components</h3><a name="wp79884"> </a><p class="pBody">Writing the components of the applications involves</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp79885"> </a><div class="pSmartList1"><li><a  href="JMSJ2EEex5.html#wp79892">Coding the Application Client: MultiAppServerClient.java</a></li></div><a name="wp79886"> </a><div class="pSmartList1"><li><a  href="JMSJ2EEex5.html#wp80151">Coding the Message-Driven Bean: ReplyMsgBean.java</a></li></div></ul></div><a name="wp79892"> </a><h4 class="pHeading3">Coding the Application Client: MultiAppServerClient.java</h4><a name="wp79893"> </a><p class="pBody">The application client class, <code class="cCode"><a  href="../examples/jms/sendremote/multiclient/src/MultiAppServerClient.java" target="_blank">multiclient/src/MultiAppServerClient.java,</a></code> does the following.</p><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp79894"> </a><div class="pSmartList1"><li>It uses the JNDI naming context <code class="cCode">java:comp/env</code> to look up two connection factories and a topic.</li></div><a name="wp79895"> </a><div class="pSmartList1"><li>For each connection factory, it creates a connection, a publisher session, a publisher, a subscriber session, a subscriber, and a temporary topic for replies.</li></div><a name="wp79896"> </a><div class="pSmartList1"><li>Each subscriber sets its message listener, <code class="cCode">ReplyListener</code>, and starts the connection.</li></div><a name="wp79897"> </a><div class="pSmartList1"><li>Each publisher publishes five messages and creates a list of the messages the listener should expect.</li></div><a name="wp79898"> </a><div class="pSmartList1"><li>When each reply arrives, the message listener displays its contents and removes it from the list of expected messages.</li></div><a name="wp79899"> </a><div class="pSmartList1"><li>When all the messages have arrived, the client exits.</li></div></ol></div><a name="wp80151"> </a><h4 class="pHeading3">Coding the Message-Driven Bean: ReplyMsgBean.java</h4><a name="wp80152"> </a><p class="pBody">The <code class="cCode">onMessage</code> method of the message-driven bean class, <code class="cCode"><a  href="../examples/jms/sendremote/replybean/src/ReplyMsgBean.java" target="_blank">replybean/src/ReplyMsgBean.java</a></code>, does the following:</p><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp80153"> </a><div class="pSmartList1"><li>Casts the incoming message to a <code class="cCode">TextMessage</code> and displays the text</li></div><a name="wp80154"> </a><div class="pSmartList1"><li>Creates a connection, session, and publisher for the reply message</li></div><a name="wp80155"> </a><div class="pSmartList1"><li>Publishes the message to the reply topic</li></div><a name="wp80156"> </a><div class="pSmartList1"><li>Closes the connection</li></div></ol></div><a name="wp83813"> </a><p class="pBody">On both servers, the bean will consume messages from the topic <code class="cCode">jms/Topic</code>.</p><a name="wp88050"> </a><h3 class="pHeading2">Creating and Packaging the Applications</h3><a name="wp88051"> </a><p class="pBody">This example uses the connection factory named <code class="cCode">jms/TopicConnectionFactory</code> and the topic named <code class="cCode">jms/Topic</code>. These objects must exist on both the local and remote servers. If you need to, you can create the objects there using the Admin Console, as described in <a  href="JMS5.html#wp80290">Creating JMS Administered Objects</a>.</p><a name="wp95131"> </a><p class="pBody">This example uses an additional connection factory, <code class="cCode">jms/EarthQueueConnectionFactory</code>, that communicates with the remote system; you created it in <a  href="JMS5.html#wp96341">Creating Administered Objects for Multiple Systems</a>. Because connection factories are not specific to a domain unless you are using a durable subscriber) you can use a <code class="cCode">QueueConnectionFactory</code> object when you are sending messages to a topic. This connection factory needs to exist on the local server only.</p><a name="wp88059"> </a><p class="pBody">Creating and packaging this application involve several steps:</p><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp94913"> </a><div class="pSmartList1"><li>Creating the connection factories</li></div><a name="wp95156"> </a><div class="pSmartList1"><li>Compiling the source files</li></div><a name="wp94914"> </a><div class="pSmartList1"><li>Creating the applications</li></div><a name="wp94924"> </a><div class="pSmartList1"><li>Packaging the application client</li></div><a name="wp94933"> </a><div class="pSmartList1"><li>Packaging the message-driven bean</li></div><a name="wp94934"> </a><div class="pSmartList1"><li>Updating the JNDI names</li></div></ol></div><a name="wp94945"> </a><p class="pBody">You may package the applications yourself as an exercise. Use the <code class="cCode">asant</code> <code class="cCode">build</code> targets in the <code class="cCode">multiclient</code> and <code class="cCode">replybean</code> directories to compile the source files.</p><a name="wp94954"> </a><p class="pBody">This section will use the prepackaged EAR files to show how to create and package the applications. You can use the systems <code class="cCode">mars</code> and <code class="cCode">earth</code> for the local and remote systems.</p><a name="wp94966"> </a><p class="pBody">The J2EE Application Server must be running on both systems. You package, deploy, and run the application from the local system.</p><a name="wp95028"> </a><h4 class="pHeading3">Examining the Applications</h4><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp95029"> </a><div class="pSmartList1"><li>In <code class="cCode">deploytool</code>, on the local system, open the two EAR files <code class="cCode">MultiClientApp.ear </code>and <code class="cCode">ReplyBeanApp.ear</code>, which reside in the directory <code class="cCode">&lt;</code><code class="cVariable">INSTALL</code><code class="cCode">&gt;/j2eetutorial14/jms/provided-ears</code>.</li></div><a name="wp95048"> </a><div class="pSmartList1"><li>In <code class="cCode">MultiClientApp.ear</code>, select the application client node, <code class="cCode">MultiAppServerClient</code>.</li></div><div class="pSmartList2"><ol type="a" class="pSmartList2"><a name="wp95049"> </a><div class="pSmartList2"><li>Click the Resource Refs tab. The client looks up two connection factories and casts them to objects of type <code class="cCode">javax.jms.ConnectionFactory</code>. The coded name <code class="cCode">jms/ConnectionFactory1</code> refers to <code class="cCode">jms/TopicConnectionFactory</code>, while the coded name <code class="cCode">jms/ConnectionFactory2</code> refers to <code class="cCode">jms/EarthQueueConnectionFactory</code>.</li></div><a name="wp95050"> </a><div class="pSmartList2"><li>Click the Msg Dest Refs tab. The coded name <code class="cCode">jms/TopicName</code> refers to the target destination <code class="cCode">PhysicalTopic</code>.</li></div><a name="wp95051"> </a><div class="pSmartList2"><li>Click the Message Destinations tab, then click <code class="cCode">PhysicalTopic</code>. The client appears in the Producers area. It refers to the JNDI name <code class="cCode">jms/Topic</code>. This is the destination where messages are sent. Replies will come to a temporary destination.</li></div></ol></div><a name="wp95052"> </a><div class="pSmartList1"><li>In <code class="cCode">ReplyBeanApp.ear</code>, expand the <code class="cCode">MDBJAR</code> node and select <code class="cCode">ReplyMsgBean</code>.</li></div><div class="pSmartList2"><ol type="a" class="pSmartList2"><a name="wp95053"> </a><div class="pSmartList2"><li>Click the Message-Driven tab. The bean uses the <code class="cCode">PhysicalTopic</code> target destination and the connection factory <code class="cCode">jms/TopicConnectionFactory</code>.</li></div><a name="wp95054"> </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. The bean 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>. The bean does not look up a topic for the reply messages; instead, it uses the temporary topic specified in the incoming message's <code class="cCode">JMSReplyTo</code> header field.</li></div><a name="wp95159"> </a><div class="pSmartList2"><li>Click the Transactions tab. The bean uses container-managed transactions.</li></div></ol></div><a name="wp95055"> </a><div class="pSmartList1"><li>Select the <code class="cCode">MDBJAR</code> node, click the Message Destinations tab, then click <code class="cCode">PhysicalTopic</code>. The message-driven bean appears in the Consumers area. It refers to the JNDI name <code class="cCode">jms/Topic</code>.</li></div><a name="wp95056"> </a><div class="pSmartList1"><li>Select the <code class="cCode">ReplyBeanApp</code> node and click the JNDI Names tab. The JNDI name for the message-driven bean is the topic destination resource, <code class="cCode">jms/Topic</code>.</li></div></ol></div><a name="wp95195"> </a><p class="pBody">Verify that the JNDI names for the applications are correct.</p><a name="wp88290"> </a><p class="pBody">The Application Pane for <code class="cCode">ReplyBeanApp</code> should appear as shown in <a  href="JMSJ2EEex5.html#wp92556">Table 30-7</a>. </p><div align="left"><table border="1" summary="Application Pane for ReplyBeanApp" id="wp92556">  <caption><a name="wp92556"> </a><div class="pTableTitle">Table 30-7   Application Pane for ReplyBeanApp</div></caption>  <tr align="center">    <th><a name="wp92562"> </a><div class="pCellHeading">Component Type</div></th>    <th><a name="wp92564"> </a><div class="pCellHeading">Component</div></th>    <th><a name="wp92566"> </a><div class="pCellHeading">JNDI Name</div></th></tr>  <tr align="left">    <td><a name="wp92568"> </a><div class="pCellBody">EJB</div></td>    <td><a name="wp92570"> </a><div class="pCellBody">ReplyMsgBean</div></td>    <td><a name="wp92572"> </a><div class="pCellBody">jms/Topic</div></td></tr></table></div><p class="pBody"></p><a name="wp88487"> </a><p class="pBody">The References Pane for <code class="cCode">ReplyBeanApp</code> should appear as shown in <a  href="JMSJ2EEex5.html#wp88464">Table 30-8</a>.</p><div align="left"><table border="1" summary="References Pane for ReplyBeanApp" id="wp88464">  <caption><a name="wp88464"> </a><div class="pTableTitle">Table 30-8   References Pane for ReplyBeanApp</div></caption>  <tr align="center">    <th><a name="wp92538"> </a><div class="pCellHeading">Ref. Type</div></th>    <th><a name="wp92540"> </a><div class="pCellHeading">Referenced By</div></th>    <th><a name="wp92542"> </a><div class="pCellHeading">Reference Name

⌨️ 快捷键说明

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