📄 jmsj2eeex4.html
字号:
<?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 Consumes Messages from a Remote J2EE Server</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="JMSJ2EEex3.html" /> <link rel="Next" href="JMSJ2EEex5.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="JMSJ2EEex3.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="JMSJ2EEex5.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="wp83233"> </a><h2 class="pHeading1">An Application Example that Consumes Messages from a Remote J2EE Server</h2><a name="wp83230"> </a><p class="pBody">This section and the following section both explain how to write, compile, package, deploy, and run a pair of J2EE applications that use the JMS API and run on two J2EE servers. A common practice is to deploy different components of an enterprise application on different systems within a company, and these examples illustrate on a small scale how to do this for an application that uses the JMS API.</p><a name="wp83646"> </a><p class="pBody">However, the two examples work in slightly different ways. In this first example, the deployment information for a message-driven bean specifies the remote server from which it will <em class="cEmphasis">consume</em> messages. In the next example, the same bean is deployed on two different servers, so it is the client application that specifies the servers (one local, one remote) to which it is <em class="cEmphasis">sending</em> messages.</p><a name="wp83261"> </a><p class="pBody">This first example divides the example in Chapter <a href="MDB.html#wp79663">23</a> into two applications, one containing the application client and the other containing the message-driven bean.</p><a name="wp83284"> </a><p class="pBody">The section covers the following topics:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp83285"> </a><div class="pSmartList1"><li><a href="JMSJ2EEex4.html#wp83303">Overview of the Applications</a></li></div><a name="wp83286"> </a><div class="pSmartList1"><li><a href="JMSJ2EEex4.html#wp83400">Writing the Application Components</a></li></div><a name="wp83287"> </a><div class="pSmartList1"><li><a href="JMSJ2EEex4.html#wp83324">Creating and Packaging the Applications</a></li></div><a name="wp83288"> </a><div class="pSmartList1"><li><a href="JMSJ2EEex4.html#wp87874">Deploying the Applications</a></li></div><a name="wp88038"> </a><div class="pSmartList1"><li><a href="JMSJ2EEex4.html#wp87612">Running the Application Client</a></li></div></ul></div><a name="wp83345"> </a><p class="pBody">You will find the source files for this section in <code class="cCode"><</code><code class="cVariable">INSTALL</code><code class="cCode">>/j2eetutorial14/examples/jms/consumeremote/</code>. Path names in this section are relative to this directory.</p><a name="wp83303"> </a><h3 class="pHeading2">Overview of the Applications</h3><a name="wp83308"> </a><p class="pBody">Except for the fact that it is packaged as two separate applications, this example is very similar to the one in Chapter <a href="MDB.html#wp79663">23</a>:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp83307"> </a><div class="pSmartList1"><li>One application contains the application client, which runs on the remote server and sends three messages to a queue.</li></div><a name="wp83304"> </a><div class="pSmartList1"><li>The other application contains the message-driven bean, which consumes the messages from the queue on the remote server.</li></div></ul></div><a name="wp83366"> </a><p class="pBody">The basic steps of the applications are as follows.</p><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp83367"> </a><div class="pSmartList1"><li>The administrator starts two J2EE servers, one on each system.</li></div><a name="wp83368"> </a><div class="pSmartList1"><li>On the remote server, the administrator deploys the client application, which sends three messages to a queue.</li></div><a name="wp83370"> </a><div class="pSmartList1"><li>On the local server, the administrator deploys the message-driven bean application, which specifies the URL of the remote server where the client is deployed.</li></div><a name="wp83371"> </a><div class="pSmartList1"><li>The message-driven bean consumes the messages.</li></div></ol></div><a name="wp83364"> </a><p class="pBody"><a href="JMSJ2EEex4.html#wp83282">Figure 30-3</a> illustrates the structure of this application. You can see that it is almost identical to <a href="MDB2.html#wp79728">Figure 23-1</a> except that there are two J2EE servers. The queue is not associated with either server; it must exist on both servers.</p><a name="wp83397"> </a><p class="pBody"></p><div align="left"><img src="images/ConsumeRemote5.gif" height="119" width="392" alt="A J2EE Application that Consumes Messages from a Remote Server" border="0" hspace="0" vspace="0"/></div><p class="pBody"></p><p> <a name="83282"> </a><strong><font >Figure 30-3 A J2EE Application that Consumes Messages from a Remote Server</font></strong></p><a name="wp83400"> </a><h3 class="pHeading2">Writing the Application Components</h3><a name="wp83406"> </a><p class="pBody">Writing the components of the applications involves</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp83407"> </a><div class="pSmartList1"><li>Coding the application client</li></div><a name="wp83408"> </a><div class="pSmartList1"><li>Coding the message-driven bean</li></div></ul></div><a name="wp83323"> </a><p class="pBody">The application client, <code class="cCode"><a href="../examples/jms/consumeremote/earthclient/src/SimpleClient.java" target="_blank">earthclient/src/SimpleClient.java</a></code>, is almost identical to the one in <a href="MDB3.html#wp79730">The J2EE Application Client</a>.</p><a name="wp87829"> </a><p class="pBody">Similarly, the message-driven bean, <code class="cCode"><a href="../examples/jms/consumeremote/marsmdb/src/MessageBean.java" target="_blank">marsmdb/src/MessageBean.java,</a></code> is almost identical to the one in <a href="MDB4.html#wp79739">The Message-Driven Bean Class</a>.</p><a name="wp83466"> </a><p class="pBody">The only major difference is that the client and bean are packaged in two separate applications.</p><a name="wp83324"> </a><h3 class="pHeading2">Creating and Packaging the Applications</h3><a name="wp87194"> </a><p class="pBody">For this example, the message-driven bean uses the connection factory named <code class="cCode">jms/EarthQueueConnectionFactory</code>, which you created in <a href="JMS5.html#wp96341">Creating Administered Objects for Multiple Systems</a>. The application client can use any connection factory that exists on the remote server; since you created <code class="cCode">jms/EarthQueueConnectionFactory</code> on that server, you can use that. Both components use the queue named <code class="cCode">jms/Queue</code>, which you created in <a href="JMS5.html#wp80290">Creating JMS Administered Objects</a>.</p><a name="wp87195"> </a><p class="pBody">We'll suppose, as we did in <a href="JMS5.html#wp81600">Running JMS Client Programs on Multiple Systems</a>, that the two servers are named <code class="cCode">earth</code> and <code class="cCode">mars</code>.</p><a name="wp87209"> </a><p class="pBody">Creating and packaging this application involve several steps:</p><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp94692"> </a><div class="pSmartList1"><li>Compiling the source files</li></div><a name="wp94693"> </a><div class="pSmartList1"><li>Creating the application</li></div><a name="wp94694"> </a><div class="pSmartList1"><li>Packaging the application client</li></div><a name="wp94695"> </a><div class="pSmartList1"><li>Packaging the message-driven bean</li></div><a name="wp94696"> </a><div class="pSmartList1"><li>Verifying the JNDI names</li></div></ol></div><a name="wp94713"> </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">earthclient</code> and <code class="cCode">marsmdb</code> directories to compile the source files.</p><a name="wp94892"> </a><p class="pBody">This section will use the prepackaged EAR files to show how to create and package the applications.</p><a name="wp90932"> </a><p class="pBody">Which system you use to package and deploy the applications and which system you use to run the client depends on your network configuration--which file system you can access remotely. These instructions assume that you can access the file system of <code class="cCode">earth</code> from <code class="cCode">mars</code>, but cannot access the file system of <code class="cCode">mars</code> from <code class="cCode">earth</code>. (You can use the same systems for <code class="cCode">earth</code> and <code class="cCode">mars</code> that you used in <a href="JMS5.html#wp81600">Running JMS Client Programs on Multiple Systems</a>.)</p><a name="wp94718"> </a><p class="pBody">The J2EE Application Server must be running on both systems.</p><a name="wp93005"> </a><p class="pBody">You package and deploy the applications from <code class="cCode">mars</code>. The only action you perform on <code class="cCode">earth</code> is running the client application.</p><a name="wp94738"> </a><h4 class="pHeading3">Examining the Applications</h4><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp94739"> </a><div class="pSmartList1"><li>In <code class="cCode">deploytool</code>, on <code class="cCode">mars</code>, open the two EAR files <code class="cCode">EarthClientApp.ear</code> and <code class="cCode">MarsMDBApp.ear</code>, which reside in the directory <code class="cCode"><</code><code class="cVariable">INSTALL</code><code class="cCode">>/j2eetutorial14/jms/provided-ears</code>.</li></div><a name="wp94752"> </a><div class="pSmartList1"><li>In <code class="cCode">EarthClientApp.ear</code>, select the application client node, <code class="cCode">SimpleClient</code>.</li></div><div class="pSmartList2"><ol type="a" class="pSmartList2"><a name="wp94760"> </a><div class="pSmartList2"><li>Click the Resource Refs tab. The client uses the connection factory <code class="cCode">jms/QueueConnectionFactory</code> to send messages to a 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="wp94761"> </a><div class="pSmartList2"><li>Click the Msg Dest Refs tab. The coded name <code class="cCode">jms/QueueName</code> refers to the target destination <code class="cCode">PhysicalQueue</code>.</li></div><a name="wp94762"> </a><div class="pSmartList2"><li>Click the Message Destinations tab, then click <code class="cCode">PhysicalQueue</code>. The client appears in the Producers area. It refers to the JNDI name <code class="cCode">jms/Queue</code>.</li></div></ol></div><a name="wp94755"> </a><div class="pSmartList1"><li>In <code class="cCode">MarsMDBApp.ear</code>, expand the <code class="cCode">MDBJAR</code> node and select <code class="cCode">MessageBean</code>.</li></div><div class="pSmartList2"><ol type="a" class="pSmartList2"><a name="wp94796"> </a><div class="pSmartList2"><li>Click the Message-Driven tab. The bean uses the <code class="cCode">PhysicalQueue</code> target destination and the connection factory <code class="cCode">jms/EarthQueueConnectionFactory</code>.</li></div><a name="wp94797"> </a><div class="pSmartList2"><li>Click the Transactions tab. The bean uses container-managed transactions.</li></div></ol></div><a name="wp94794"> </a><div class="pSmartList1"><li>Select the <code class="cCode">MDBJAR</code> node, click the Message Destinations tab, then click <code class="cCode">PhysicalQueue</code>. The message-driven bean appears in the Consumers area. It refers to the JNDI name <code class="cCode">jms/Queue</code>.</li></div><a name="wp94848"> </a><div class="pSmartList1"><li>Select the <code class="cCode">MarsMDBApp</code> node and click the JNDI Names tab. The JNDI name for the message-driven bean is the queue destination resource, <code class="cCode">jms/Queue</code>.</li></div></ol></div><a name="wp87511"> </a><p class="pBody">The JNDI name for the <code class="cCode">MarsMDBApp</code> application should appear as shown in <a href="JMSJ2EEex4.html#wp92463">Table 30-5</a>. Only the Application Pane has any content.</p><div align="left"><table border="1" summary="Application Pane for RemoteMDBApp" id="wp92463"> <caption><a name="wp92463"> </a><div class="pTableTitle">Table 30-5 Application Pane for MarsMDBApp</div></caption> <tr align="center"> <th><a name="wp92469"> </a><div class="pCellHeading">Component Type
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -