📄 jms5.html
字号:
<a name="wp81122"> </a><div class="pSmartList1"><li>Now run the <code class="cCode">SimpleProducer</code> program using a topic instead of a queue:</li></div><a name="wp81123"> </a><p class="pBodyRelative"><code class="cCode">appclient -client SimpleProducer.jar jms/Topic topic 3</code></p><a name="wp81125"> </a><p class="pBodyRelative">The output of the program looks like this:</p><a name="wp81126"> </a><p class="pBodyRelative"><code class="cCode">Destination name is jms/Topic, type is topic<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="wp81130"> </a><div class="pSmartList1"><li>Now run the <code class="cCode">SimpleSynchConsumer</code> program using the topic:</li></div><a name="wp81131"> </a><p class="pBodyRelative"><code class="cCode">appclient -client SimpleSynchConsumer.jar jms/Topic topic</code></p><a name="wp81133"> </a><p class="pBodyRelative">The result, however, is different. Because you are using a topic, messages that were sent before you started the consumer cannot be received. (See <a href="JMS3.html#wp78763">Publish/Subscribe Messaging Domain</a>, for details.) Instead of receiving the messages, the program appears to hang. </p><a name="wp81140"> </a><div class="pSmartList1"><li>Run the <code class="cCode">SimpleProducer</code> program again in another terminal window. Now the <code class="cCode">SimpleSynchConsumer</code> program receives the messages:</li></div><a name="wp81141"> </a><p class="pBodyRelative"><code class="cCode">Destination name is jms/Topic, type is topic<br />Reading message: This is message 1<br />Reading message: This is message 2<br />Reading message: This is message 3</code></p></ol></div><a name="wp81196"> </a><h3 class="pHeading2">A Simple Example of Asynchronous Message Consumption</h3><a name="wp81197"> </a><p class="pBody">This section describes the receiving programs in an example that uses a message listener to consume messages asynchronously. This section then explains how to compile and run the programs, using the J2EE 1.4 Application Server.</p><a name="wp81198"> </a><p class="pBody">The following sections describe the steps in creating and running the example:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp81199"> </a><div class="pSmartList1"><li>Writing the client programs</li></div><a name="wp81200"> </a><div class="pSmartList1"><li>Compiling the clients</li></div><a name="wp81201"> </a><div class="pSmartList1"><li>Starting the JMS provider</li></div><a name="wp81202"> </a><div class="pSmartList1"><li>Creating the JMS administered objects</li></div><a name="wp81203"> </a><div class="pSmartList1"><li>Running the clients</li></div><a name="wp81204"> </a><div class="pSmartList1"><li>Deleting the destinations and stopping the server</li></div></ul></div><a name="wp81206"> </a><h4 class="pHeading3">Writing the Client Programs</h4><a name="wp81207"> </a><p class="pBody">The sending program is <code class="cCode"><a href="../examples/jms/simple/src/SimpleProducer.java" target="_blank">src/SimpleProducer.java</a></code>, the same program used in the example in <a href="JMS5.html#wp79938">A Simple Example of Synchronous Message Receives</a>. You may, however, want to comment out the following line of code, where the producer sends a non-text control message to indicate the end of the messages:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">producer.send(session.createMessage());<a name="wp81214"> </a></pre></div><a name="wp81218"> </a><p class="pBody">An asynchronous consumer normally runs indefinitely. This one runs until the user types the letter <code class="cCode">q</code> or <code class="cCode">Q</code> to stop the program, so it does not use the non-text control message.</p><a name="wp81219"> </a><p class="pBody">The receiving program, <code class="cCode"><a href="../examples/jms/simple/src/SimpleAsynchConsumer.java" target="_blank">src/SimpleAsynchConsumer.java</a></code>, performs the following steps:</p><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp81220"> </a><div class="pSmartList1"><li>Performs a JNDI lookup of the <code class="cCode">ConnectionFactory</code> and <code class="cCode">Destination</code></li></div><a name="wp81221"> </a><div class="pSmartList1"><li>Creates a <code class="cCode">Connection</code> and a <code class="cCode">Session</code></li></div><a name="wp81222"> </a><div class="pSmartList1"><li>Creates a <code class="cCode">MessageConsumer</code></li></div><a name="wp81223"> </a><div class="pSmartList1"><li>Creates an instance of the <code class="cCode">TextListener</code> class and registers it as the message listener for the <code class="cCode">MessageConsumer</code>:</li></div><a name="wp85628"> </a><p class="pBodyRelative"><code class="cCode">listener = new TextListener();<br />consumer.setMessageListener(listener);</code></p><a name="wp81224"> </a><div class="pSmartList1"><li>Starts the connection, causing message delivery to begin</li></div><a name="wp81225"> </a><div class="pSmartList1"><li>Listens for the messages published to the destination, stopping when the user types the character <code class="cCode">q</code> or <code class="cCode">Q</code>:</li></div><a name="wp85670"> </a><p class="pBodyRelative"><code class="cCode">System.out.println("To end program, type Q or q, " +<br /> "then <return>");<br />inputStreamReader = new InputStreamReader(System.in);<br />while (!((answer == 'q') || (answer == 'Q'))) {<br /> try {<br /> answer = (char) inputStreamReader.read();<br /> } catch (IOException e) {<br /> System.out.println("I/O exception: " <br /> + e.toString());<br /> }<br />}</code></p><a name="wp95369"> </a><div class="pSmartList1"><li>Closes the connection, which automatically closes the session and <code class="cCode">MessageConsumer</code></li></div></ol></div><a name="wp95370"> </a><p class="pBody">The message listener, <code class="cCode"><a href="../examples/jms/simple/src/TextListener.java" target="_blank">src/TextListener.java</a></code>, follows these steps: </p><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp95371"> </a><div class="pSmartList1"><li>When a message arrives, the <code class="cCode">onMessage</code> method is called automatically.</li></div><a name="wp81229"> </a><div class="pSmartList1"><li>The <code class="cCode">onMessage</code> method converts the incoming message to a <code class="cCode">TextMessage</code> and displays its content. If the message is not a text message, it reports this fact:</li></div><a name="wp86176"> </a><p class="pBodyRelative"><code class="cCode">public void onMessage(Message message) {<br /> TextMessage msg = null;<br /><br /> try {<br /> if (message instanceof TextMessage) {<br /> msg = (TextMessage) message;<br /> System.out.println("Reading message: " +<br /> msg.getText());<br /> } else {<br /> System.out.println("Message is not a " +<br /> "TextMessage");<br /> }<br /> } catch (JMSException e) {<br /> System.out.println("JMSException in onMessage(): " +<br /> e.toString());<br /> } catch (Throwable t) {<br /> System.out.println("Exception in onMessage():" +<br /> t.getMessage());<br /> }<br />}</code></p></ol></div><a name="wp81412"> </a><h4 class="pHeading3">Compiling the Clients</h4><a name="wp81421"> </a><p class="pBody">Compile the programs if you did not do so before or if you edited <code class="cCode">SimpleProducer.java</code> as described in <a href="JMS5.html#wp81206">Writing the Client Programs</a>: </p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">asant build<a name="wp81422"> </a></pre></div><a name="wp81454"> </a><h4 class="pHeading3">Starting the JMS Provider</h4><a name="wp81455"> </a><p class="pBody">If you did not do so before, start the J2EE Application Server in another terminal window.</p><a name="wp81460"> </a><p class="pBody">You will use the connection factories and destinations you created in <a href="JMS5.html#wp80290">Creating JMS Administered Objects</a>.</p><a name="wp92353"> </a><h4 class="pHeading3">Packaging the SimpleAsynchConsumer Client</h4><a name="wp92358"> </a><p class="pBody">If you did not do so before, start <code class="cCode">deploytool</code>.</p><a name="wp92360"> </a><p class="pBody">Package the SimpleAsynchConsumer example as follows:</p><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp92361"> </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="wp92362"> </a><div class="pSmartList1"><li>Select the radio button labeled Create New Stand-Alone AppClient Module.</li></div><a name="wp92363"> </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="wp92364"> </a><div class="pSmartList1"><li>Type <code class="cCode">SimpleAsynchConsumer</code> in the File Name field, then click Choose Module File.</li></div><a name="wp92365"> </a><div class="pSmartList1"><li>Verify that <code class="cCode">SimpleAsynchConsumer</code> appears in the AppClient Name field.</li></div><a name="wp92366"> </a><div class="pSmartList1"><li>Click the Edit button next to the Contents text area. </li></div><a name="wp92367"> </a><div class="pSmartList1"><li>In the dialog box, locate the <code class="cCode">build</code> directory. Select <code class="cCode">SimpleAsynchConsumer.class</code> and <code class="cCode">TextListener.class</code> from the Available Files tree and click Add, then OK</li></div><a name="wp92368"> </a><div class="pSmartList1"><li>In the General screen, select <code class="cCode">SimpleAsynchConsumer</code> in the Main Class combo box.</li></div><a name="wp92369"> </a><div class="pSmartList1"><li>Click Next.</li></div><a name="wp92370"> </a><div class="pSmartList1"><li>Click Finish.</li></div></ol></div><a name="wp92391"> </a><p class="pBody">If you did not already package the SimpleProducer program, do so by following the instructions in <a href="JMS5.html#wp92094">Packaging the Clients</a>.</p><a name="wp81468"> </a><h4 class="pHeading3">Running the Clients</h4><a name="wp86224"> </a><p class="pBody">As before, you run the sample programs using the <code class="cCode">appclient</code> command.</p><a name="wp86225"> </a><p class="pBody">Run the clients as follows. </p><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp81470"> </a><div class="pSmartList1"><li>Run the <code class="cCode">SimpleAsynchConsumer</code> program, specifying the topic <code class="cCode">jms/Topic</code> and its type.</li></div><a name="wp81472"> </a><p class="pBodyRelative"><code class="cCode">appclient -client SimpleAsynchConsumer.jar jms/Topic topic</code></p><a name="wp81477"> </a><p class="pBodyRelative">The program displays the following lines and appears to hang:</p><a name="wp81478"> </a><p class="pBodyRelative"><code class="cCode">Destination name is jms/Topic, type is topic<br />To end program, type Q or q, then <return></code></p><a name="wp81480"> </a><div class="pSmartList1"><li>In another terminal window, run the <code class="cCode">SimpleProducer</code> program, sending three messages. The commands look like this:</li></div><a name="wp81482"> </a><p class="pBodyRelative"><code class="cCode">appclient -client SimpleProducer.jar jms/Topic topic 3</code></p><a name="wp81487"> </a><p class="pBodyRelative">The output of the program looks like this:</p><a name="wp81488"> </a><p class="pBodyRelative"><code class="cCode">Destination name is jms/Topic, type is topic<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="wp81492"> </a><p class="pBodyRelative">In the other window, the <code class="cCode">SimpleAsynchConsumer</code> program displays the following:</p><a name="wp81493"> </a><p class="pBodyRelative"><code class="cCode">Destination name is jms/Topic, type is topic<br />To end program, type Q or q, then <return><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="wp81498"> </a><p class="pBodyRelative">If you did not edit <code class="cCode">SimpleProducer.java</code>, the following line also appears:</p><a name="wp81505"> </a><p class="pBodyRelative"><code class="cCode">Message is not a TextMessage</code></p><a name="wp81506"> </a><div class="pSmartList1"><li>Type <code class="cCode">Q</code> or <code class="cCode">q</code> to stop the program.</li></div><a name="wp81507"> </a><div class="pSmartList1"><li>Now run the programs using a queue. In this case, as with the synchronous example, you can run the <code class="cCode">SimpleProducer</code> program first, because there is no timing dependency between the sender and receiver:</li></div><a name="wp81508"> </a><p class="pBodyRelative"><code class="cCode">appclient -client SimpleProducer.jar jms/Queue queue 3</code></p><a name="wp81510"> </a><p class="pBodyRelative">The output of the program looks like this:</p><a name="wp81511"> </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="wp81515"> </a><div class="pSmartList1"><li>Run the <code class="cCode">SimpleAsynchConsumer</code> program:</li></div><a name="wp81516"> </a><p class="pBodyRelative"><code class="cCode">appclient -client SimpleAsynchConsumer.jar jms/Queue queue</code></p><a name="wp81518"> </a><p class="pBodyRelative">
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -