📄 ackequivexample.java
字号:
/* * @(#)AckEquivExample.java 1.6 00/08/18 * * Copyright (c) 2000 Sun Microsystems, Inc. All Rights Reserved. * * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use, * modify and redistribute this software in source and binary code form, * provided that i) this copyright notice and license appear on all copies of * the software; and ii) Licensee does not utilize the software in a manner * which is disparaging to Sun. * * This software is provided "AS IS," without a warranty of any kind. ALL * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE * POSSIBILITY OF SUCH DAMAGES. * * This software is not designed or intended for use in on-line control of * aircraft, air traffic, aircraft navigation or aircraft communications; or in * the design, construction, operation or maintenance of any nuclear * facility. Licensee represents and warrants that it will not use or * redistribute the Software for such purposes. */import javax.jms.*;/** * The AckEquivExample class shows how the following two scenarios both ensure * that a message will not be acknowledged until processing of it is complete: * <ul> * <li> Using an asynchronous receiver (message listener) in an * AUTO_ACKNOWLEDGE session * <li> Using a synchronous receiver in a CLIENT_ACKNOWLEDGE session * </ul> * <p> * With a message listener, the automatic acknowledgment happens when the * onMessage method returns -- that is, after message processing has finished. * <p> * With a synchronous receive, the client acknowledges the message after * processing is complete. (If you use AUTO_ACKNOWLEDGE with a synchronous * receive, the acknowledgement happens immediately after the receive call; if * any subsequent processing steps fail, the message cannot be redelivered.) * <p> * The program contains a SynchSender class, a SynchReceiver class, an * AsynchSubscriber class with a TextListener class, a MultiplePublisher class, * a main method, and a method that runs the other classes' threads. * <p> * Specify a queue name and a topic name on the command line when you run the * program. The program also uses a queue named "controlQueue", which should be * created before you run the program. * * @author Kim Haase * @version 1.6, 08/18/00 */public class AckEquivExample { final String CONTROL_QUEUE = "controlQueue"; String queueName = null; String topicName = null; int exitResult = 0; /** * The SynchSender class creates a session in CLIENT_ACKNOWLEDGE mode and * sends a message. * * @author Kim Haase * @version 1.6, 08/18/00 */ public class SynchSender extends Thread { /** * Runs the thread. */ public void run() { QueueConnectionFactory queueConnectionFactory = null; QueueConnection queueConnection = null; QueueSession queueSession = null; Queue queue = null; QueueSender queueSender = null; final String MSG_TEXT = new String("Here is a client-acknowledge message"); TextMessage message = null; try { queueConnectionFactory = SampleUtilities.getQueueConnectionFactory(); queueConnection = queueConnectionFactory.createQueueConnection(); queueSession = queueConnection.createQueueSession(false, Session.CLIENT_ACKNOWLEDGE); queue = SampleUtilities.getQueue(queueName, queueSession); } catch (Exception e) { System.out.println("Connection problem: " + e.toString()); if (queueConnection != null) { try { queueConnection.close(); } catch (JMSException ee) {} } System.exit(1); } /* * Create client-acknowledge sender. * Create and send message. */ try { System.out.println(" SENDER: Created client-acknowledge session"); queueSender = queueSession.createSender(queue); message = queueSession.createTextMessage(); message.setText(MSG_TEXT); System.out.println(" SENDER: Sending message: " + message.getText()); queueSender.send(message); } catch (JMSException e) { System.out.println("Exception occurred: " + e.toString()); exitResult = 1; } finally { if (queueConnection != null) { try { queueConnection.close(); } catch (JMSException e) { exitResult = 1; } } } } } /** * The SynchReceiver class creates a session in CLIENT_ACKNOWLEDGE mode and * receives the message sent by the SynchSender class. * * @author Kim Haase * @version 1.6, 08/18/00 */ public class SynchReceiver extends Thread { /** * Runs the thread. */ public void run() { QueueConnectionFactory queueConnectionFactory = null; QueueConnection queueConnection = null; QueueSession queueSession = null; Queue queue = null; QueueReceiver queueReceiver = null; TextMessage message = null; try { queueConnectionFactory = SampleUtilities.getQueueConnectionFactory(); queueConnection = queueConnectionFactory.createQueueConnection(); queueSession = queueConnection.createQueueSession(false, Session.CLIENT_ACKNOWLEDGE); queue = SampleUtilities.getQueue(queueName, queueSession); } catch (Exception e) { System.out.println("Connection problem: " + e.toString()); if (queueConnection != null) { try { queueConnection.close(); } catch (JMSException ee) {} } System.exit(1); } /* * Create client-acknowledge receiver. * Receive message and process it. * Acknowledge message. */ try { System.out.println(" RECEIVER: Created client-acknowledge session"); queueReceiver = queueSession.createReceiver(queue); queueConnection.start(); message = (TextMessage) queueReceiver.receive(); System.out.println(" RECEIVER: Processing message: " + message.getText()); System.out.println(" RECEIVER: Now I'll acknowledge the message"); message.acknowledge(); } catch (JMSException e) { System.out.println("Exception occurred: " + e.toString()); exitResult = 1; } finally { if (queueConnection != null) { try { queueConnection.close(); } catch (JMSException e) { exitResult = 1; } } } } } /** * The AsynchSubscriber class creates a session in AUTO_ACKNOWLEDGE mode * and fetches several messages from a topic asynchronously, using a * message listener, TextListener. * <p> * Each message is acknowledged after the onMessage method completes. * * @author Kim Haase * @version 1.6, 08/18/00 */ public class AsynchSubscriber extends Thread { /** * The TextListener class implements the MessageListener interface by * defining an onMessage method for the AsynchSubscriber class. * * @author Kim Haase * @version 1.6, 08/18/00 */ private class TextListener implements MessageListener { final SampleUtilities.DoneLatch monitor = new SampleUtilities.DoneLatch(); /** * Casts the message to a TextMessage and displays its text. * A non-text message is interpreted as the end of the message * stream, and the message listener sets its monitor state to all * done processing messages. * * @param message the incoming message */ public void onMessage(Message message) { if (message instanceof TextMessage) { TextMessage msg = (TextMessage) message; try {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -