📄 jms5.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>Writing Simple JMS Client Applications</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="JMS4.html" /> <link rel="Next" href="JMS6.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="JMS4.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="JMS6.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="wp79822"> </a><h2 class="pHeading1">Writing Simple JMS Client Applications</h2><a name="wp79827"> </a><p class="pBody">This section shows how to create, package, and run simple JMS client programs packaged as standalone J2EE application clients. These clients access a server based on J2EE technology ("J2EE server"). The clients demonstrate the basic tasks that a JMS application must perform: </p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp79828"> </a><div class="pSmartList1"><li>Creating a connection and a session</li></div><a name="wp79829"> </a><div class="pSmartList1"><li>Creating message producers and consumers</li></div><a name="wp79830"> </a><div class="pSmartList1"><li>Sending and receiving messages</li></div></ul></div><a name="wp79831"> </a><p class="pBody">In a J2EE application, some of these tasks are performed, in whole or in part, by the container. If you learn about these tasks, you will have a good basis for understanding how a JMS application works on the J2EE platform.</p><a name="wp79832"> </a><p class="pBody">The section covers the following topics:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp79833"> </a><div class="pSmartList1"><li>Setting your environment to run J2EE applications</li></div><a name="wp79834"> </a><div class="pSmartList1"><li>An example that uses synchronous message receives </li></div><a name="wp79835"> </a><div class="pSmartList1"><li>An example that uses a message listener</li></div><a name="wp79836"> </a><div class="pSmartList1"><li>Running JMS clients on multiple systems</li></div></ul></div><a name="wp79840"> </a><p class="pBody">Each example uses two programs: one that sends messages and one that receives them. You can run the programs in two terminal windows.</p><a name="wp79841"> </a><p class="pBody">When you write a JMS application to run in a J2EE application, you use many of the same methods in much the same sequence as you do for a standalone application client. However, there are some significant differences. <a href="JMS7.html#wp82114">Using the JMS API in a J2EE Application</a> describes these differences, and the next chapter provides examples that illustrate them.</p><a name="wp85603"> </a><p class="pBody">The examples for this section are in the following directory:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><<code class="cVariable">INSTALL</code>>/j2eetutorial14/examples/jms/simple/<a name="wp94277"> </a></pre></div><a name="wp79938"> </a><h3 class="pHeading2">A Simple Example of Synchronous Message Receives</h3><a name="wp79939"> </a><p class="pBody">This section describes the sending and receiving programs in an example that uses the <code class="cCode">receive</code> method to consume messages synchronously. This section then explains how to compile, package, and run the programs, using the J2EE 1.4 Application Server.</p><a name="wp79940"> </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="wp79941"> </a><div class="pSmartList1"><li><a href="JMS5.html#wp79947">Writing the Client Programs</a></li></div><a name="wp79942"> </a><div class="pSmartList1"><li><a href="JMS5.html#wp81077">Compiling the Clients</a></li></div><a name="wp79943"> </a><div class="pSmartList1"><li><a href="JMS5.html#wp80279">Starting the JMS Provider</a></li></div><a name="wp79944"> </a><div class="pSmartList1"><li><a href="JMS5.html#wp80290">Creating JMS Administered Objects</a></li></div><a name="wp92291"> </a><div class="pSmartList1"><li><a href="JMS5.html#wp92094">Packaging the Clients</a></li></div><a name="wp79945"> </a><div class="pSmartList1"><li><a href="JMS5.html#wp81091">Running the Clients</a></li></div></ul></div><a name="wp79947"> </a><h4 class="pHeading3">Writing the Client Programs</h4><a name="wp79948"> </a><p class="pBody">The sending program, <code class="cCode"><a href="../examples/jms/simple/src/SimpleProducer.java" target="_blank">src/SimpleProducer.java</a></code>, performs the following steps:</p><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp79949"> </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="wp84511"> </a><p class="pBodyRelative"><code class="cCode">/* <br /> * Create a JNDI API InitialContext object if none exists<br /> * yet.<br /> */<br />try {<br /> jndiContext = new InitialContext();<br />} catch (NamingException e) {<br /> System.out.println("Could not create JNDI API " +<br /> "context: " + e.toString());<br /> System.exit(1);<br />}<br /><br />/* <br /> * Look up connection factory and destination. If either<br /> * does not exist, exit. If you look up a <br /> * TopicConnectionFactory instead of a <br /> * QueueConnectionFactory, program behavior is the same.<br /> */<br />try {<br /> connectionFactory = (ConnectionFactory)<br /> jndiContext.lookup("jms/QueueConnectionFactory");<br /> if (destType.equals("queue")) {<br /> dest = (Queue) jndiContext.lookup(destName);<br /> } else if (destType.equals("topic")) {<br /> dest = (Topic) jndiContext.lookup(destName);<br /> } else {<br /> throw new Exception("Invalid destination type" +<br /> "; must be queue or topic");<br /> }<br />} catch (Exception e) {<br /> System.out.println("JNDI API lookup failed: " + <br /> e.toString());<br /> System.exit(1);<br />}</code></p><a name="wp79950"> </a><div class="pSmartList1"><li>Creates a <code class="cCode">Connection</code> and a <code class="cCode">Session</code>:</li></div><a name="wp85062"> </a><p class="pBodyRelative"><code class="cCode">connection = connectionFactory.createConnection();<br />session = connection.createSession(false, <br /> Session.AUTO_ACKNOWLEDGE);</code></p><a name="wp79951"> </a><div class="pSmartList1"><li>Creates a <code class="cCode">MessageProducer</code> and a <code class="cCode">TextMessage</code>:</li></div><a name="wp85071"> </a><p class="pBodyRelative"><code class="cCode">producer = session.createProducer(dest);<br />message = session.createTextMessage();</code></p><a name="wp79988"> </a><div class="pSmartList1"><li>Sends one or more messages to the destination:</li></div><a name="wp85090"> </a><p class="pBodyRelative"><code class="cCode">for (int i = 0; i < NUM_MSGS; i++) {<br /> message.setText("This is message " + (i + 1));<br /> System.out.println("Sending message: " + <br /> message.getText());<br /> producer.send(message);<br />}</code></p><a name="wp79989"> </a><div class="pSmartList1"><li>Sends an empty control message to indicate the end of the message stream:</li></div><a name="wp85202"> </a><p class="pBodyRelative"><code class="cCode">producer.send(session.createMessage());</code></p><a name="wp79990"> </a><div class="pSmartList1"><li>Closes the connection in a <code class="cCode">finally</code> block, automatically closing the session and <code class="cCode">MessageProducer</code>:</li></div><a name="wp85209"> </a><p class="pBodyRelative"><code class="cCode">} finally {<br /> if (connection != null) {<br /> try {<br /> connection.close();<br /> } catch (JMSException e) {}<br /> }<br />}</code></p></ol></div><a name="wp79991"> </a><p class="pBody">The receiving program, <code class="cCode"><a href="../examples/jms/simple/src/SimpleSynchConsumer.java" target="_blank">src/SimpleSynchConsumer.java</a></code>, performs the following steps:</p><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp79992"> </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="wp79993"> </a><div class="pSmartList1"><li>Creates a <code class="cCode">Connection</code> and a <code class="cCode">Session</code></li></div><a name="wp79994"> </a><div class="pSmartList1"><li>Creates a <code class="cCode">MessageConsumer</code>:</li></div><a name="wp85318"> </a><p class="pBodyRelative"><code class="cCode">consumer = session.createConsumer(dest);</code></p><a name="wp79995"> </a><div class="pSmartList1"><li>Starts the connection, causing message delivery to begin:</li></div><a name="wp85323"> </a><p class="pBodyRelative"><code class="cCode">connection.start();</code></p><a name="wp79996"> </a><div class="pSmartList1"><li>Receives the messages sent to the destination until the end-of-message-stream control message is received:</li></div><a name="wp85330"> </a><p class="pBodyRelative"><code class="cCode">while (true) {<br /> Message m = consumer.receive(1);<br /> if (m != null) {<br /> if (m instanceof TextMessage) {<br /> message = (TextMessage) m;<br /> System.out.println("Reading message: " +<br /> message.getText());<br /> } else {<br /> break;<br /> }<br /> }<br />}</code></p><a name="wp79997"> </a><div class="pSmartList1"><li>Closes the connection in a <code class="cCode">finally</code> block, automatically closing the session and <code class="cCode">MessageConsumer</code>.</li></div></ol></div><a name="wp79998"> </a><p class="pBody">The <code class="cCode">receive</code> method can be used in several ways to perform a synchronous receive. If you specify no arguments or an argument of <code class="cCode">0</code>, the method blocks indefinitely until a message arrives:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">Message m = consumer.receive();<a name="wp79999"> </a>Message m = consumer.receive(0);<a name="wp80000"> </a></pre></div><a name="wp80001"> </a><p class="pBody">For a simple client program, this may not matter. But if you do not want your program to consume system resources unnecessarily, use a timed synchronous receive. Do one of the following:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp80002"> </a><div class="pSmartList1"><li>Call the <code class="cCode">receive</code> method with a timeout argument greater than <code class="cCode">0</code>:</li></div><a name="wp80003"> </a><p class="pBodyRelative"><code class="cCode">Message m = consumer.receive(1); // 1 millisecond</code></p><a name="wp80007"> </a><div class="pSmartList1"><li>Call the <code class="cCode">receiveNoWait</code> method, which receives a message only if one is available:</li></div><a name="wp80008"> </a><p class="pBodyRelative"><code class="cCode">Message m = consumer.receiveNoWait();</code></p></ul></div><a name="wp80009"> </a><p class="pBody">The <code class="cCode">SimpleSynchConsumer</code> program uses an indefinite <code class="cCode">while</code> loop to receive messages, calling <code class="cCode">receive</code> with a timeout argument. Calling <code class="cCode">receiveNoWait</code> would have the same effect. </p><a name="wp81077"> </a><h4 class="pHeading3">Compiling the Clients</h4><a name="wp80265"> </a><p class="pBody">You can compile the examples using the <code class="cCode">asant</code> tool.</p>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -