📄 ackequivexample.java
字号:
System.out.println("SUBSCRIBER: Processing message: " + msg.getText()); } catch (JMSException e) { System.out.println("Exception in onMessage(): " + e.toString()); } } else { monitor.allDone(); } } } /** * Runs the thread. */ public void run() { TopicConnectionFactory topicConnectionFactory = null; TopicConnection topicConnection = null; TopicSession topicSession = null; Topic topic = null; TopicSubscriber topicSubscriber = null; TextListener topicListener = null; try { topicConnectionFactory = SampleUtilities.getTopicConnectionFactory(); topicConnection = topicConnectionFactory.createTopicConnection(); topicConnection.setClientID("AckEquivExample"); topicSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); System.out.println("SUBSCRIBER: Created auto-acknowledge session"); topic = SampleUtilities.getTopic(topicName, topicSession); } catch (Exception e) { System.out.println("Connection problem: " + e.toString()); if (topicConnection != null) { try { topicConnection.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 { topicSubscriber = topicSession.createDurableSubscriber(topic, "AckEquivExampleSubscription"); topicListener = new TextListener(); topicSubscriber.setMessageListener(topicListener); topicConnection.start(); // Let publisher know that subscriber is ready. try { SampleUtilities.sendSynchronizeMessage("SUBSCRIBER: ", CONTROL_QUEUE); } catch (Exception e) { System.out.println("Queue probably missing: " + e.toString()); if (topicConnection != null) { try { topicConnection.close(); } catch (JMSException ee) {} } System.exit(1); } /* * Asynchronously process messages. * Block until publisher issues a control message indicating * end of publish stream. */ topicListener.monitor.waitTillDone(); topicSubscriber.close(); topicSession.unsubscribe("AckEquivExampleSubscription"); } catch (JMSException e) { System.out.println("Exception occurred: " + e.toString()); exitResult = 1; } finally { if (topicConnection != null) { try { topicConnection.close(); } catch (JMSException e) { exitResult = 1; } } } } } /** * The MultiplePublisher class creates a session in AUTO_ACKNOWLEDGE mode * and publishes three messages to a topic. * * @author Kim Haase * @version 1.6, 08/18/00 */ public class MultiplePublisher extends Thread { /** * Runs the thread. */ public void run() { TopicConnectionFactory topicConnectionFactory = null; TopicConnection topicConnection = null; TopicSession topicSession = null; Topic topic = null; TopicPublisher topicPublisher = null; TextMessage message = null; final int NUMMSGS = 3; final String MSG_TEXT = new String("Here is an auto-acknowledge message"); try { topicConnectionFactory = SampleUtilities.getTopicConnectionFactory(); topicConnection = topicConnectionFactory.createTopicConnection(); topicSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); System.out.println("PUBLISHER: Created auto-acknowledge session"); topic = SampleUtilities.getTopic(topicName, topicSession); } catch (Exception e) { System.out.println("Connection problem: " + e.toString()); if (topicConnection != null) { try { topicConnection.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.out.println("Queue probably missing: " + e.toString()); if (topicConnection != null) { try { topicConnection.close(); } catch (JMSException ee) {} } System.exit(1); } topicPublisher = topicSession.createPublisher(topic); message = topicSession.createTextMessage(); for (int i = 0; i < NUMMSGS; i++) { message.setText(MSG_TEXT + " " + (i + 1)); System.out.println("PUBLISHER: Publishing message: " + message.getText()); topicPublisher.publish(message); } // Send a non-text control message indicating end of messages. topicPublisher.publish(topicSession.createMessage()); } catch (JMSException e) { System.out.println("Exception occurred: " + e.toString()); exitResult = 1; } finally { if (topicConnection != null) { try { topicConnection.close(); } catch (JMSException e) { exitResult = 1; } } } } } /** * Instantiates the sender, receiver, subscriber, and publisher classes and * starts their threads. * Calls the join method to wait for the threads to die. */ public void run_threads() { SynchSender synchSender = new SynchSender(); SynchReceiver synchReceiver = new SynchReceiver(); AsynchSubscriber asynchSubscriber = new AsynchSubscriber(); MultiplePublisher multiplePublisher = new MultiplePublisher(); synchSender.start(); synchReceiver.start(); try { synchSender.join(); synchReceiver.join(); } catch (InterruptedException e) {} asynchSubscriber.start(); multiplePublisher.start(); try { asynchSubscriber.join(); multiplePublisher.join(); } catch (InterruptedException e) {} } /** * Reads the queue and topic names from the command line, then calls the * run_threads method to execute the program threads. * * @param args the topic used by the example */ public static void main(String[] args) { AckEquivExample aee = new AckEquivExample(); if (args.length != 2) { System.out.println("Usage: java AckEquivExample <queue_name> <topic_name>"); System.exit(1); } aee.queueName = new String(args[0]); aee.topicName = new String(args[1]); System.out.println("Queue name is " + aee.queueName); System.out.println("Topic name is " + aee.topicName); aee.run_threads(); SampleUtilities.exit(aee.exitResult); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -