public MapMessage convert(BusinessObject bo) throws Exception { MapMessage mm = context.createMapMessage(); for (String key : bo.keySet()) { mm.setObject(key, bo.get(key)); } return mm; }
public static void main(String[] args) { MapMessage outMessage; JMSConsumer orderConfirmReceiver; MapMessage inMessage; if (args.length != 1) { System.out.println("Error: Program takes numerical argument."); System.exit(1); } quantity = (new Integer(args[0])).intValue(); System.out.println("Retailer: Quantity to be ordered is " + quantity); /* * Create non-transacted context 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 (JMSContext context = connectionFactory.createContext(); ) { outMessage = context.createMapMessage(); outMessage.setString("Item", "Computer(s)"); outMessage.setInt("Quantity", quantity); outMessage.setJMSReplyTo(retailerConfirmQueue); context.createProducer().send(vendorOrderQueue, outMessage); System.out.println("Retailer: Ordered " + quantity + " computer(s)"); orderConfirmReceiver = context.createConsumer(retailerConfirmQueue); 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); context.createProducer().send(vendorOrderQueue, 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. */ context.createProducer().send(vendorOrderQueue, context.createMessage()); } catch (JMSException e) { System.err.println("Retailer: Exception occurred: " + e.toString()); } }