📄 jms4.html
字号:
</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">MessageProducer producer = session.createProducer(myQueue);<a name="wp79088"> </a>MessageProducer producer = session.createProducer(myTopic);<a name="wp79089"> </a></pre></div><a name="wp79090"> </a><p class="pBody">You can create an unidentified producer by specifying <code class="cCode">null</code> as the argument to <code class="cCode">createProducer</code>. With an unidentified producer, you can wait to specify which destination to send the message to until you send a message.</p><a name="wp79091"> </a><p class="pBody">Once you have created a message producer, you can use it to send messages, using the <code class="cCode">send</code> method: </p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">producer.send(message);<a name="wp79092"> </a></pre></div><a name="wp79093"> </a><p class="pBody">You have to create the messages first; see <a href="JMS4.html#wp79302">Messages</a>.</p><a name="wp79100"> </a><p class="pBody">If you created an unidentified producer, use an overloaded <code class="cCode">send</code> method that specifies the destination as the first parameter. For example:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">MessageProducer anon_prod = session.createProducer(null);<a name="wp79101"> </a>anon_prod.send(myQueue, message);<a name="wp79102"> </a></pre></div><a name="wp79145"> </a><h3 class="pHeading2">Message Consumers</h3><a name="wp79146"> </a><p class="pBody">A <em class="cEmphasis">message consumer</em> is an object created by a session and is used for receiving messages sent to a destination. A message consumer allows a JMS client to register interest in a destination with a JMS provider. The JMS provider manages the delivery of messages from a destination to the registered consumers of the destination.</p><a name="wp79147"> </a><p class="pBody">A message consumer implements the <code class="cCode">MessageConsumer</code> interface. </p><a name="wp79148"> </a><p class="pBody">For example, you use a <code class="cCode">Session</code> to create a <code class="cCode">MessageConsumer</code> for either a queue or a topic:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">MessageConsumer consumer = session.createConsumer(myQueue);<a name="wp79149"> </a>MessageConsumer consumer = session.createConsumer(myTopic);<a name="wp79150"> </a></pre></div><a name="wp79151"> </a><p class="pBody">You use the <code class="cCode">Session.createDurableSubscriber</code> method to create a durable topic subscriber. This method is valid only if you are using a topic. For details, see <a href="JMS6.html#wp81941">Creating Durable Subscriptions</a>.</p><a name="wp79158"> </a><p class="pBody">Once you have created a message consumer, it becomes active, and you can use it to receive messages. You can use the <code class="cCode">close</code> method for a <code class="cCode">MessageConsumer</code> to make the message consumer inactive. Message delivery does not begin until you start the connection you created by calling the <code class="cCode">start</code> method. (Remember always to call the <code class="cCode">start</code> method; forgetting to start the connection is one of the most common JMS programming errors.)</p><a name="wp79159"> </a><p class="pBody">You use the <code class="cCode">receive</code> method to consume a message synchronously. You can use this method at any time after you call the <code class="cCode">start</code> method:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">connection.start();Message m = consumer.receive();<a name="wp79160"> </a>connection.start();Message m = consumer.receive(1000); // time out after a second<a name="wp79162"> </a></pre></div><a name="wp79164"> </a><p class="pBody">To consume a message asynchronously, you use a message listener, described in <a href="JMS4.html#wp79175">Message Listeners</a>.</p><a name="wp79175"> </a><h4 class="pHeading3">Message Listeners</h4><a name="wp79176"> </a><p class="pBody">A <em class="cEmphasis">message listener</em> is an object that acts as an asynchronous event handler for messages. This object implements the <code class="cCode">MessageListener</code> interface, which contains one method, <code class="cCode">onMessage</code>. In the <code class="cCode">onMessage</code> method, you define the actions to be taken when a message arrives.</p><a name="wp79177"> </a><p class="pBody">You register the message listener with a specific <code class="cCode">MessageConsumer</code> by using the <code class="cCode">setMessageListener</code> method. For example, if you define a class named <code class="cCode">Listener</code> that implements the <code class="cCode">MessageListener</code> interface, you can register the message listener as follows:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">Listener myListener = new Listener();consumer.setMessageListener(myListener);<a name="wp79178"> </a></pre></div><a name="wp79180"> </a><p class="pBody">After you register the message listener, you call the <code class="cCode">start</code> method on the <code class="cCode">Connection</code> to begin message delivery. (If you call <code class="cCode">start</code> before you register the message listener, you are likely to miss messages.)</p><a name="wp79181"> </a><p class="pBody">Once message delivery begins, the message consumer automatically calls the message listener's <code class="cCode">onMessage</code> method whenever a message is delivered. The <code class="cCode">onMessage</code> method takes one argument of type <code class="cCode">Message</code>, which your implementation of the method can cast to any of the other message types (see <a href="JMS4.html#wp79620">Message Bodies</a>).</p><a name="wp79188"> </a><p class="pBody">A message listener is not specific to a particular destination type. The same listener can obtain messages from either a queue or a topic, depending on the type of destination for which the message consumer was created. A message listener does, however, usually expect a specific message type and format. Moreover, if it needs to reply to messages, a message listener must either assume a particular destination type or obtain the destination type of the message and create a producer for that destination type.</p><a name="wp79189"> </a><p class="pBody">Your <code class="cCode">onMessage</code> method should handle all exceptions. It must not throw checked exceptions, and throwing a <code class="cCode">RuntimeException</code> is considered a programming error.</p><a name="wp79190"> </a><p class="pBody">The session used to create the message consumer serializes the execution of all message listeners registered with the session. At any time, only one of the session's message listeners is running. </p><a name="wp79191"> </a><p class="pBody">In the J2EE platform, a message-driven bean is a special kind of message listener. For details, see <a href="JMS7.html#wp82163">Using Message-Driven Beans</a>.</p><a name="wp79281"> </a><h4 class="pHeading3">Message Selectors</h4><a name="wp79282"> </a><p class="pBody">If your messaging application needs to filter the messages it receives, you can use a JMS API message selector, which allows a message consumer to specify the messages it is interested in. Message selectors assign the work of filtering messages to the JMS provider rather than to the application. For an example of an application that uses a message selector, see <a href="JMSJ2EEex2.html#wp95588">A J2EE Application that Uses the JMS API with a Session Bean</a>.</p><a name="wp79286"> </a><p class="pBody">A message selector is a <code class="cCode">String</code> that contains an expression. The syntax of the expression is based on a subset of the SQL92 conditional expression syntax. The message selector in the example selects any message with a <code class="cCode">NewsType</code> property that is set to the value <code class="cCode">'Sports'</code> or <code class="cCode">'Opinion'</code>:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">NewsType = 'Sports' OR NewsType = 'Opinion'<a name="wp84407"> </a></pre></div><a name="wp84406"> </a><p class="pBody">The <code class="cCode">createConsumer</code> and <code class="cCode">createDurableSubscriber</code> methods allow you to specify a message selector as an argument when you create a message consumer.</p><a name="wp79287"> </a><p class="pBody">The message consumer then receives only messages whose headers and properties match the selector. (See <a href="JMS4.html#wp79367">Message Headers</a>, and <a href="JMS4.html#wp79540">Message Properties</a>). A message selector cannot select messages on the basis of the content of the message body.</p><a name="wp79302"> </a><h3 class="pHeading2">Messages</h3><a name="wp79303"> </a><p class="pBody">The ultimate purpose of a JMS application is to produce and to consume messages that can then be used by other software applications. JMS messages have a basic format that is simple but highly flexible, allowing you to create messages that match formats used by non-JMS applications on heterogeneous platforms.</p><a name="wp79304"> </a><p class="pBody">A JMS message has three parts: a header, properties, and a body. Only the header is required. The following sections describe these parts:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp79305"> </a><div class="pSmartList1"><li><a href="JMS4.html#wp79367">Message Headers</a></li></div><a name="wp79306"> </a><div class="pSmartList1"><li><a href="JMS4.html#wp79540">Message Properties</a> (optional)</li></div><a name="wp79307"> </a><div class="pSmartList1"><li><a href="JMS4.html#wp79620">Message Bodies</a> (optional)</li></div></ul></div><a name="wp79308"> </a><p class="pBody">For complete documentation of message headers, properties, and bodies, see the documentation of the <code class="cCode">Message</code> interface in the API documentation.</p><a name="wp79367"> </a><h4 class="pHeading3">Message Headers</h4><a name="wp79368"> </a><p class="pBody">A JMS message header contains a number of predefined fields that contain values that both clients and providers use to identify and to route messages. <a href="JMS4.html#wp79545">Table 29-1</a> lists the JMS message header fields and indicates how their values are set. For example, every message has a unique identifier, represented in the header field <code class="cCode">JMSMessageID</code>. The value of another header field, <code class="cCode">JMSDestination</code>, represents the queue or the topic to which the message is sent. Other fields include a timestamp and a priority level. </p><a name="wp79611"> </a><p class="pBody">Each header field has associated setter and getter methods, which are documented in the description of the <code class="cCode">Message</code> interface. Some header fields are intended to be set by a client, but many are set automatically by the <code class="cCode">send</code> or the <code class="cCode">publish</code> method, which overrides any client-set values.</p><div align="left"><table border="1" summary="How JMS Message Header Field Values Are Set" id="wp79545"> <caption><a name="wp79545"> </a><div class="pTableTitle">Table 29-1 How JMS Message Header Field Values Are Set</div></caption> <tr align="center"> <th><a name="wp79549"> </a><div class="pCellHeading">Header Field</div></th> <th><a name="wp79551"> </a><div class="pCellHeading">Set By</div></th></tr> <tr align="left"> <td><a name="wp79555"> </a><div class="pCellBody"><code class="cCode">JMSDestination</code></div></td> <td><a name="wp79557"> </a><div class="pCellBody"><code class="cCode">send</code> or <code class="cCode">publish</code> method</div></td></tr> <tr align="left"> <td><a name="wp79561"> </a><div class="pCellBody"><code class="cCode">JMSDeliveryMode</code></div></td> <td><a name="wp79563"> </a><div class="pCellBody"><code class="cCode">send</code> or <code class="cCode">publish</code> method</div></td></tr> <tr align="left"> <td><a name="wp79567"> </a><div class="pCellBody"><code class="cCode">JMSExpiration</code></div></td> <td><a name="wp79569"> </a><div class="pCellBody"><code class="cCode">send</code> or <code class="cCode">publish</code> method</div></td></tr> <tr align="left"> <td><a name="wp79573"> </a><div class="pCellBody"><code class="cCode">JMSPriority</code></div></td> <td><a name="wp79575"> </a><div class="pCellBody"><code class="cCode">send</code> or <code class="cCode">publish</code> method</div></td></tr> <tr align="left"> <td><a name="wp79579"> </a><div class="pCellBody"><code class="cCode">JMSMessageID</code></div></td> <td><a name="wp79581"> </a><div class="pCellBody"><code class="cCode">send</code> or <code class="cCode">publish</code> method</div></td></tr> <tr align="left"> <td><a name="wp79585"> </a><div class="pCellBody"><code class="cCode">JMSTimestamp</code></div></td> <td><a name="wp79587"> </a><div class="pCellBody"><code class="cCode">send</code> or <code class="cCode">publish</code> method</div></td></tr> <tr align="left"> <td><a name="wp79591"> </a><div class="pCellBody"><code class="cCode">JMSCorrelationID</code></div></td> <td><a name="wp79593"> </a><div class="pCellBody">
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -