⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 topicselectors.java

📁 JMS的例子程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
     * message.     *     * @author Kim Haase     * @version 1.6, 08/18/00     */    public class Subscriber extends Thread {        String                  whatKind;        int                     subscriberNumber;        /**         * The MultipleListener class implements the MessageListener interface           * by defining an onMessage method for the Subscriber class.         *         * @author Kim Haase         * @version 1.6, 08/18/00         */        private class MultipleListener implements MessageListener {            final SampleUtilities.DoneLatch  monitor =                new SampleUtilities.DoneLatch();            /**             * Displays the message text.             * If the value of the NewsType property is "Finished", the message              * listener sets its monitor state to all done processing messages.             *             * @param inMessage	the incoming message             */            public void onMessage(Message inMessage) {                TextMessage  msg = (TextMessage) inMessage;                String       newsType;                try {                    System.out.println("SUBSCRIBER " + subscriberNumber                                        + " THREAD: Message received: "                                        + msg.getText());                    newsType = msg.getStringProperty("NewsType");                    if (newsType.equals(TopicSelectors.END_OF_MESSAGE_STREAM_TYPE)) {                        System.out.println("SUBSCRIBER " + subscriberNumber                              + " THREAD: Received finished-publishing message");                        monitor.allDone();                    }                } catch(JMSException e) {                    System.out.println("Exception in onMessage(): "                                        + e.toString());                }            }        }                /**         * Constructor.  Sets whatKind to indicate the type of         * message this Subscriber object will listen for; sets         * subscriberNumber based on Subscriber array index.         *         * @param str	a String from the MESSAGE_TYPES array         * @param num	the index of the Subscriber array         */        public Subscriber(String str, int num) {            whatKind = str;            subscriberNumber = num + 1;        }         /**         * Runs the thread.         */        public void run() {            TopicConnectionFactory  topicConnectionFactory = null;            TopicConnection         topicConnection = null;            TopicSession            topicSession = null;            Topic                   topic = null;            String                  selector = null;            TopicSubscriber         topicSubscriber = null;            MultipleListener        multipleListener = new MultipleListener();            try {                topicConnectionFactory =                     SampleUtilities.getTopicConnectionFactory();                topicConnection =                     topicConnectionFactory.createTopicConnection();                topicSession = topicConnection.createTopicSession(false,                     Session.AUTO_ACKNOWLEDGE);                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 subscriber with message selector.             * Start message delivery.             * Send synchronize message to publisher, then wait till all             * messages have arrived.             * Listener displays the messages obtained.             */            try {                selector = new String("NewsType = '" + whatKind + "'" +                                       " OR NewsType = '" + END_OF_MESSAGE_STREAM_TYPE + "'");                System.out.println("SUBSCRIBER " + subscriberNumber                                     + " THREAD: selector is \"" + selector + "\"");                topicSubscriber =                     topicSession.createSubscriber(topic, selector, false);                topicSubscriber.setMessageListener(multipleListener);                topicConnection.start();                                // Let publisher know that subscriber is ready.                try {                    SampleUtilities.sendSynchronizeMessage("SUBSCRIBER "                                                             + subscriberNumber                                                             + " THREAD: ",                                                             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 appropriate news messages.                 * Block until publisher issues a finished message.                 */                multipleListener.monitor.waitTillDone();            } catch (JMSException e) {                System.out.println("Exception occurred: " + e.toString());                exitResult = 1;            } finally {                if (topicConnection != null) {                    try {                        topicConnection.close();                    } catch (JMSException e) {                        exitResult = 1;                    }                }            }   	            }    }        /**     * Creates an array of Subscriber objects, one for each of three message       * types including the Finished type, and starts their threads.     * Creates a Publisher object and starts its thread.     * Calls the join method to wait for the threads to die.     */    public void run_threads() {        final       int NUM_SUBSCRIBERS = 3;        Subscriber  subscriberArray[] = new Subscriber[NUM_SUBSCRIBERS];        Publisher   publisher = new Publisher(NUM_SUBSCRIBERS);        subscriberArray[0] = new Subscriber(MESSAGE_TYPES[2], 0);        subscriberArray[0].start();                subscriberArray[1] = new Subscriber(MESSAGE_TYPES[3], 1);        subscriberArray[1].start();        subscriberArray[2] = new Subscriber(MESSAGE_TYPES[4], 2);        subscriberArray[2].start();            publisher.start();                for (int i = 0; i < subscriberArray.length; i++) {            try {                subscriberArray[i].join();            } catch (InterruptedException e) {}        }                try {            publisher.join();        } catch (InterruptedException e) {}    }        /**     * Reads the topic name 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) {        TopicSelectors  ts = new TopicSelectors();        if (args.length != 1) {    	    System.out.println("Usage: java TopicSelectors <topic_name>");    	    System.exit(1);    	}    	        ts.topicName = new String(args[0]);        System.out.println("Topic name is " + ts.topicName);    	ts.run_threads();    	SampleUtilities.exit(ts.exitResult);    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -