Package javax.jms

Examples of javax.jms.MapMessage


                return ex.toString();
            }
        } else if (m instanceof BytesMessage) {
            return jmsBytesBodyAsString(m);
        } else if (m instanceof MapMessage) {
            MapMessage msg = (MapMessage)m;
            HashMap props = new HashMap();
            // Get all MapMessage properties and stuff into a hash table
            try {
                for (Enumeration enu = msg.getMapNames();
                    enu.hasMoreElements();) {
                    String name = (enu.nextElement()).toString();
                    props.put(name, (msg.getObject(name)).toString());
                }
                return props.toString();
            } catch (JMSException ex) {
                return (ex.toString());
            }
        } else if (m instanceof ObjectMessage) {
            ObjectMessage msg = (ObjectMessage)m;
            Object obj = null;
            try {
                obj = msg.getObject();
                if (obj != null) {
                    return obj.toString();
                } else {
                    return "null";
                }
View Full Code Here


     * combo-box menu.
     */
    public void onMessage(Message msg) {

  try  {
            MapMessage mapMsg = (MapMessage)msg;
            String type = mapMsg.getStringProperty("type");

            if (type.equals(DEST_LIST_TOPIC_NAME))  {
    String oneRow[] = new String[ 3 ];

                TreeSet names = new TreeSet();

    /*
                 * Extract list of destinations
     */
    for (Enumeration e = mapMsg.getMapNames();
                     e.hasMoreElements();) {
                    String name = (String)e.nextElement();
                    Hashtable values = (Hashtable)mapMsg.getObject(name);

                    // Sort names by putting them into TreeSet
                    if (values.get("type").toString().equals("queue")) {
                        names.add((String)values.get("name"));
                    }
View Full Code Here

            Connection           connection = null;
            Session              session = null;
            Queue                vendorOrderQueue = null;
            Queue                retailerConfirmationQueue = null;
            MessageProducer      msgProducer = null;
            MapMessage           outMessage = null;
            MessageConsumer      orderConfirmationConsumer = null;
            MapMessage           inMessage = null;

            try {
                connectionFactory =
                    SampleUtilities.getConnectionFactory();
                connection =
                    connectionFactory.createConnection();
                session = connection.createSession(false,
                                                     Session.AUTO_ACKNOWLEDGE);
                vendorOrderQueue =
                    SampleUtilities.getQueue(vendorOrderQueueName, session);
                retailerConfirmationQueue =
                    SampleUtilities.getQueue(retailerConfirmationQueueName,
                                             session);
            } catch (Exception e) {
                System.out.println("Connection problem: " + e.toString());
                System.out.println("Program assumes five queues named A B C D E");
                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 {
                msgProducer = session.createProducer(vendorOrderQueue);
                outMessage = session.createMapMessage();
                outMessage.setString("Item", "Computer(s)");
                outMessage.setInt("Quantity", quantity);
                outMessage.setJMSReplyTo(retailerConfirmationQueue);
                msgProducer.send(outMessage);
                System.out.println("Retailer: ordered " + quantity
                                   + " computer(s)");

                orderConfirmationConsumer =
                    session.createConsumer(retailerConfirmationQueue);
                connection.start();
                inMessage = (MapMessage) orderConfirmationConsumer.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);
                msgProducer.send(outMessage);
                System.out.println("Retailer: ordered "
                                   + outMessage.getInt("Quantity")
                                   + " computer(s)");
                inMessage =
                    (MapMessage) orderConfirmationConsumer.receive();
                if (inMessage.getBoolean("OrderAccepted") == true) {
                    System.out.println("Retailer: Order filled");
                } else {
                    System.out.println("Retailer: Order not filled");
                }

View Full Code Here

            Queue                    storageOrderQueue = null;
            Queue                    vendorConfirmationQueue = null;
            MessageConsumer          vendorOrderMessageConsumer = null;
            MessageProducer          monitorOrderMessageProducer = null;
            MessageProducer          storageOrderMessageProducer = null;
            MapMessage               orderMessage = null;
            MessageConsumer          vendorConfirmationMessageConsumer = 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);
                monitorOrderQueue =
                    SampleUtilities.getQueue(monitorOrderQueueName, session);
                storageOrderQueue =
                    SampleUtilities.getQueue(storageOrderQueueName, session);
                vendorConfirmationQueue =
                    SampleUtilities.getQueue(vendorConfirmationQueueName, session);
            } catch (Exception e) {
                System.out.println("Connection problem: " + e.toString());
                System.out.println("Program assumes six queues named A B C D E F");
                if (connection != null) {
                    try {
                        connection.close();
                    } catch (JMSException ee) {}
                }
                System.exit(1);
            }

            try {
                /*
                 * Create receiver for vendor order queue, sender for
                 * supplier order queues, and message to send to suppliers.
                 */
                vendorOrderMessageConsumer =
                    session.createConsumer(vendorOrderQueue);
                monitorOrderMessageProducer =
                    session.createProducer(monitorOrderQueue);
                storageOrderMessageProducer =
                    session.createProducer(storageOrderQueue);
                orderMessage = session.createMapMessage();

                /*
                 * Configure an asynchronous message listener to process
                 * supplier replies to inquiries for parts to fill order.
                 * Start delivery.
                 */
                vendorConfirmationMessageConsumer =
                    asyncSession.createConsumer(vendorConfirmationQueue);
                listener = new VendorMessageListener(asyncSession, 2);
                vendorConfirmationMessageConsumer.setMessageListener(listener);
                connection.start();

                /*
                 * Process orders in vendor order queue.
                 * Use one transaction to receive order from order queue
                 * and send messages to suppliers' order queues to order
                 * components to fulfill the order placed with the vendor.
                 */
                while (true) {
                    try {

                        // Receive an order from a retailer.
                        inMessage = vendorOrderMessageConsumer.receive();
                        if (inMessage instanceof MapMessage) {
                            vendorOrderMessage = (MapMessage) inMessage;
                        } else {
                            /*
                             * Message is an end-of-message-stream message from
                             * retailer.  Send similar messages to suppliers,
                             * then break out of processing loop.
                             */
                            endOfMessageStream = session.createMessage();
                            endOfMessageStream.setJMSReplyTo(vendorConfirmationQueue);
                            monitorOrderMessageProducer.send(endOfMessageStream);
                            storageOrderMessageProducer.send(endOfMessageStream);
                            session.commit();
                            break;
                        }

                        /*
                         * A real application would check an inventory database
                         * and order only the quantities needed.  Throw an
                         * exception every few times to simulate a database
                         * concurrent-access exception and cause a rollback.
                         */
                        if (rgen.nextInt(3) == throwException) {
                            throw new JMSException("Simulated database concurrent access exception");
                        }

                        // Record retailer order as a pending order.
                        order = new Order(vendorOrderMessage);
                       
                        /*
                         * Set order number and reply queue for outgoing
                         * message.
                         */
                        orderMessage.setInt("VendorOrderNumber",
                                            order.orderNumber);
                        orderMessage.setJMSReplyTo(vendorConfirmationQueue);
                        quantity = vendorOrderMessage.getInt("Quantity");
                        System.out.println("Vendor: Retailer ordered " +
                                           quantity + " " +
                                           vendorOrderMessage.getString("Item"));

                        // Send message to monitor supplier.
                        orderMessage.setString("Item", "Monitor");
                        orderMessage.setInt("Quantity", quantity);
                        monitorOrderMessageProducer.send(orderMessage);
View Full Code Here

            /*
             * Message is an order confirmation message from a supplier.
             */
            int orderNumber = -1;
            try {
                MapMessage component = (MapMessage) message;

                /*
                 * Process the order confirmation message and commit the
                 * transaction.
                 */
                orderNumber = component.getInt("VendorOrderNumber");
                Order order =
                    Order.getOrder(orderNumber).processSubOrder(component);
                session.commit();
               
                /*
                 * If this message is the last supplier message, send message
                 * to Retailer and commit transaction.
                 */
                if (! order.isPending()) {
                    System.out.println("Vendor: Completed processing for order "
                        + order.orderNumber);
                    Queue replyQueue = (Queue) order.order.getJMSReplyTo();
                    MessageProducer mp = session.createProducer(replyQueue);
                    MapMessage retailerConfirmationMessage =
                        session.createMapMessage();
                    if (order.isFulfilled()) {
                        retailerConfirmationMessage.setBoolean("OrderAccepted",
                                                               true);
                        System.out.println("Vendor: sent " + order.quantity
                                           + " computer(s)");
                    } else if (order.isCancelled()) {
                        retailerConfirmationMessage.setBoolean("OrderAccepted",
                                                               false);
                        System.out.println("Vendor: unable to send " +
                                           order.quantity + " computer(s)");
                    }
                    mp.send(retailerConfirmationMessage);
View Full Code Here

            Connection           connection = null;
            Session              session = null;
            Queue                orderQueue = null;
            MessageConsumer      msgConsumer = null;
            Message              inMessage = null;
            MapMessage           orderMessage = null;
            MapMessage           outMessage = null;

            try {
                connectionFactory =
                    SampleUtilities.getConnectionFactory();
                connection =
                    connectionFactory.createConnection();
                session = connection.createSession(true, 0);
                orderQueue =
                    SampleUtilities.getQueue(IN_ORDER_QUEUE, session);
            } catch (Exception e) {
                System.out.println("Connection problem: " + e.toString());
                System.out.println("Program assumes six queues named A B C D E F");
                if (connection != null) {
                    try {
                        connection.close();
                    } catch (JMSException ee) {}
                }
                System.exit(1);
            }
           
            // Create receiver for order queue and start message delivery.
            try {
                msgConsumer = session.createConsumer(orderQueue);
                connection.start();
            } catch (JMSException je) {
                exitResult = 1;
            }

            /*
             * Keep checking supplier order queue for order request until
             * end-of-message-stream message is received.
             * Receive order and send an order confirmation as one transaction.
             */
            while (true) {
                try {
                    inMessage = msgConsumer.receive();
                    if (inMessage instanceof MapMessage) {
                        orderMessage = (MapMessage) inMessage;
                    } else {
                        /*
                         * Message is an end-of-message-stream message.
                         * Send a similar message to reply queue, commit
                         * transaction, then stop processing orders by breaking
                         * out of loop.
                         */
                        MessageProducer msgProducer =
                            session.createProducer((Queue) inMessage.getJMSReplyTo());
                        msgProducer.send(session.createMessage());
                        session.commit();
                        break;
                    }

                    // Extract quantity ordered from order message.
                    quantity = orderMessage.getInt("Quantity");
                    System.out.println(PRODUCT_NAME
                        + " Supplier: Vendor ordered " + quantity + " "
                        + orderMessage.getString("Item") + "(s)");

                    /*
                     * Create sender and message for reply queue.
                     * Set order number and item; check inventory and set
                     * quantity available. 
                     * Send message to vendor and commit transaction.
                     */
                    MessageProducer msgProducer =
                        session.createProducer((Queue) orderMessage.getJMSReplyTo());
                    outMessage = session.createMapMessage();
                    outMessage.setInt("VendorOrderNumber",
                                      orderMessage.getInt("VendorOrderNumber"));
                    outMessage.setString("Item", PRODUCT_NAME);
                    int numAvailable = checkInventory();
                    if (numAvailable >= quantity) {
                        outMessage.setInt("Quantity", quantity);
                    } else {
                        outMessage.setInt("Quantity", numAvailable);
                    }
                    msgProducer.send(outMessage);
                    System.out.println(PRODUCT_NAME + " Supplier: sent "
                                       + outMessage.getInt("Quantity") + " "
                                       + outMessage.getString("Item") + "(s)");
                    session.commit();
                    System.out.println("  " + PRODUCT_NAME
                                       + " Supplier: committed transaction");
                } catch (Exception e) {
                    System.out.println(PRODUCT_NAME
View Full Code Here

          "Processing message \"" + id + '@' + queueName
              + "\" reports " + level.toLowerCase() + "."
              + (message != null ? " (" + message + ")" : ""));
    } else {
      try {
        MapMessage report = ActiveMQDirector.getSession().createMapMessage();

        DateTime now = new DateTime();
        DateTimeFormatter iso8601formatter = ISODateTimeFormat.dateTime();
        report.setString("timestamp", iso8601formatter.print(now));
        report.setString("queue", queueName);
        report.setString("id", id);
        report.setString("level", level.toLowerCase());
        if (message != null)
          report.setString("message", message);

        ActiveMQDirector.getResultsTopic().send(report);

      } catch (Exception exce) {
        logger.fatal("Error sending report  for \"" + id + '@'
View Full Code Here

      super.tearDown();
   }

   public void testNullValue() throws Exception
   {
      MapMessage m = session.createMapMessage();

      m.setString("nullValue", null);

      queueProd.send(m);

      MapMessage rm = (MapMessage)queueCons.receive(2000);
     
      log.info("Got rm:" + rm);
     
      assertNotNull(rm);
     
      log.info("String is " + rm.getString("nullValue"));

      assertNull(rm.getString("nullValue"));
   }
View Full Code Here

   protected void prepareMessage(Message m) throws JMSException
   {
      super.prepareMessage(m);

      MapMessage mm = (MapMessage)m;

      mm.setBoolean("boolean", true);
      mm.setByte("byte", (byte)3);
      mm.setBytes("bytes", new byte[] { (byte)3, (byte)4, (byte)5 });
      mm.setChar("char", (char)6);
      mm.setDouble("double", 7.0);
      mm.setFloat("float", 8.0f);
      mm.setInt("int", 9);
      mm.setLong("long", 10l);
      mm.setObject("object", new String("this is an object"));
      mm.setShort("short", (short)11);
      mm.setString("string", "this is a string");
   }
View Full Code Here

   protected void assertEquivalent(Message m, int mode, boolean redelivery) throws JMSException
   {
      super.assertEquivalent(m, mode, redelivery);

      MapMessage mm = (MapMessage)m;

      assertEquals(true, mm.getBoolean("boolean"));
      assertEquals((byte)3, mm.getByte("byte"));
      byte[] bytes = mm.getBytes("bytes");
      assertEquals((byte)3, bytes[0]);
      assertEquals((byte)4, bytes[1]);
      assertEquals((byte)5, bytes[2]);
      assertEquals((char)6, mm.getChar("char"));
      assertEquals(new Double(7.0), new Double(mm.getDouble("double")));
      assertEquals(new Float(8.0f), new Float(mm.getFloat("float")));
      assertEquals(9, mm.getInt("int"));
      assertEquals(10l, mm.getLong("long"));
      assertEquals("this is an object", mm.getObject("object"));
      assertEquals((short)11, mm.getShort("short"));
      assertEquals("this is a string", mm.getString("string"));
   }
View Full Code Here

TOP

Related Classes of javax.jms.MapMessage

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.