📄 transactedexample.java
字号:
/* * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved. U.S. * Government Rights - Commercial software. Government users are subject * to the Sun Microsystems, Inc. standard license agreement and * applicable provisions of the FAR and its supplements. Use is subject * to license terms. * * This distribution may include materials developed by third parties. * Sun, Sun Microsystems, the Sun logo, Java and J2EE are trademarks * or registered trademarks of Sun Microsystems, Inc. in the U.S. and * other countries. * * Copyright (c) 2005 Sun Microsystems, Inc. Tous droits reserves. * * Droits du gouvernement americain, utilisateurs gouvernementaux - logiciel * commercial. Les utilisateurs gouvernementaux sont soumis au contrat de * licence standard de Sun Microsystems, Inc., ainsi qu'aux dispositions * en vigueur de la FAR (Federal Acquisition Regulations) et des * supplements a celles-ci. Distribue par des licences qui en * restreignent l'utilisation. * * Cette distribution peut comprendre des composants developpes par des * tierces parties. Sun, Sun Microsystems, le logo Sun, Java et J2EE * sont des marques de fabrique ou des marques deposees de Sun * Microsystems, Inc. aux Etats-Unis et dans d'autres pays. */import java.util.*;import javax.jms.*;public class TransactedExample { public static String vendorOrderQueueName = null; public static String retailerConfirmQueueName = null; public static String supplierOrderTopicName = null; public static String vendorConfirmQueueName = null; /** * Creates the Retailer and Vendor classes and the two * supplier classes, then starts the threads. * * @param quantity the quantity specified on the command * line */ public static void run_threads(int quantity) { Retailer r = new Retailer(quantity); Vendor v = new Vendor(); GenericSupplier ms = new GenericSupplier("Monitor", supplierOrderTopicName); GenericSupplier ss = new GenericSupplier("Hard Drive", supplierOrderTopicName); ms.start(); ss.start(); r.start(); v.start(); try { ms.join(); ss.join(); r.join(); v.join(); } catch (InterruptedException e) { } } /** * Reads the order quantity from the command line, then * calls the run_threads method to execute the program * threads. * * @param args the quantity of computers being ordered */ public static void main(String[] args) { TransactedExample te = new TransactedExample(); int quantity = 0; if (args.length != 1) { System.out.println("Program takes numerical argument."); System.err.println("Program assumes three " + "queues named jms/AQueue, jms/BQueue, and jms/CQueue " + "and one topic named jms/OTopic"); System.exit(1); } te.vendorOrderQueueName = new String("jms/AQueue"); te.retailerConfirmQueueName = new String("jms/BQueue"); te.supplierOrderTopicName = new String("jms/OTopic"); te.vendorConfirmQueueName = new String("jms/CQueue"); quantity = (new Integer(args[0])).intValue(); System.out.println("Quantity to be ordered is " + quantity); if (quantity > 0) { te.run_threads(quantity); System.exit(0); } else { System.out.println("Quantity must be positive and " + "nonzero"); System.exit(1); } } /** * 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() { ConnectionFactory connectionFactory = null; Connection connection = null; Session session = null; javax.jms.Queue vendorOrderQueue = null; javax.jms.Queue retailerConfirmQueue = null; MessageProducer producer = null; MapMessage outMessage = null; MessageConsumer orderConfirmReceiver = null; MapMessage inMessage = null; try { connectionFactory = SampleUtilities.getConnectionFactory(); connection = connectionFactory.createConnection(); session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); vendorOrderQueue = SampleUtilities.getQueue(vendorOrderQueueName, session); retailerConfirmQueue = SampleUtilities.getQueue(retailerConfirmQueueName, session); } 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. */ public void run() { ConnectionFactory connectionFactory = null; Connection connection = null; Session session = null; Session asyncSession = null; javax.jms.Queue vendorOrderQueue = null; Topic supplierOrderTopic = null; javax.jms.Queue vendorConfirmQueue = null; MessageConsumer vendorOrderReceiver = null; MessageProducer supplierOrderProducer = null; MapMessage orderMessage = null; MessageConsumer vendorConfirmReceiver = null; VendorMessageListener listener = null; Message inMessage = null; MapMessage vendorOrderMessage = null; Message endOfMessageStream = null; Order order = null; int quantity = 0; try { connectionFactory = SampleUtilities.getConnectionFactory(); connection = connectionFactory.createConnection(); session = connection.createSession(true, 0); asyncSession = connection.createSession(true, 0); vendorOrderQueue = SampleUtilities.getQueue(vendorOrderQueueName, session); supplierOrderTopic = SampleUtilities.getTopic(supplierOrderTopicName, session); vendorConfirmQueue = SampleUtilities.getQueue(vendorConfirmQueueName, session); } 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) { } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -