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

📄 messageheaderstopic.java

📁 JMS的例子程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                try {                    SampleUtilities.sendSynchronizeMessage("SUBSCRIBER 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);    	        }                for (int i = 0; i < 3; i++) {                    message = (TextMessage) topicSubscriber.receive();                    System.out.println("\nSUBSCRIBER THREAD: Message received: "                         + message.getText());                    System.out.println("SUBSCRIBER THREAD: Headers after message is received:");                    displayHeaders(message, "SUBSCRIBER THREAD: ");                }            } catch (JMSException e) {                System.out.println("Exception occurred: " + e.toString());                exitResult = 1;            } finally {                if (topicConnection != null) {                    try {                        topicConnection.close();                    } catch (JMSException e) {                        exitResult = 1;                    }                }            }   	            }    }        /**     * Displays all message headers.  Each display is in a try/catch block in     * case the header is not set before the message is published.     *     * @param message	the message whose headers are to be displayed     * @param prefix	the prefix (publisher or subscriber) to be displayed     */    public void displayHeaders (Message message, String prefix) {        Destination  dest = null;              int          delMode = 0;        long         expiration = 0;        Time         expTime = null;        int          priority = 0;        String       msgID = null;        long         timestamp = 0;        Time         timestampTime = null;        String       correlID = null;        Destination  replyTo = null;        boolean      redelivered = false;        String       type = null;        String       propertyName = null;                try {            System.out.println(prefix + "Headers set by publish/send method: ");                        // Display the destination (topic, in this case).            try {                dest = message.getJMSDestination();                System.out.println(prefix + " JMSDestination: " + dest);            } catch (Exception e) {                System.out.println(prefix + "Exception occurred: "                     + e.toString());                exitResult = 1;            }                        // Display the delivery mode.            try {                delMode = message.getJMSDeliveryMode();                System.out.print(prefix);                if (delMode == DeliveryMode.NON_PERSISTENT) {                    System.out.println(" JMSDeliveryMode: non-persistent");                } else if (delMode == DeliveryMode.PERSISTENT) {                    System.out.println(" JMSDeliveryMode: persistent");                } else {                    System.out.println(" JMSDeliveryMode: neither persistent nor non-persistent; error");                }            } catch (Exception e) {                System.out.println(prefix + "Exception occurred: "                     + e.toString());                exitResult = 1;            }                        /*             * Display the expiration time.  If value is 0 (the default),             * the message never expires.  Otherwise, cast the value             * to a Time object for display.             */            try {                expiration = message.getJMSExpiration();                System.out.print(prefix);                if (expiration != 0) {                    expTime = new Time(expiration);                    System.out.println(" JMSExpiration: " + expTime);                } else {                    System.out.println(" JMSExpiration: " + expiration);                }            } catch (Exception e) {                System.out.println(prefix + "Exception occurred: "                     + e.toString());                exitResult = 1;            }                        // Display the priority.            try {                priority = message.getJMSPriority();                System.out.println(prefix + " JMSPriority: " + priority);            } catch (Exception e) {                System.out.println(prefix + "Exception occurred: "                     + e.toString());                exitResult = 1;            }                        // Display the message ID.            try {                msgID = message.getJMSMessageID();                System.out.println(prefix + " JMSMessageID: " + msgID);            } catch (Exception e) {                System.out.println(prefix + "Exception occurred: "                     + e.toString());                exitResult = 1;            }                        /*             * Display the timestamp.             * If value is not 0, cast it to a Time object for display.             */            try {                timestamp = message.getJMSTimestamp();                System.out.print(prefix);                if (timestamp != 0) {                    timestampTime = new Time(timestamp);                    System.out.println(" JMSTimestamp: " + timestampTime);                } else {                    System.out.println(" JMSTimestamp: " + timestamp);                }            } catch (Exception e) {                System.out.println(prefix + "Exception occurred: "                     + e.toString());                exitResult = 1;            }                        // Display the correlation ID.            try {                correlID = message.getJMSCorrelationID();                System.out.println(prefix + " JMSCorrelationID: " + correlID);            } catch (Exception e) {                System.out.println(prefix + "Exception occurred: "                     + e.toString());                exitResult = 1;            }                       // Display the ReplyTo destination.           try {                replyTo = message.getJMSReplyTo();                System.out.println(prefix + " JMSReplyTo: " + replyTo);            } catch (Exception e) {                System.out.println(prefix + "Exception occurred: "                     + e.toString());                exitResult = 1;            }                        // Display the Redelivered value (usually false).            System.out.println(prefix + "Header set by JMS provider:");            try {                redelivered = message.getJMSRedelivered();                System.out.println(prefix + " JMSRedelivered: " + redelivered);            } catch (Exception e) {                System.out.println(prefix + "Exception occurred: "                     + e.toString());                exitResult = 1;            }                        // Display the JMSType.            System.out.println(prefix + "Headers set by client program:");            try {                type = message.getJMSType();                System.out.println(prefix + " JMSType: " + type);            } catch (Exception e) {                System.out.println(prefix + "Exception occurred: "                     + e.toString());                exitResult = 1;            }                        // Display any client properties.            try {                for (Enumeration e = message.getPropertyNames(); e.hasMoreElements() ;) {                    propertyName = new String((String) e.nextElement());                    System.out.println(prefix + " Client property "                         + propertyName + ": "                         + message.getObjectProperty(propertyName));                 }            } catch (Exception e) {                System.out.println(prefix + "Exception occurred: "                     + e.toString());                exitResult = 1;            }        } catch (Exception e) {                System.out.println(prefix + "Exception occurred: "                     + e.toString());            exitResult = 1;        }    }    /**     * Instantiates the subscriber and publisher classes and starts their     * threads.     * Calls the join method to wait for the threads to die.     * <p>     * It is essential to start the subscriber before starting the publisher.     * In the publish/subscribe model, a subscriber can ordinarily receive only      * messages published while it is active.     */    public void run_threads() {        HeaderSubscriber  headerSubscriber = new HeaderSubscriber();        HeaderPublisher   headerPublisher = new HeaderPublisher();        headerSubscriber.start();        headerPublisher.start();        try {            headerSubscriber.join();            headerPublisher.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) {        MessageHeadersTopic  mht = new MessageHeadersTopic();        if (args.length != 1) {    	    System.out.println("Usage: java MessageHeadersTopic <topic_name>");    	    System.exit(1);    	}        mht.topicName = new String(args[0]);        System.out.println("Topic name is " + mht.topicName);    	mht.run_threads();    	SampleUtilities.exit(mht.exitResult);     }}

⌨️ 快捷键说明

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