📄 ackequivexample.java
字号:
*/ public class AsynchSubscriber extends Thread { /** * Runs the thread. */ public void run() { ConnectionFactory connectionFactory = null; Connection connection = null; Session session = null; Topic topic = null; MessageConsumer subscriber = null; TextListener listener = null; /* * Look up connection factory and topic. If either * does not exist, exit. */ try { connectionFactory = SampleUtilities.getConnectionFactory(conFacName); connection = connectionFactory.createConnection(); session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); System.out.println("SUBSCRIBER: Created " + "auto-acknowledge session"); topic = SampleUtilities.getTopic(topicName, session); } catch (Exception e) { System.err.println("Connection problem: " + e.toString()); if (connection != null) { try { connection.close(); } catch (JMSException ee) { } } System.exit(1); } /* * Create auto-acknowledge subscriber. * Register message listener (TextListener). * Start message delivery. * Send synchronize message to publisher, then wait * till all messages have arrived. * Listener displays the messages obtained. */ try { subscriber = session.createDurableSubscriber(topic, "AckSub"); listener = new TextListener(); subscriber.setMessageListener(listener); connection.start(); // Let publisher know that subscriber is ready. try { SampleUtilities.sendSynchronizeMessage("SUBSCRIBER: ", CONTROL_QUEUE); } catch (Exception e) { System.err.println("Queue probably missing: " + e.toString()); if (connection != null) { try { connection.close(); } catch (JMSException ee) { } } System.exit(1); } /* * Asynchronously process messages. * Block until publisher issues a control message * indicating end of publish stream. */ listener.monitor.waitTillDone(); subscriber.close(); session.unsubscribe("AckSub"); } catch (JMSException e) { System.err.println("Exception occurred: " + e.toString()); } finally { if (connection != null) { try { connection.close(); } catch (JMSException e) { } } } } /** * The TextListener class implements the MessageListener * interface by defining an onMessage method for the * AsynchSubscriber class. */ 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 { System.out.println("SUBSCRIBER: " + "Processing message: " + msg.getText()); } catch (JMSException e) { System.err.println("Exception in " + "onMessage(): " + e.toString()); } } else { monitor.allDone(); } } } } /** * The MultiplePublisher class creates a session in * AUTO_ACKNOWLEDGE mode and publishes three messages * to a topic. */ public class MultiplePublisher extends Thread { /** * Runs the thread. */ public void run() { ConnectionFactory connectionFactory = null; Connection connection = null; Session session = null; Topic topic = null; MessageProducer publisher = null; TextMessage message = null; final int NUMMSGS = 3; final String MSG_TEXT = new String("Here is an auto-acknowledge message"); try { connectionFactory = SampleUtilities.getConnectionFactory(); connection = connectionFactory.createConnection(); session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); System.out.println("PUBLISHER: Created " + "auto-acknowledge session"); topic = SampleUtilities.getTopic(topicName, session); } catch (Exception e) { System.err.println("Connection problem: " + e.toString()); if (connection != null) { try { connection.close(); } catch (JMSException ee) { } } System.exit(1); } /* * After synchronizing with subscriber, create * publisher. * Send 3 messages, varying text slightly. * Send end-of-messages message. */ try { /* * Synchronize with subscriber. Wait for message * indicating that subscriber is ready to receive * messages. */ try { SampleUtilities.receiveSynchronizeMessages("PUBLISHER: ", CONTROL_QUEUE, 1); } catch (Exception e) { System.err.println("Queue probably " + "missing: " + e.toString()); if (connection != null) { try { connection.close(); } catch (JMSException ee) { } } System.exit(1); } publisher = session.createProducer(topic); message = session.createTextMessage(); for (int i = 0; i < NUMMSGS; i++) { message.setText(MSG_TEXT + " " + (i + 1)); System.out.println("PUBLISHER: Publishing " + "message: " + message.getText()); publisher.send(message); } /* * Send a non-text control message indicating * end of messages. */ publisher.send(session.createMessage()); } catch (JMSException e) { System.err.println("Exception occurred: " + e.toString()); } finally { if (connection != null) { try { connection.close(); } catch (JMSException e) { } } } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -