📄 jms5.html
字号:
<a name="wp90135"> </a><p class="pBody">To compile the examples, do the following.</p><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp80273"> </a><div class="pSmartList1"><li>In a terminal window, go to the following directory:</li></div><a name="wp94236"> </a><p class="pBodyRelative"><code class="cCode"><</code><code class="cVariable">INSTALL</code><code class="cCode">>/j2eetutorial14/examples/jms/simple/</code></p><a name="wp85586"> </a><div class="pSmartList1"><li>Type the following command:</li></div><a name="wp80274"> </a><p class="pBodyRelative"><code class="cCode">asant build</code></p></ol></div><a name="wp85593"> </a><p class="pBody">This command uses the <code class="cCode">build.xml</code> file in the <code class="cCode">simple</code> directory to compile all the source files in the directory. The class files are placed in the <code class="cCode">build</code> directory. </p><a name="wp80279"> </a><h4 class="pHeading3">Starting the JMS Provider</h4><a name="wp80280"> </a><p class="pBody">When you use the J2EE Application Server, your JMS provider is the Application Server. Start the server as described in <a href="WebApp3.html#wp213803">Starting and Stopping the J2EE Application Server</a>.</p><a name="wp80290"> </a><h4 class="pHeading3">Creating JMS Administered Objects</h4><a name="wp80291"> </a><p class="pBody">Creating the JMS administered objects for this section involves the following:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp96164"> </a><div class="pSmartList1"><li>Starting the Admin Console</li></div><a name="wp96165"> </a><div class="pSmartList1"><li>Creating two connection factories</li></div><a name="wp96166"> </a><div class="pSmartList1"><li>Creating two physical destinations</li></div><a name="wp96167"> </a><div class="pSmartList1"><li>Creating two destination resources</li></div></ul></div><a name="wp96168"> </a><p class="pBody">If you built and ran the <code class="cCode">SimpleMessage</code> example in Chapter <a href="MDB.html#wp79663">23</a> and did not delete the resources afterwards, you need to create only half of these resources, those that involve topics.</p><a name="wp96297"> </a><p class="pBody">To start the Admin Console, follow the instructions in <a href="WebApp3.html#wp213824">Starting the Admin Console</a>.</p><a name="wp96169"> </a><p class="pBody">To create the connection factories, perform the following steps:</p><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp96024"> </a><div class="pSmartList1"><li>Expand the Java Message Service node, then click Connection Factories.</li></div><a name="wp96036"> </a><div class="pSmartList1"><li>Click New. The Create JMS Connection Factory screen appears.</li></div><a name="wp96145"> </a><div class="pSmartList1"><li>In the JNDI Name field, type <code class="cCode">jms/QueueConnectionFactory</code>.</li></div><a name="wp96146"> </a><div class="pSmartList1"><li>Choose<code class="cCode"> javax.jms.QueueConnectionFactory</code> from the Type menu.</li></div><a name="wp96147"> </a><div class="pSmartList1"><li>Select the Enabled checkbox.</li></div><a name="wp96151"> </a><div class="pSmartList1"><li>Click OK.</li></div><a name="wp96152"> </a><div class="pSmartList1"><li>Click New again.</li></div><a name="wp96155"> </a><div class="pSmartList1"><li>In the JNDI Name field, type <code class="cCode">jms/TopicConnectionFactory</code>.</li></div><a name="wp96156"> </a><div class="pSmartList1"><li>Choose<code class="cCode"> javax.jms.TopicConnectionFactory</code> from the Type menu.</li></div><a name="wp96157"> </a><div class="pSmartList1"><li>Select the Enabled checkbox.</li></div><a name="wp96153"> </a><div class="pSmartList1"><li>Click OK.</li></div></ol></div><a name="wp96174"> </a><p class="pBody">To create the physical destinations, perform the following steps:</p><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp96184"> </a><div class="pSmartList1"><li>In the left-hand pane, click Physical Destinations.</li></div><a name="wp96185"> </a><div class="pSmartList1"><li>Click New. The Create Physical Destination pane appears.</li></div><a name="wp96186"> </a><div class="pSmartList1"><li>In the Physical Destination Name field, type <code class="cCode">PhysicalQueue</code>.</li></div><a name="wp96187"> </a><div class="pSmartList1"><li>Choose <code class="cCode">queue</code> from the Type menu.</li></div><a name="wp96188"> </a><div class="pSmartList1"><li>Click OK.</li></div><a name="wp96189"> </a><div class="pSmartList1"><li>Click New again.</li></div><a name="wp96192"> </a><div class="pSmartList1"><li>In the Physical Destination Name field, type <code class="cCode">PhysicalTopic</code>.</li></div><a name="wp96193"> </a><div class="pSmartList1"><li>Choose <code class="cCode">topic</code> from the Type menu.</li></div><a name="wp96194"> </a><div class="pSmartList1"><li>Click OK.</li></div></ol></div><a name="wp96179"> </a><p class="pBody">To create the destination resources and link them to the physical destinations, perform the following steps:</p><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp96204"> </a><div class="pSmartList1"><li>In the left-hand pane, click Destination Resources.</li></div><a name="wp96205"> </a><div class="pSmartList1"><li>Click New. The Create JMS Destination Resource pane appears.</li></div><a name="wp96206"> </a><div class="pSmartList1"><li>In the JNDI Name field, type <code class="cCode">jms/Queue</code>.</li></div><a name="wp96207"> </a><div class="pSmartList1"><li>Choose <code class="cCode">javax.jms.Queue</code> from the Type menu.</li></div><a name="wp96224"> </a><div class="pSmartList1"><li>Select the Enabled checkbox.</li></div><a name="wp96422"> </a><div class="pSmartList1"><li>Under Additional Properties, click Add.</li></div><a name="wp96423"> </a><div class="pSmartList1"><li>Type <code class="cCode">Name</code> in the Name field.</li></div><a name="wp96424"> </a><div class="pSmartList1"><li>Type <code class="cCode">PhysicalQueue</code> in the Value field.</li></div><a name="wp96208"> </a><div class="pSmartList1"><li>Click OK.</li></div><a name="wp96209"> </a><div class="pSmartList1"><li>Click New again.</li></div><a name="wp96210"> </a><div class="pSmartList1"><li>In the JNDI Name field, type <code class="cCode">jms/Topic</code>.</li></div><a name="wp96211"> </a><div class="pSmartList1"><li>Choose <code class="cCode">javax.jms.Topic</code> from the Type menu.</li></div><a name="wp96228"> </a><div class="pSmartList1"><li>Select the Enabled checkbox.</li></div><a name="wp96426"> </a><div class="pSmartList1"><li>Under Additional Properties, click Add.</li></div><a name="wp96427"> </a><div class="pSmartList1"><li>Enter <code class="cCode">Name</code> in the Name field.</li></div><a name="wp96428"> </a><div class="pSmartList1"><li>Enter <code class="cCode">PhysicalTopic</code> in the Value field.</li></div><a name="wp96212"> </a><div class="pSmartList1"><li>Click OK.</li></div></ol></div><a name="wp96294"> </a><p class="pBody">If you are in a hurry, you can create all the administered objects described in this section by using the following <code class="cCode">asant</code> target:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">asant create-resources<a name="wp91874"> </a></pre></div><a name="wp91873"> </a><p class="pBody">This target is in the file <code class="cCode"><</code><code class="cVariable">INSTALL</code><code class="cCode">>/j2eetutorial14/examples/jms/common/targets.xml</code>, so you can use it from any of the JMS examples directories.</p><a name="wp95847"> </a><p class="pBody">If you built and ran the <code class="cCode">SimpleMessage</code> example in Chapter <a href="MDB.html#wp79663">23</a> and did not delete the resources afterwards, use the following <code class="cCode">asant</code> target to create only the resources that do not exist already:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">asant create-topic-resources<a name="wp95851"> </a></pre></div><a name="wp92094"> </a><h4 class="pHeading3">Packaging the Clients</h4><a name="wp92095"> </a><p class="pBody">In order to run these examples using the J2EE Application Server, you need to package each one in a standalone client JAR file.</p><a name="wp92098"> </a><p class="pBody">First, start <code class="cCode">deploytool</code>:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp92660"> </a><div class="pSmartList1"><li>On Windows systems, choose Start<span style="font-family: Symbol"><img src="images/arrwrite.gif" border="0" alt="Right Arrow"></span>Programs<span style="font-family: Symbol"><img src="images/arrwrite.gif" border="0" alt="Right Arrow"></span>Sun Microsystems<span style="font-family: Symbol"><img src="images/arrwrite.gif" border="0" alt="Right Arrow"></span>J2EE 1.4 SDK<span style="font-family: Symbol"><img src="images/arrwrite.gif" border="0" alt="Right Arrow"></span>Deploytool. </li></div><a name="wp92662"> </a><div class="pSmartList1"><li>On UNIX systems, use the <code class="cCode">deploytool</code> command.</li></div></ul></div><a name="wp92096"> </a><p class="pBody">Package the SimpleProducer example as follows:</p><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp92101"> </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 Client to start the Application Client Wizard.</li></div><a name="wp92104"> </a><div class="pSmartList1"><li>Select the radio button labeled Create New Stand-Alone AppClient Module.</li></div><a name="wp92107"> </a><div class="pSmartList1"><li>Click Browse next to the AppClient Location field and navigate to the <code class="cCode"><</code><code class="cVariable">INSTALL</code><code class="cCode">>/j2eetutorial14/examples/jms/simple/</code> directory.</li></div><a name="wp92116"> </a><div class="pSmartList1"><li>Type <code class="cCode">SimpleProducer</code> in the File Name field and click Choose Module File.</li></div><a name="wp92117"> </a><div class="pSmartList1"><li>Verify that <code class="cCode">SimpleProducer</code> appears in the AppClient Name field.</li></div><a name="wp92128"> </a><div class="pSmartList1"><li>Click the Edit button next to the Contents text area. </li></div><a name="wp92133"> </a><div class="pSmartList1"><li>In the dialog box, locate the <code class="cCode">build</code> directory. Select <code class="cCode">SimpleProducer.class</code> from the Available Files tree and click Add, then OK</li></div><a name="wp92148"> </a><div class="pSmartList1"><li>In the General screen, select <code class="cCode">SimpleProducer</code> in the Main Class combo box.</li></div><a name="wp92157"> </a><div class="pSmartList1"><li>Click Next.</li></div><a name="wp92158"> </a><div class="pSmartList1"><li>Click Finish.</li></div></ol></div><a name="wp92097"> </a><p class="pBody">Package the SimpleSynchConsumer example the same way, except for the following:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp92159"> </a><div class="pSmartList1"><li>In Step 4, type <code class="cCode">SimpleSynchConsumer.jar</code> in the File Name field.</li></div><a name="wp92166"> </a><div class="pSmartList1"><li>In Step 5, verify that <code class="cCode">SimpleSynchConsumer</code> appears in the AppClient Name field.</li></div><a name="wp92175"> </a><div class="pSmartList1"><li>In Step 7, select <code class="cCode">SimpleSynchConsumer.class</code> from the Available Files tree and click Add, then OK.</li></div><a name="wp92216"> </a><div class="pSmartList1"><li>In Step 8, select <code class="cCode">SimpleSynchConsumer</code> in the Main Class combo box.</li></div></ul></div><a name="wp81091"> </a><h4 class="pHeading3">Running the Clients</h4><a name="wp81092"> </a><p class="pBody">You run the sample programs using the appclient command. Each of the programs takes command-line arguments: a destination name, a destination type, and, for SimpleProducer, a number of messages.</p><a name="wp86200"> </a><p class="pBody">Run the clients as follows.</p><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp91170"> </a><div class="pSmartList1"><li>Run the <code class="cCode">SimpleProducer</code> program, sending three messages to the queue <code class="cCode">jms/Queue</code>:</li></div><a name="wp81095"> </a><p class="pBodyRelative"><code class="cCode">appclient -client SimpleProducer.jar jms/Queue queue 3</code></p><a name="wp81100"> </a><p class="pBodyRelative">The output of the program looks like this, along with some output from the AppClient container:</p><a name="wp81101"> </a><p class="pBodyRelative"><code class="cCode">Destination name is jms/Queue, type is queue<br />Sending message: This is message 1<br />Sending message: This is message 2<br />Sending message: This is message 3</code></p><a name="wp85621"> </a><p class="pBodyRelative">The messages are now in the queue, waiting to be received.</p><a name="wp81105"> </a><div class="pSmartList1"><li>In the same window, run the <code class="cCode">SimpleSynchConsumer</code> program, specifying the queue name and type:</li></div><a name="wp81107"> </a><p class="pBodyRelative"><code class="cCode">appclient -client SimpleSynchConsumer.jar jms/Queue queue</code></p><a name="wp81115"> </a><p class="pBodyRelative">The output of the program looks like this:</p><a name="wp81116"> </a><p class="pBodyRelative"><code class="cCode">Destination name is jms/Queue, type is queue<br />Reading message: This is message 1<br />Reading message: This is message 2<br />Reading message: This is message 3</code></p><a name="wp81120"> </a><div class="pSmartList1"><li>Now try running the programs in the opposite order. Run the <code class="cCode">SimpleSynchConsumer</code> program. It displays the queue name and then appears to hang, waiting for messages.</li></div><a name="wp81121"> </a><div class="pSmartList1"><li>In a different terminal window, run the <code class="cCode">SimpleProducer</code> program. When the messages have been sent, the <code class="cCode">SimpleSynchConsumer</code> program receives them and exits.</li></div>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -