Example usage for javax.jms MapMessage getString

List of usage examples for javax.jms MapMessage getString

Introduction

In this page you can find the example usage for javax.jms MapMessage getString.

Prototype


String getString(String name) throws JMSException;

Source Link

Document

Returns the String value with the specified name.

Usage

From source file:Supplier.java

public void run() {
    ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(user, password, url);
    Session session = null;/*from  w ww .j  a va 2  s .c  o  m*/
    Destination orderQueue;
    try {
        Connection connection = connectionFactory.createConnection();

        session = connection.createSession(true, Session.SESSION_TRANSACTED);
        orderQueue = session.createQueue(QUEUE);
        MessageConsumer consumer = session.createConsumer(orderQueue);

        connection.start();

        while (true) {
            Message message = consumer.receive();
            MessageProducer producer = session.createProducer(message.getJMSReplyTo());
            MapMessage orderMessage;
            if (message instanceof MapMessage) {
                orderMessage = (MapMessage) message;
            } else {
                // End of Stream
                producer.send(session.createMessage());
                session.commit();
                producer.close();
                break;
            }

            int quantity = orderMessage.getInt("Quantity");
            System.out.println(
                    ITEM + " Supplier: Vendor ordered " + quantity + " " + orderMessage.getString("Item"));

            MapMessage outMessage = session.createMapMessage();
            outMessage.setInt("VendorOrderNumber", orderMessage.getInt("VendorOrderNumber"));
            outMessage.setString("Item", ITEM);

            quantity = Math.min(orderMessage.getInt("Quantity"),
                    new Random().nextInt(orderMessage.getInt("Quantity") * 10));
            outMessage.setInt("Quantity", quantity);

            producer.send(outMessage);
            System.out.println(ITEM + " Supplier: Sent " + quantity + " " + ITEM + "(s)");
            session.commit();
            System.out.println(ITEM + " Supplier: committed transaction");
            producer.close();
        }
        connection.close();
    } catch (JMSException e) {
        e.printStackTrace();
    }
}

From source file:Vendor.java

public void run() {
    ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(user, password, url);
    Session session = null;//from  w  w  w .j av  a  2s  .  c om
    Destination orderQueue;
    Destination monitorOrderQueue;
    Destination storageOrderQueue;
    TemporaryQueue vendorConfirmQueue;
    MessageConsumer orderConsumer = null;
    MessageProducer monitorProducer = null;
    MessageProducer storageProducer = null;

    try {
        Connection connection = connectionFactory.createConnection();

        session = connection.createSession(true, Session.SESSION_TRANSACTED);
        orderQueue = session.createQueue("VendorOrderQueue");
        monitorOrderQueue = session.createQueue("MonitorOrderQueue");
        storageOrderQueue = session.createQueue("StorageOrderQueue");

        orderConsumer = session.createConsumer(orderQueue);
        monitorProducer = session.createProducer(monitorOrderQueue);
        storageProducer = session.createProducer(storageOrderQueue);

        Connection asyncconnection = connectionFactory.createConnection();
        asyncSession = asyncconnection.createSession(true, Session.SESSION_TRANSACTED);

        vendorConfirmQueue = asyncSession.createTemporaryQueue();
        MessageConsumer confirmConsumer = asyncSession.createConsumer(vendorConfirmQueue);
        confirmConsumer.setMessageListener(this);

        asyncconnection.start();

        connection.start();

        while (true) {
            Order order = null;
            try {
                Message inMessage = orderConsumer.receive();
                MapMessage message;
                if (inMessage instanceof MapMessage) {
                    message = (MapMessage) inMessage;

                } else {
                    // end of stream
                    Message outMessage = session.createMessage();
                    outMessage.setJMSReplyTo(vendorConfirmQueue);
                    monitorProducer.send(outMessage);
                    storageProducer.send(outMessage);
                    session.commit();
                    break;
                }

                // Randomly throw an exception in here to simulate a Database error
                // and trigger a rollback of the transaction
                if (new Random().nextInt(3) == 0) {
                    throw new JMSException("Simulated Database Error.");
                }

                order = new Order(message);

                MapMessage orderMessage = session.createMapMessage();
                orderMessage.setJMSReplyTo(vendorConfirmQueue);
                orderMessage.setInt("VendorOrderNumber", order.getOrderNumber());
                int quantity = message.getInt("Quantity");
                System.out.println("Vendor: Retailer ordered " + quantity + " " + message.getString("Item"));

                orderMessage.setInt("Quantity", quantity);
                orderMessage.setString("Item", "Monitor");
                monitorProducer.send(orderMessage);
                System.out.println("Vendor: ordered " + quantity + " Monitor(s)");

                orderMessage.setString("Item", "HardDrive");
                storageProducer.send(orderMessage);
                System.out.println("Vendor: ordered " + quantity + " Hard Drive(s)");

                session.commit();
                System.out.println("Vendor: Comitted Transaction 1");

            } catch (JMSException e) {
                System.out.println("Vendor: JMSException Occured: " + e.getMessage());
                e.printStackTrace();
                session.rollback();
                System.out.println("Vendor: Rolled Back Transaction.");
            }
        }

        synchronized (supplierLock) {
            while (numSuppliers > 0) {
                try {
                    supplierLock.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }

        connection.close();
        asyncconnection.close();

    } catch (JMSException e) {
        e.printStackTrace();
    }

}

From source file:org.apache.falcon.messaging.FeedProducerTest.java

private void verifyMesssage(MessageConsumer consumer) throws JMSException {
    for (String instancePath : instancePaths) {
        // receive call is blocking
        MapMessage m = (MapMessage) consumer.receive();

        System.out.println("Received JMS message {}" + m.toString());
        System.out.println("Consumed: " + m.toString());
        assertMessage(m);/*from   ww w. j  a v a2  s  .  c  o m*/
        Assert.assertEquals(m.getString(WorkflowExecutionArgs.OUTPUT_FEED_PATHS.getName()), instancePath);
    }
}

From source file:org.apache.falcon.messaging.FeedProducerTest.java

private void assertMessage(MapMessage m) throws JMSException {
    Assert.assertEquals(m.getString(WorkflowExecutionArgs.ENTITY_NAME.getName()), TOPIC_NAME);
    Assert.assertEquals(m.getString(WorkflowExecutionArgs.OPERATION.getName()), "DELETE");
    Assert.assertEquals(m.getString(WorkflowExecutionArgs.WORKFLOW_ID.getName()), "workflow-01-00");
    Assert.assertEquals(m.getString(WorkflowExecutionArgs.WORKFLOW_USER.getName()), "falcon");
    Assert.assertEquals(m.getString(WorkflowExecutionArgs.RUN_ID.getName()), "1");
    Assert.assertEquals(m.getString(WorkflowExecutionArgs.NOMINAL_TIME.getName()), "2011-01-01-01-00");
    Assert.assertEquals(m.getString(WorkflowExecutionArgs.TIMESTAMP.getName()), "2012-01-01-01-00");
    Assert.assertEquals(m.getString(WorkflowExecutionArgs.STATUS.getName()), "SUCCEEDED");
}

From source file:org.apache.falcon.messaging.JMSMessageConsumer.java

private WorkflowExecutionContext createContext(MapMessage mapMessage) throws JMSException {
    // for backwards compatibility, read all args from message
    Map<WorkflowExecutionArgs, String> wfProperties = new HashMap<WorkflowExecutionArgs, String>();
    for (WorkflowExecutionArgs arg : WorkflowExecutionArgs.values()) {
        String optionValue = mapMessage.getString(arg.getName());
        if (StringUtils.isNotEmpty(optionValue)) {
            wfProperties.put(arg, optionValue);
        }//from   w  w w .ja  va2s  . c  o m
    }

    return WorkflowExecutionContext.create(wfProperties);
}

From source file:org.apache.falcon.messaging.ProcessProducerTest.java

private void consumer() throws JMSException {
    ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(BROKER_URL);
    Connection connection = connectionFactory.createConnection();
    connection.start();/*from   w  w  w .  ja v a  2s.c  om*/

    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Destination destination = session.createTopic(getTopicName());
    MessageConsumer consumer = session.createConsumer(destination);

    latch.countDown();

    for (int index = 0; index < outputFeedNames.length; ++index) {
        MapMessage m = (MapMessage) consumer.receive();
        System.out.println("Consumed: " + m.toString());
        assertMessage(m);
        Assert.assertEquals(m.getString(WorkflowExecutionArgs.OUTPUT_FEED_NAMES.getName()),
                outputFeedNames[index]);
        Assert.assertEquals(m.getString(WorkflowExecutionArgs.OUTPUT_FEED_PATHS.getName()),
                outputFeedPaths[index]);
    }
    connection.close();
}

From source file:org.apache.falcon.messaging.ProcessProducerTest.java

private void assertMessage(MapMessage m) throws JMSException {
    Assert.assertEquals(m.getString(WorkflowExecutionArgs.ENTITY_NAME.getName()), ENTITY_NAME);
    Assert.assertEquals(m.getString(WorkflowExecutionArgs.WORKFLOW_ID.getName()), "workflow-01-00");
    Assert.assertEquals(m.getString(WorkflowExecutionArgs.WORKFLOW_USER.getName()), "falcon");
    Assert.assertEquals(m.getString(WorkflowExecutionArgs.RUN_ID.getName()), "1");
    Assert.assertEquals(m.getString(WorkflowExecutionArgs.NOMINAL_TIME.getName()), "2011-01-01-01-00");
    Assert.assertEquals(m.getString(WorkflowExecutionArgs.TIMESTAMP.getName()), "2012-01-01-01-00");
    Assert.assertEquals(m.getString(WorkflowExecutionArgs.STATUS.getName()), "SUCCEEDED");
}

From source file:org.apache.falcon.oozie.workflow.FalconPostProcessingTest.java

private void verifyMesssage(MessageConsumer consumer) throws JMSException {
    for (int index = 0; index < outputFeedPaths.length; ++index) {
        // receive call is blocking
        MapMessage m = (MapMessage) consumer.receive();

        System.out.println("Received JMS message {}" + m.toString());
        System.out.println("Consumed: " + m.toString());
        assertMessage(m);/* w ww .j  a va 2 s  .  c  o m*/
        Assert.assertEquals(m.getString(WorkflowExecutionArgs.OUTPUT_FEED_NAMES.getName()),
                outputFeedNames[index]);
        Assert.assertEquals(m.getString(WorkflowExecutionArgs.OUTPUT_FEED_PATHS.getName()),
                outputFeedPaths[index]);
    }
}

From source file:org.apache.falcon.oozie.workflow.FalconPostProcessingTest.java

private void assertMessage(MapMessage m) throws JMSException {
    Assert.assertEquals(m.getString(WorkflowExecutionArgs.ENTITY_NAME.getName()), "agg-coord");
    String workflowUser = m.getString(WorkflowExecutionArgs.WORKFLOW_USER.getName());
    if (workflowUser != null) { // in case of user message, its NULL
        Assert.assertEquals(workflowUser, "falcon");
    }//from  w  ww  .ja  v  a  2  s.  co m
    Assert.assertEquals(m.getString(WorkflowExecutionArgs.NOMINAL_TIME.getName()), "2011-01-01-01-00");
    Assert.assertEquals(m.getString(WorkflowExecutionArgs.TIMESTAMP.getName()), "2012-01-01-01-00");
    Assert.assertEquals(m.getString(WorkflowExecutionArgs.STATUS.getName()), "SUCCEEDED");
}

From source file:org.mule.transport.jms.JmsMessageUtilsTestCase.java

/**
 * Tests that is able to convert a Map which only contains simple values into a
 * MapMessage./*w  w  w  .j  a  v  a2  s  . c o m*/
 */
@Test
public void testConvertsValidMapWithSimpleValuesToMapMessage() throws JMSException {
    Session session = mock(Session.class);
    when(session.createMapMessage()).thenReturn(new ActiveMQMapMessage());

    // Creates a test Map with data
    Map data = new HashMap();
    data.put("value1", new Float(4));
    data.put("value2", new byte[] { 1, 2, 3 });
    data.put("value3", "value3");
    data.put("value4", new Double(67.9));
    data.put("value5", true);
    data.put("value6", null);

    Message message = JmsMessageUtils.toMessage(data, session);
    assertTrue(message instanceof MapMessage);

    MapMessage mapMessage = (MapMessage) message;
    assertEquals(new Float(4), mapMessage.getFloat("value1"), 0);
    assertTrue(Arrays.equals(new byte[] { 1, 2, 3 }, mapMessage.getBytes("value2")));
    assertEquals("value3", mapMessage.getString("value3"));
    assertEquals(new Double(67.9), mapMessage.getDouble("value4"), 0);
    assertTrue(mapMessage.getBoolean("value5"));
    assertNull(mapMessage.getObject("value6"));
}