ackequivexample.java
来自「j2ee API 开发重要工具 免费下载 欢迎使用」· Java 代码 · 共 451 行 · 第 1/2 页
JAVA
451 行
* 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 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) {} } } } } /** * 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 != 0) { System.out.println("Program takes no arguments."); System.exit(1); } System.out.println("Queue name is " + aee.CONTROL_QUEUE); System.out.println("Queue name is " + aee.queueName); System.out.println("Topic name is " + aee.topicName); System.out.println("Connection factory name is " + aee.conFacName); aee.run_threads(); System.exit(0); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?