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

📄 transactedexample.java

📁 java书 java书 java书 java书 java书 java书
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
        private static final int CANCELLED_STATUS = 2;        private static final int FULFILLED_STATUS = 3;        // Original order from retailer        public final MapMessage order;        public final int orderNumber;        // Reply from supplier        public MapMessage monitor = null;        // Reply from supplier        public MapMessage storage = null;        public int quantity;        int status;        /**         * Constructor.  Sets order number; sets order and         * quantity from incoming message. Sets status to         * pending, and adds order to hash table of pending         * orders.         *         * @param order    the message containing the order         */        public Order(MapMessage order) {            this.orderNumber = getNextOrderNumber();            this.order = order;            try {                this.quantity = order.getInt("Quantity");            } catch (JMSException je) {                System.err.println(                        "Unexpected error. Message " + "missing Quantity");                this.quantity = 0;            }            status = PENDING_STATUS;            pendingOrders.put(                new Integer(orderNumber),                this);        }        /**         * Returns the next order number and increments the         * static variable that holds this value.         *         * @return    the next order number         */        private static int getNextOrderNumber() {            int result = nextOrderNumber;            nextOrderNumber++;            return result;        }        /**         * Returns the number of orders in the hash table.         *         * @return    the number of pending orders         */        public static int outstandingOrders() {            return pendingOrders.size();        }        /**         * Returns the order corresponding to a given order         * number.         *         * @param orderNumber   the number of the requested order         * @return              the requested order         */        public static Order getOrder(int orderNumber) {            return (Order) pendingOrders.get(new Integer(orderNumber));        }        /**         * Called by the onMessage method of the         * VendorMessageListener class to process a reply from         * a supplier to the Vendor.         *         * @param component    the message from the supplier         * @return             the order with updated status         *                     information         */        public Order processSubOrder(MapMessage component) {            String itemName = null;            // Determine which subcomponent this is.            try {                itemName = component.getString("Item");            } catch (JMSException je) {                System.err.println(                        "Unexpected exception. " + "Message missing Item");            }            if (itemName.compareTo("Monitor") == 0) {                monitor = component;            } else if (itemName.compareTo("Hard Drive") == 0) {                storage = component;            }            /*             * If notification for all subcomponents has been             * received, verify the quantities to compute if able             * to fulfill order.             */            if ((monitor != null) && (storage != null)) {                try {                    if (quantity > monitor.getInt("Quantity")) {                        status = CANCELLED_STATUS;                    } else if (quantity > storage.getInt("Quantity")) {                        status = CANCELLED_STATUS;                    } else {                        status = FULFILLED_STATUS;                    }                } catch (JMSException je) {                    System.err.println(                            "Unexpected exception: " + je.toString());                    status = CANCELLED_STATUS;                }                /*                 * Processing of order is complete, so remove it                 * from pending-order list.                 */                pendingOrders.remove(new Integer(orderNumber));            }            return this;        }        /**         * Determines if order status is pending.         *         * @return    true if order is pending, false if not         */        public boolean isPending() {            return status == PENDING_STATUS;        }        /**         * Determines if order status is cancelled.         *         * @return    true if order is cancelled, false if not         */        public boolean isCancelled() {            return status == CANCELLED_STATUS;        }        /**         * Determines if order status is fulfilled.         *         * @return    true if order is fulfilled, false if not         */        public boolean isFulfilled() {            return status == FULFILLED_STATUS;        }    }    /**     * The Retailer class orders a number of computers by sending     * a message to a vendor.  It then waits for the order to be     * confirmed.     *     * In this example, the Retailer places two orders, one for     * the quantity specified on the command line and one for     * twice that number.     *     * This class does not use transactions.     */    public static class Retailer extends Thread {        int quantity = 0;        /**         * Constructor.  Instantiates the retailer with the         * quantity of computers being ordered.         *         * @param q    the quantity specified in the program         *             arguments         */        public Retailer(int q) {            quantity = q;        }        /**         * Runs the thread.         */        public void run() {            Connection connection = null;            Session session = null;            MessageProducer producer = null;            MapMessage outMessage = null;            MessageConsumer orderConfirmReceiver = null;            MapMessage inMessage = null;            try {                connection = connectionFactory.createConnection();                session = connection.createSession(                            false,                            Session.AUTO_ACKNOWLEDGE);            } catch (Exception e) {                System.err.println("Connection problem: " + e.toString());                System.err.println(                        "Program assumes three "                        + "queues named jms/AQueue, jms/BQueue, and jms/CQueue "                        + "and one topic named jms/OTopic");                if (connection != null) {                    try {                        connection.close();                    } catch (JMSException ee) {                    }                }                System.exit(1);            }            /*             * Create non-transacted session and sender for             * vendor order queue.             * Create message to vendor, setting item and             * quantity values.             * Send message.             * Create receiver for retailer confirmation queue.             * Get message and report result.             * Send an end-of-message-stream message so vendor             * will stop processing orders.             */            try {                producer = session.createProducer(vendorOrderQueue);                outMessage = session.createMapMessage();                outMessage.setString("Item", "Computer(s)");                outMessage.setInt("Quantity", quantity);                outMessage.setJMSReplyTo(retailerConfirmQueue);                producer.send(outMessage);                System.out.println(                        "Retailer: ordered " + quantity + " computer(s)");                orderConfirmReceiver = session.createConsumer(                            retailerConfirmQueue);                connection.start();                inMessage = (MapMessage) orderConfirmReceiver.receive();                if (inMessage.getBoolean("OrderAccepted") == true) {                    System.out.println("Retailer: Order filled");                } else {                    System.out.println("Retailer: Order not " + "filled");                }                System.out.println("Retailer: placing another " + "order");                outMessage.setInt("Quantity", quantity * 2);                producer.send(outMessage);                System.out.println(                        "Retailer: ordered " + outMessage.getInt("Quantity")                        + " computer(s)");                inMessage = (MapMessage) orderConfirmReceiver.receive();                if (inMessage.getBoolean("OrderAccepted") == true) {                    System.out.println("Retailer: Order filled");                } else {                    System.out.println("Retailer: Order not " + "filled");                }                /*                 * Send a non-text control message indicating end                 * of messages.                 */                producer.send(session.createMessage());            } catch (Exception e) {                System.err.println(                        "Retailer: Exception " + "occurred: " + e.toString());                e.printStackTrace();            } finally {                if (connection != null) {                    try {                        connection.close();                    } catch (JMSException e) {                    }                }            }        }    }    /**     * The Vendor class uses one transaction to receive the     * computer order from the retailer and order the needed     * number of monitors and disk drives from its suppliers.     * At random intervals, it throws an exception to simulate a     * database problem and cause a rollback.     *     * The class uses an asynchronous message listener to process     * replies from suppliers. When all outstanding supplier     * inquiries complete, it sends a message to the Retailer     * accepting or refusing the order.     */    public static class Vendor extends Thread {        Random rgen = new Random();        int throwException = 1;        /**         * Runs the thread.         */

⌨️ 快捷键说明

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