📄 jms4.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>The JMS API Programming Model</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="JMS3.html" /> <link rel="Next" href="JMS5.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="JMS3.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="JMS5.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="wp78839"> </a><h2 class="pHeading1">The JMS API Programming Model</h2><a name="wp78842"> </a><p class="pBody">The basic building blocks of a JMS application consist of</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp78843"> </a><div class="pSmartList1"><li><a href="JMS4.html#wp78884">Administered Objects</a>: connection factories and destinations</li></div><a name="wp78844"> </a><div class="pSmartList1"><li><a href="JMS4.html#wp78986">Connections</a></li></div><a name="wp78845"> </a><div class="pSmartList1"><li><a href="JMS4.html#wp79001">Sessions</a></li></div><a name="wp78846"> </a><div class="pSmartList1"><li><a href="JMS4.html#wp79085">Message Producers</a></li></div><a name="wp78847"> </a><div class="pSmartList1"><li><a href="JMS4.html#wp79145">Message Consumers</a></li></div><a name="wp78848"> </a><div class="pSmartList1"><li><a href="JMS4.html#wp79302">Messages</a></li></div></ul></div><a name="wp78852"> </a><p class="pBody"><a href="JMS4.html#wp78861">Figure 29-5</a> shows how all these objects fit together in a JMS client application.</p><a name="wp84400"> </a><p class="pBody"></p><div align="left"><img src="images/JMS055.gif" height="251" width="338" alt="The JMS API Programming Model" border="0" hspace="0" vspace="0"/></div><p class="pBody"></p><p> <a name="78861"> </a><strong><font >Figure 29-5 The JMS API Programming Model</font></strong></p><a name="wp78862"> </a><p class="pBody">This section describes all these objects briefly and provides sample commands and code snippets that show how to create and use the objects. The last section briefly describes JMS API exception handling. </p><a name="wp78863"> </a><p class="pBody">Examples that show how to combine all these objects in applications appear in later sections. For more details, see the JMS API documentation, which is part of the J2EE API documentation.</p><a name="wp78884"> </a><h3 class="pHeading2">Administered Objects</h3><a name="wp78885"> </a><p class="pBody">Two parts of a JMS application--destinations and connection factories--are best maintained administratively rather than programmatically. The technology underlying these objects is likely to be very different from one implementation of the JMS API to another. Therefore, the management of these objects belongs with other administrative tasks that vary from provider to provider.</p><a name="wp78886"> </a><p class="pBody">JMS clients access these objects through interfaces that are portable, so a client application can run with little or no change on more than one implementation of the JMS API. Ordinarily, an administrator configures administered objects in a JNDI namespace, and JMS clients then look them up, using the JNDI API. J2EE applications always use the JNDI API.</p><a name="wp78887"> </a><p class="pBody">With the J2EE Application Server, you use the Admin Console to create JMS administered objects in the form of resources. You can also use the <code class="cCode">asadmin</code> command.</p><a name="wp78893"> </a><h4 class="pHeading3">Connection Factories</h4><a name="wp78894"> </a><p class="pBody">A <em class="cEmphasis">connection factory</em> is the object a client uses to create a connection with a provider. A connection factory encapsulates a set of connection configuration parameters that has been defined by an administrator. Each connection factory is an instance of either the <code class="cCode">QueueConnectionFactory</code> or the <code class="cCode">TopicConnectionFactory</code> interface.</p><a name="wp78895"> </a><p class="pBody">To learn how to use the Admin Console to create connection factories, see <a href="JMS5.html#wp80290">Creating JMS Administered Objects</a>.</p><a name="wp96876"> </a><p class="pBody">At the beginning of a JMS client program, you usually perform a JNDI lookup of the connection factory. The connection factory itself is specific to one domain or the other. However, you normally assign it to a <code class="cCode">ConnectionFactory</code> object.</p><a name="wp78899"> </a><p class="pBody">Calling the <code class="cCode">InitialContext</code> method with no parameters results in a search of the current classpath for a vendor-specific file named <code class="cCode">jndi.properties</code>. This file indicates which JNDI implementation to use and which namespace to use.</p><a name="wp78900"> </a><p class="pBody">For example, the following code fragment obtains an <code class="cCode">InitialContext</code> object and uses it to look up the <code class="cCode">QueueConnectionFactory</code> and the <code class="cCode">TopicConnectionFactory</code> by name, then assigns each to a <code class="cCode">ConnectionFactory</code> object:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">Context ctx = new InitialContext();<a name="wp78901"> </a>ConnectionFactory connectionFactory1 = (ConnectionFactory) ctx.lookup("QueueConnectionFactory");<a name="wp78903"> </a>ConnectionFactory connectionFactory2 = (ConnectionFactory) ctx.lookup("TopicConnectionFactory");<a name="wp78906"> </a></pre></div><a name="wp78920"> </a><h4 class="pHeading3">Destinations</h4><a name="wp78921"> </a><p class="pBody">A <em class="cEmphasis">destination</em> is the object a client uses to specify the target of messages it produces and the source of messages it consumes. In the PTP messaging domain, destinations are called queues. In the pub/sub messaging domain, destinations are called topics.</p><a name="wp91764"> </a><p class="pBody">Creating destinations using the J2EE Application Server is a two-step process. You create a JMS destination resource that specifies the JNDI name of the destination. You also create a physical destination to which the JNDI name refers.</p><a name="wp96894"> </a><p class="pBody">To learn how to use the Admin Console to create physical destinations and destination resources, see <a href="JMS5.html#wp80290">Creating JMS Administered Objects</a>.</p><a name="wp78925"> </a><p class="pBody">A JMS application may use multiple queues and/or topics.</p><a name="wp78926"> </a><p class="pBody">In addition to looking up a connection factory in a client program, you usually look up a destination. Like connection factories, destinations are specific to one domain or the other. You normally assign the destination to a <code class="cCode">Destination</code> object. In order to preserve the semantics of queues and topics, however, you cast the object to a destination of the appropriate type.</p><a name="wp78927"> </a><p class="pBody">For example, the following line of code performs a JNDI lookup of the previously created topic <code class="cCode">MyTopic</code> and assigns it to a <code class="cCode">Destination</code> object, after casting it to a <code class="cCode">Topic</code> object:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">Destination myDest = (Topic) ctx.lookup("MyTopic");<a name="wp78928"> </a></pre></div><a name="wp78929"> </a><p class="pBody">The following line of code looks up a queue named <code class="cCode">MyQueue</code> and assigns it to a <code class="cCode">Destination</code> object, after casting it to a <code class="cCode">Queue</code> object:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">Destination myDest = (Queue) ctx.lookup("MyQueue");<a name="wp78930"> </a></pre></div><a name="wp78931"> </a><p class="pBody">With the common interfaces, you can mix or match connection factories and destinations. That is, you can look up a <code class="cCode">QueueConnectionFactory</code> and use it with a <code class="cCode">Topic</code>, and you can look up a <code class="cCode">TopicConnectionFactory</code> and use it with a <code class="cCode">Queue</code>. The behavior of the application will depend on the kind of destination you use, not on the kind of connection factory you use.</p><a name="wp78986"> </a><h3 class="pHeading2">Connections</h3><a name="wp78987"> </a><p class="pBody">A <em class="cEmphasis">connection</em> encapsulates a virtual connection with a JMS provider. A connection could represent an open TCP/IP socket between a client and a provider service daemon. You use a connection to create one or more sessions.</p><a name="wp78988"> </a><p class="pBody">Connections implement the <code class="cCode">Connection</code> interface. Once you have a <code class="cCode">ConnectionFactory</code> object, you can use it to create a <code class="cCode">Connection</code>:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">Connection connection = connectionFactory.createConnection();<a name="wp78989"> </a></pre></div><a name="wp78990"> </a><p class="pBody">When an application completes, you need to close any connections that you have created. Failure to close a connection can cause resources not to be released by the JMS provider. Closing a connection also closes its sessions and their message producers and message consumers.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">connection.close();<a name="wp78991"> </a></pre></div><a name="wp78992"> </a><p class="pBody">Before your application can consume messages, you must call the connection's <code class="cCode">start</code> method; for details, see <a href="JMS4.html#wp79145">Message Consumers</a>. If you want to stop message delivery temporarily without closing the connection, you call the <code class="cCode">stop</code> method.</p><a name="wp79001"> </a><h3 class="pHeading2">Sessions</h3><a name="wp79002"> </a><p class="pBody">A <em class="cEmphasis">session</em> is a single-threaded context for producing and consuming messages. You use sessions to create message producers, message consumers, and messages. Sessions serialize the execution of message listeners; for details, see <a href="JMS4.html#wp79175">Message Listeners</a>.</p><a name="wp79009"> </a><p class="pBody">A session provides a transactional context with which to group a set of sends and receives into an atomic unit of work. For details, see <a href="JMS6.html#wp92878">Using JMS API Local Transactions</a>.</p><a name="wp79016"> </a><p class="pBody">Sessions implement the <code class="cCode">Session</code> interface. After you create a <code class="cCode">Connection</code> object, you use it to create a <code class="cCode">Session</code>:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);<a name="wp79017"> </a></pre></div><a name="wp79072"> </a><p class="pBody">The first argument means that the session is not transacted; the second means that the session automatically acknowledges messages when they have been received successfully. (For more information, see <a href="JMS6.html#wp81785">Controlling Message Acknowledgment</a>.)</p><a name="wp79079"> </a><p class="pBody">To create a transacted session, use the following code:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">Session session = connection.createSession(true, 0);<a name="wp79080"> </a></pre></div><a name="wp79081"> </a><p class="pBody">Here, the first argument means that the session is transacted; the second indicates that message acknowledgment is not specified for transacted sessions. For more information on transactions, see <a href="JMS6.html#wp92878">Using JMS API Local Transactions</a>. For the way JMS transactions work in J2EE applications, see <a href="JMS7.html#wp82114">Using the JMS API in a J2EE Application</a>.</p><a name="wp79085"> </a><h3 class="pHeading2">Message Producers</h3><a name="wp79086"> </a><p class="pBody">A <em class="cEmphasis">message producer</em> is an object created by a session and is used for sending messages to a destination. It implements the <code class="cCode">MessageProducer</code> interface.</p><a name="wp79087"> </a><p class="pBody">You use a <code class="cCode">Session</code> to create a <code class="cCode">MessageProducer</code> for a destination. Here, the first example creates a producer for the destination <code class="cCode">myQueue</code>, the second for the destination <code class="cCode">myTopic</code>:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -