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

📄 jmsj2eeex6.html

📁 j2eePDF格式的电子书
💻 HTML
📖 第 1 页 / 共 3 页
字号:
<?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="JMSJ2EEex5.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="JMSJ2EEex5.html"><img id="LongDescNotReq1" src="images/PrevArrow.gif" width="26" height="26" border="0" alt="Prev" /></a><a accesskey="c" href="Front.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. The term <em class="cEmphasis">remote server</em> means the server on which only the message-driven bean is deployed.</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="JMSJ2EEex6.html#wp79860">Overview of the Applications</a></li></div><a name="wp79855"> </a><div class="pSmartList1"><li><a  href="JMSJ2EEex6.html#wp79883">Writing the Application Components</a></li></div><a name="wp79856"> </a><div class="pSmartList1"><li><a  href="JMSJ2EEex6.html#wp88050">Creating and Packaging the Applications</a></li></div><a name="wp91262"> </a><div class="pSmartList1"><li><a  href="JMSJ2EEex6.html#wp88520">Deploying the Applications</a></li></div><a name="wp91267"> </a><div class="pSmartList1"><li><a  href="JMSJ2EEex6.html#wp88567">Saving the Client JAR and Running the Application Client</a></li></div><a name="wp79857"> </a><div class="pSmartList1"><li><a  href="JMSJ2EEex6.html#wp88694">Undeploying and Removing the Applications</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="JMSJ2EEex5.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>The administrator starts two J2EE servers, one on each system.</li></div><a name="wp79867"> </a><div class="pSmartList1"><li>On the local server, the administrator creates a connection factory to communicate with the remote server.</li></div><a name="wp79868"> </a><div class="pSmartList1"><li>The application client uses two connection factories--an ordinary one and the one just created--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="JMSJ2EEex6.html#wp79880">Figure 29-5</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 29-5    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="JMSJ2EEex6.html#wp79892">Coding the Application Client: MultiAppServerClient.java</a></li></div><a name="wp79886"> </a><div class="pSmartList1"><li><a  href="JMSJ2EEex6.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">multiclient/src/MultiAppServerClient.java,</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">replybean/src/ReplyMsgBean.java</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, it 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>, both of which you created in <a  href="JMS5.html#wp79822">Writing Simple JMS Client Applications</a>. It uses an additional connection factory, <code class="cCode">jms/RemoteTopicConnectionFactory</code>, that communicates with the remote system.</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="wp88063"> </a><div class="pSmartList1"><li><a  href="JMSJ2EEex6.html#wp88081">Starting the J2EE Application Server and Compiling the Source Files</a></li></div><a name="wp88102"> </a><div class="pSmartList1"><li><a  href="JMSJ2EEex6.html#wp83801">Creating the Connection Factory</a></li></div><a name="wp88067"> </a><div class="pSmartList1"><li><a  href="JMSJ2EEex6.html#wp88183">Starting the J2EE Deploytool and Creating the Applications</a></li></div><a name="wp88071"> </a><div class="pSmartList1"><li><a  href="JMSJ2EEex3.html#wp89825">Packaging the Application Client</a></li></div><a name="wp88075"> </a><div class="pSmartList1"><li><a  href="JMSJ2EEex6.html#wp88255">Packaging the Message-Driven Bean</a></li></div><a name="wp88079"> </a><div class="pSmartList1"><li><a  href="JMSJ2EEex6.html#wp88283">Verifying and Updating the JNDI Names</a></li></div></ol></div><a name="wp88081"> </a><h4 class="pHeading3">Starting the J2EE Application Server and Compiling the Source Files</h4><a name="wp88151"> </a><p class="pBody">You need to start the J2EE Application Server on both the local and remote systems. You can perform all other tasks on the local system.</p><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp88082"> </a><div class="pSmartList1"><li>Start the J2EE Application Server on the local system, if it is not already running.</li></div><a name="wp88164"> </a><div class="pSmartList1"><li>Start the J2EE Application Server on the remote system, if it is not already running.</li></div><a name="wp88132"> </a><div class="pSmartList1"><li>On the local system, go to the directory <code class="cCode">multiclient</code>. Compile the source files:</li></div><a name="wp88133"> </a><p class="pBodyRelative"><code class="cCode">asant build</code></p><a name="wp88088"> </a><div class="pSmartList1"><li>Also on the local system, go to the directory <code class="cCode">replybean</code>. Compile the source files:</li></div><a name="wp88089"> </a><p class="pBodyRelative"><code class="cCode">asant build</code></p></ol></div><a name="wp83801"> </a><h4 class="pHeading3">Creating the Connection Factory</h4><a name="wp88119"> </a><p class="pBody">To create a connection factory that will communicate with the remote server, perform the following steps:</p><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp88225"> </a><div class="pSmartList1"><li>Use the following command, replacing <code class="cVariable">sys-name</code> with the name of your remote system:</li></div><a name="wp88120"> </a><p class="pBodyRelative"><code class="cCode">asadmin create-jms-resource --resourcetype <br />javax.jms.TopicConnectionFactory --property <br />imqAddressList=sys-name jms/RemoteTopicConnectionFactory</code></p><a name="wp88121"> </a><div class="pSmartList1"><li>Reconfigure the server:</li></div><a name="wp88224"> </a><p class="pBodyRelative"><code class="cCode">asant reconfig_common</code></p><a name="wp88223"> </a><div class="pSmartList1"><li>To verify that the factory was created, use the following command:</li></div><a name="wp88122"> </a><p class="pBodyRelative"><code class="cCode">asadmin list-jms-resources --user admin</code></p></ol></div><a name="wp88746"> </a><p class="pBody">For this example, the remote server must have the <code class="cCode">TopicConnectionFactory</code> and topic you created in <a  href="JMS5.html#wp79822">Writing Simple JMS Client Applications</a>. If you have not run any of the tutorial examples on the remote system, run the following <code class="cCode">asant</code> targets on it:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">asant create-tcfasant create-topicasant reconfig_common<a name="wp88752"> </a></pre></div><a name="wp88183"> </a><h4 class="pHeading3">Starting the J2EE Deploytool and Creating the Applications</h4><a name="wp88184"> </a><p class="pBody">You can perform all the packaging and deployment tasks on the local system.</p><a name="wp88185"> </a><p class="pBody">Do the following to create the application for the client:</p><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp88186"> </a><div class="pSmartList1"><li>Start the deploytool.</li></div><a name="wp88188"> </a><div class="pSmartList1"><li>Choose File<span style="font-family: Symbol"><img src="images/arrwrite.gif" border="0" alt="Right Arrow"></span>New<span style="font-family: Symbol"><img src="images/arrwrite.gif" border="0" alt="Right Arrow"></span>Application EAR. </li></div><a name="wp88189"> </a><div class="pSmartList1"><li>Click Browse next to the Application File Name field and use the file chooser to locate the directory <code class="cCode">multiclient</code>. </li></div><a name="wp88190"> </a><div class="pSmartList1"><li>In the File Name field, type <code class="cCode">MultiClientApp</code>.</li></div><a name="wp88191"> </a><div class="pSmartList1"><li>Click New Application.</li></div><a name="wp88192"> </a><div class="pSmartList1"><li>Click OK.</li></div></ol></div><a name="wp88193"> </a><p class="pBody">

⌨️ 快捷键说明

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