📄 jms6.html
字号:
Creating Temporary Destinations</h4><a name="wp81885"> </a><p class="pBody">Normally, you create JMS destinations--queues and topics--administratively rather than programmatically. Your JMS provider includes a tool that you use to create and to remove destinations, and it is common for destinations to be long lasting.</p><a name="wp81886"> </a><p class="pBody">The JMS API also enables you to create destinations--<code class="cCode">TemporaryQueue</code> and <code class="cCode">TemporaryTopic</code> objects--that last only for the duration of the connection in which they are created. You create these destinations dynamically, using the <code class="cCode">Session.createTemporaryQueue</code> and the <code class="cCode">Session.createTemporaryTopic</code> methods.</p><a name="wp81887"> </a><p class="pBody">The only message consumers that can consume from a temporary destination are those created by the same connection that created the destination. Any message producer can send to the temporary destination. If you close the connection that a temporary destination belongs to, the destination is closed and its contents lost.</p><a name="wp81889"> </a><p class="pBody">You can use temporary destinations to implement a simple request/reply mechanism. If you create a temporary destination and specify it as the value of the <code class="cCode">JMSReplyTo</code> message header field when you send a message, the consumer of the message can use the value of the <code class="cCode">JMSReplyTo</code> field as the destination to which it sends a reply. The consumer can also reference the original request by setting the <code class="cCode">JMSCorrelationID</code> header field of the reply message to the value of the <code class="cCode">JMSMessageID</code> header field of the request. For example, an <code class="cCode">onMessage</code> method may create a session so that it can send a reply to the message it receives. It can use code like the following:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">producer = session.createProducer(msg.getJMSReplyTo());replyMsg = session.createTextMessage("Consumer " + "processed message: " + msg.getText());replyMsg.setJMSCorrelationID(msg.getJMSMessageID());producer.send(replyMsg);<a name="wp81890"> </a></pre></div><a name="wp81895"> </a><p class="pBody">For more examples, see Chapter <a href="JMSJ2EEex.html#wp78297">30</a>.</p><a name="wp81932"> </a><h3 class="pHeading2">Using Advanced Reliability Mechanisms</h3><a name="wp81933"> </a><p class="pBody">The more advanced mechanisms for achieving reliable message delivery are the following:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp81934"> </a><div class="pSmartList1"><li><b class="cBold">Creating durable subscriptions.</b> You can create durable topic subscriptions, which receive messages published while the subscriber is not active. Durable subscriptions offer the reliability of queues to the publish/subscribe message domain.</li></div><a name="wp81935"> </a><div class="pSmartList1"><li><b class="cBold">Using local transactions.</b> You can use local transactions, which allow you to group a series of sends and receives into an atomic unit of work. Transactions are rolled back if they fail at any time.</li></div></ul></div><a name="wp81941"> </a><h4 class="pHeading3">Creating Durable Subscriptions</h4><a name="wp81942"> </a><p class="pBody">To ensure that a pub/sub application receives all published messages, use <code class="cCode">PERSISTENT</code> delivery mode for the publishers. In addition, use durable subscriptions for the subscribers.</p><a name="wp81943"> </a><p class="pBody">The <code class="cCode">Session.createConsumer</code> method creates a nondurable subscriber if a topic is specified as the destination. A nondurable subscriber can receive only messages that are published while it is active. </p><a name="wp81944"> </a><p class="pBody">At the cost of higher overhead, you can use the <code class="cCode">Session.createDurableSubscriber</code> method to create a durable subscriber. A durable subscription can have only one active subscriber at a time.</p><a name="wp81945"> </a><p class="pBody">A durable subscriber registers a durable subscription with a unique identity that is retained by the JMS provider. Subsequent subscriber objects with the same identity resume the subscription in the state in which it was left by the previous subscriber. If a durable subscription has no active subscriber, the JMS provider retains the subscription's messages until they are received by the subscription or until they expire.</p><a name="wp81948"> </a><p class="pBody">You establish the unique identity of a durable subscriber by setting the following:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp81949"> </a><div class="pSmartList1"><li>A client ID for the connection</li></div><a name="wp81950"> </a><div class="pSmartList1"><li>A topic and a subscription name for the subscriber</li></div></ul></div><a name="wp81951"> </a><p class="pBody">You set the client ID administratively for a client-specific connection factory using the Admin Console. </p><a name="wp97217"> </a><p class="pBody">After using this connection factory to create the connection and the session, you call the <code class="cCode">createDurableSubscriber</code> method with two arguments--the topic and a string that specifies the name of the subscription:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">String subName = "MySub";MessageConsumer topicSubscriber = session.createDurableSubscriber(myTopic, subName);<a name="wp81955"> </a></pre></div><a name="wp81958"> </a><p class="pBody">The subscriber becomes active after you start the <code class="cCode">Connection</code> or <code class="cCode">TopicConnection</code>. Later on, you might close the subscriber:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">topicSubscriber.close();<a name="wp81959"> </a></pre></div><a name="wp81960"> </a><p class="pBody">The JMS provider stores the messages sent or published to the topic, as it would store messages sent to a queue. If the program or another application calls <code class="cCode">createDurableSubscriber</code> with the same connection factory and its client ID, the same topic, and the same subscription name, the subscription is reactivated, and the JMS provider delivers the messages that were published while the subscriber was inactive. </p><a name="wp81961"> </a><p class="pBody">To delete a durable subscription, first close the subscriber, and then use the <code class="cCode">unsubscribe</code> method, with the subscription name as the argument:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">topicSubscriber.close();session.unsubscribe("MySub");<a name="wp81962"> </a></pre></div><a name="wp81964"> </a><p class="pBody">The <code class="cCode">unsubscribe</code> method deletes the state that the provider maintains for the subscriber.</p><a name="wp81965"> </a><p class="pBody"><a href="JMS6.html#wp81986">Figure 29-6</a> and <a href="JMS6.html#wp81996">Figure 29-7</a> show the difference between a nondurable and a durable subscriber. With an ordinary, nondurable, subscriber, the subscriber and the subscription begin and end at the same point and are, in effect, identical. When a subscriber is closed, the subscription ends as well. Here, <code class="cCode">create</code> stands for a call to <code class="cCode">Session.createConsumer</code> with a <code class="cCode">Topic</code> argument, and <code class="cCode">close</code> stands for a call to <code class="cCode">MessageConsumer.close</code>. Any messages published to the topic between the time of the first <code class="cCode">close</code> and the time of the second <code class="cCode">create</code> are not consumed by the subscriber. In <a href="JMS6.html#wp81986">Figure 29-6</a>, the subscriber consumes messages M1, M2, M5, and M6, but messages M3 and M4 are lost.</p><a name="wp81984"> </a><p class="pBody"></p><div align="left"><img src="images/JMS066.gif" height="99" width="342" alt="Nondurable Subscribers and Subscriptions" border="0" hspace="0" vspace="0"/></div><p class="pBody"></p><p> <a name="81986"> </a><strong><font >Figure 29-6 Nondurable Subscribers and Subscriptions</font></strong></p><a name="wp81987"> </a><p class="pBody">With a durable subscriber, the subscriber can be closed and recreated, but the subscription continues to exist and to hold messages until the application calls the <code class="cCode">unsubscribe</code> method. In <a href="JMS6.html#wp81996">Figure 29-7</a>, <code class="cCode">create</code> stands for a call to <code class="cCode">Session.createDurableSubscriber</code>, <code class="cCode">close</code> stands for a call to <code class="cCode">MessageConsumer.close</code>, and <code class="cCode">unsubscribe</code> stands for a call to <code class="cCode">Session.unsubscribe</code>. Messages published while the subscriber is closed are received when the subscriber is created again. So even though messages M2, M4, and M5 arrive while the subscriber is closed, they are not lost.</p><a name="wp81994"> </a><p class="pBody"></p><div align="left"><img src="images/JMS077.gif" height="135" width="447" alt="A Durable Subscriber and Subscription" border="0" hspace="0" vspace="0"/></div><p class="pBody"> </p><p> <a name="81996"> </a><strong><font >Figure 29-7 A Durable Subscriber and Subscription</font></strong></p><a name="wp82000"> </a><p class="pBody">See <a href="JMSJ2EEex2.html#wp95588">A J2EE Application that Uses the JMS API with a Session Bean</a> for an example of a J2EE application that uses durable subscriptions. See <a href="JMS6.html#wp83602">A Message Acknowledgment Example</a> and the next section for examples of client applications that use durable subscriptions.</p><a name="wp83706"> </a><h5 class="pHeading4">A Durable Subscription Example</h5><a name="wp83713"> </a><p class="pBody">The <code class="cCode"><a href="../examples/jms/advanced/src/DurableSubscriberExample.java" target="_blank">DurableSubscriberExample.java</a></code> program in the directory <code class="cCode"><</code><code class="cVariable">INSTALL</code><code class="cCode">>/j2eetutorial14/examples/jms/advanced/src/</code> shows how durable subscriptions work. It demonstrates that a durable subscription is active even when the subscriber is not active. The program contains a <code class="cCode">DurableSubscriber</code> class, a <code class="cCode">MultiplePublisher</code> class, a main method, and a method that instantiates the classes and calls their methods in sequence.</p><a name="wp83714"> </a><p class="pBody">The program begins like any publish/subscribe program: The subscriber starts, the publisher publishes some messages, and the subscriber receives them. At this point, the subscriber closes itself. The publisher then publishes some messages while the subscriber is not active. The subscriber then restarts and receives the messages.</p><a name="wp83715"> </a><p class="pBody">Before you run this program, compile the source file and create a connection factory with a client ID. If you did not already do so in <a href="JMS6.html#wp83602">A Message Acknowledgment Example</a>, perform the following steps:</p><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp97230"> </a><div class="pSmartList1"><li>Compile the source code as follows:</li></div><a name="wp87023"> </a><p class="pBodyRelative"><code class="cCode">asant build</code></p><a name="wp97227"> </a><div class="pSmartList1"><li>Create a connection factory with the name <code class="cCode">jms/DurableTopicConnectionFactory</code> and type <code class="cCode">javax.jms.TopicConnectionFactory</code>. Add the property <code class="cCode">ClientId</code> with the value <code class="cCode">MyID</code>.</li></div></ol></div><a name="wp97225"> </a><p class="pBody">To package the program, perform the following steps:</p><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp92811"> </a><div class="pSmartList1"><li>If you did not do so before, start <code class="cCode">deploytool</code>.</li></div><a name="wp92812"> </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="wp92813"> </a><div class="pSmartList1"><li>Select the radio button labeled Create New Stand-Alone AppClient Module.</li></div><a name="wp92814"> </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/advanced/</code> directory.</li></div><a name="wp92815"> </a><div class="pSmartList1"><li>Type <code class="cCode">DurableSubscriberExample</code> in the File Name field and click Choose Module File.</li></div><a name="wp92816"> </a><div class="pSmartList1"><li>Verify that <code class="cCode">DurableSubscriberExample</code> appears in the AppClient Name field.</li></div><a name="wp92817"> </a><div class="pSmartList1"><li>Click the Edit button next to the Contents text area. </li></div><a name="wp92818"> </a><div class="pSmartList1"><li>In the dialog box, locate the <code class="cCode">build</code> directory. Select the five classes whose names begin with <code class="cCode">DurableSubscriberExample</code> from the Available Files tree and click Add.</li></div><a name="wp92819"> </a><div class="pSmartList1"><li>Select the two classes whose names begin with <code class="cCode">SampleUtilities</code> from the Available Files tree and click Add, then OK.</li></div><a name="wp92820"> </a><div class="pSmartList1"><li>Click Next.</li></div><a name="wp96829"> </a><div class="pSmartList1"><li>In the General screen, select <code class="cCode">DurableSubscriberExample</code> in the Main Class combo box.</li></div><a name="wp92821"> </a><div class="pSmartList1"><li>Click Next.</li></div>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -