Example usage for javax.jms MapMessage getInt

List of usage examples for javax.jms MapMessage getInt

Introduction

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

Prototype


int getInt(String name) throws JMSException;

Source Link

Document

Returns the int value with the specified name.

Usage

From source file:Vendor.java

public void onMessage(Message message) {
    if (!(message instanceof MapMessage)) {
        synchronized (supplierLock) {
            numSuppliers--;//from   w  w  w . ja va 2  s .  c om
            supplierLock.notifyAll();
        }
        try {
            asyncSession.commit();
            return;
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }

    int orderNumber = -1;
    try {
        MapMessage componentMessage = (MapMessage) message;

        orderNumber = componentMessage.getInt("VendorOrderNumber");
        Order order = Order.getOrder(orderNumber);
        order.processSubOrder(componentMessage);
        asyncSession.commit();

        if (!"Pending".equals(order.getStatus())) {
            System.out.println("Vendor: Completed processing for order " + orderNumber);

            MessageProducer replyProducer = asyncSession.createProducer(order.getMessage().getJMSReplyTo());
            MapMessage replyMessage = asyncSession.createMapMessage();
            if ("Fulfilled".equals(order.getStatus())) {
                replyMessage.setBoolean("OrderAccepted", true);
                System.out.println("Vendor: sent " + order.quantity + " computer(s)");
            } else {
                replyMessage.setBoolean("OrderAccepted", false);
                System.out.println("Vendor: unable to send " + order.quantity + " computer(s)");
            }
            replyProducer.send(replyMessage);
            asyncSession.commit();
            System.out.println("Vender: committed transaction 2");
        }
    } catch (JMSException e) {
        e.printStackTrace();
    }
}

From source file:Supplier.java

public void run() {
    ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(user, password, url);
    Session session = null;/*from   w w  w . j ava2s .com*/
    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;/*  w ww .j  a va  2 s .  c o  m*/
    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.jbpm.bpel.tutorial.purchase.ejb.InvoiceCallbackMessageBean.java

/**
 * Process the invoice callback message.
 *///from   w  w  w  . j  a va 2  s .co  m
public void onMessage(Message message) {
    if (!(message instanceof MapMessage)) {
        log.error("received non-map message: " + message);
        messageContext.setRollbackOnly();
        return;
    }
    try {
        // extract contents
        MapMessage invoiceMessage = (MapMessage) message;
        int orderId = invoiceMessage.getInt("orderId");
        float amount = invoiceMessage.getFloat("amount");

        // populate invoice with contents
        Invoice inv = new Invoice();
        inv.setOrderId(orderId);
        inv.setAmount(amount);

        // send invoice back to requester
        invoiceRequester.sendInvoice(inv);

        log.debug("sent invoice: orderId=" + orderId + ", amount=" + amount);
    } catch (JMSException e) {
        messageContext.setRollbackOnly();
        log.error("could not read invoice message", e);
    } catch (RemoteException e) {
        messageContext.setRollbackOnly();
        log.error("could not send invoice", e);
    }
}

From source file:org.smartfrog.avalanche.shared.jms.MessageListener.java

public MonitoringEvent receive() throws Exception {
    MonitoringEvent event = null;/*from ww  w  . j  a  v  a  2s . c  o m*/
    // TODO : no need to open a new session every time .. fix it
    QueueSession qs = null;
    QueueReceiver qr = null;
    try {
        qs = qc.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
        qr = qs.createReceiver(queue);
        // TODO : Fix message timeout
        log.info("MonitoringEvent.receive() : Checking for new message on Queue");
        MapMessage mm = (MapMessage) qr.receive(jmsTimeout);

        if (mm != null) {
            log.info("Message received");
            event = new MonitoringEventDefaultImpl();
            event.setHost(mm.getString(MonitoringEvent.HOST));
            event.setInstanceName(mm.getString(MonitoringEvent.INSTANCE_NAME));
            event.setModuleId(mm.getString(MonitoringEvent.MODULEID));
            event.setModuleState(mm.getString(MonitoringEvent.MODULE_STATE));
            event.setMsg(mm.getString(MonitoringEvent.MODULE_STATE));
            event.setMessageType(mm.getInt(MonitoringEvent.MESSAGE_TYPE));
            log.info("MessageListener.receive() - " + event);

        } else {
            log.info("No message found in queue");
        }
        return event;
    } finally {
        qr.close();
        qs.close();
    }
}

From source file:org.toobsframework.jms.email.JmsEmailReceiver.java

public EmailBean recieveMessage() throws JmsEmailException {
    Message msg = jmsTemplate.receive();
    if (msg == null) {
        return null;
    }//from  ww w .  j  av  a 2s. c o  m
    EmailBean bean = new EmailBean();
    MapMessage mapMessage = (MapMessage) msg;
    try {
        bean.setEmailSender(mapMessage.getString("sender"));
        bean.setEmailSubject(mapMessage.getString("subject"));
        bean.setRecipients(getRecipientList(mapMessage.getString("recipients")));
        bean.setMessageHtml(mapMessage.getString("messageHtml"));
        bean.setMessageText(mapMessage.getString("messageText"));
        bean.setMailSenderKey(mapMessage.getString("mailSenderKey"));
        bean.setAttempts(mapMessage.getInt("attempts"));
        bean.setType(mapMessage.getInt("type"));
        bean.setFailureCause(mapMessage.getString("failureCause"));
    } catch (JMSException e) {
        log.error("Exception getting email from queue: " + e.getMessage(), e);
        throw new JmsEmailException(e);
    }
    return bean;
}