Example usage for javax.jms MapMessage setInt

List of usage examples for javax.jms MapMessage setInt

Introduction

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

Prototype


void setInt(String name, int value) throws JMSException;

Source Link

Document

Sets an int value with the specified name into the Map.

Usage

From source file:Supplier.java

public void run() {
    ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(user, password, url);
    Session session = null;/*  w w w  .j  ava  2s .  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:Retailer.java

public void run() {
    ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(user, password, url);
    try {/*from w  ww  .jav a2 s .  c  o  m*/
        Connection connection = connectionFactory.createConnection();

        // The Retailer's session is non-trasacted.
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Destination vendorOrderQueue = session.createQueue("VendorOrderQueue");
        TemporaryQueue retailerConfirmQueue = session.createTemporaryQueue();

        MessageProducer producer = session.createProducer(vendorOrderQueue);
        MessageConsumer replyConsumer = session.createConsumer(retailerConfirmQueue);

        connection.start();

        for (int i = 0; i < 5; i++) {
            MapMessage message = session.createMapMessage();
            message.setString("Item", "Computer(s)");
            int quantity = (int) (Math.random() * 4) + 1;
            message.setInt("Quantity", quantity);
            message.setJMSReplyTo(retailerConfirmQueue);
            producer.send(message);
            System.out.println("Retailer: Ordered " + quantity + " computers.");

            MapMessage reply = (MapMessage) replyConsumer.receive();
            if (reply.getBoolean("OrderAccepted")) {
                System.out.println("Retailer: Order Filled");
            } else {
                System.out.println("Retailer: Order Not Filled");
            }
        }

        // Send a non-MapMessage to signal the end
        producer.send(session.createMessage());

        replyConsumer.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  www. j a v  a2  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.toobsframework.jms.email.JmsEmailSender.java

public void sendMessage(final EmailBean emailBean) {
    jmsTemplate.send(new MessageCreator() {
        public Message createMessage(Session session) throws JMSException {

            MapMessage mapMessage = null;
            try {
                mapMessage = session.createMapMessage();
                mapMessage.setString("sender", emailBean.getEmailSender());
                mapMessage.setString("subject", emailBean.getEmailSubject());
                mapMessage.setString("recipients", getRecipientList(emailBean.getRecipients()));
                mapMessage.setString("messageText", emailBean.getMessageText());
                mapMessage.setString("messageHtml", emailBean.getMessageHtml());
                mapMessage.setString("mailSenderKey", emailBean.getMailSenderKey());
                mapMessage.setInt("attempts", emailBean.getAttempts());
                mapMessage.setInt("type", emailBean.getType());
                mapMessage.setString("failureCause", emailBean.getFailureCause());
            } catch (Exception e) {
                log.error("JMS Mail exception " + e.getMessage(), e);
                throw new JMSException(e.getMessage());
            }//  w  w  w  .j a v a  2s  .  c  o  m
            return mapMessage;
        }
    });

}

From source file:com.espertech.esperio.jms.JMSDefaultMapMessageMarshaller.java

public Message marshal(EventBean eventBean, Session session, long timestamp) throws EPException {
    EventType eventType = eventBean.getEventType();
    MapMessage mapMessage = null;
    try {//ww w .  jav a2  s . c o  m
        mapMessage = session.createMapMessage();
        String[] properties = eventType.getPropertyNames();
        for (String property : properties) {
            if ((ExecutionPathDebugLog.isDebugEnabled) && (log.isDebugEnabled())) {
                log.debug(
                        ".Marshal EventProperty property==" + property + ", value=" + eventBean.get(property));
            }
            Class clazz = eventType.getPropertyType(property);
            if (JavaClassHelper.isNumeric(clazz)) {
                Class boxedClazz = JavaClassHelper.getBoxedType(clazz);
                if (boxedClazz == Double.class) {
                    mapMessage.setDouble(property, (Double) eventBean.get(property));
                }
                if (boxedClazz == Float.class) {
                    mapMessage.setFloat(property, (Float) eventBean.get(property));
                }
                if (boxedClazz == Byte.class) {
                    mapMessage.setFloat(property, (Byte) eventBean.get(property));
                }
                if (boxedClazz == Short.class) {
                    mapMessage.setShort(property, (Short) eventBean.get(property));
                }
                if (boxedClazz == Integer.class) {
                    mapMessage.setInt(property, (Integer) eventBean.get(property));
                }
                if (boxedClazz == Long.class) {
                    mapMessage.setLong(property, (Long) eventBean.get(property));
                }
            } else if ((clazz == boolean.class) || (clazz == Boolean.class)) {
                mapMessage.setBoolean(property, (Boolean) eventBean.get(property));
            } else if ((clazz == Character.class) || (clazz == char.class)) {
                mapMessage.setChar(property, (Character) eventBean.get(property));
            } else if (clazz == String.class) {
                mapMessage.setString(property, (String) eventBean.get(property));
            } else {
                mapMessage.setObject(property, eventBean.get(property));
            }
        }
        mapMessage.setJMSTimestamp(timestamp);
    } catch (JMSException ex) {
        throw new EPException(ex);
    }
    return mapMessage;
}

From source file:hermes.impl.DefaultXMLHelper.java

public Message createMessage(MessageFactory hermes, XMLMessage message)
        throws JMSException, IOException, ClassNotFoundException, DecoderException {
    try {//from  www. java2  s .c o m
        Message rval = hermes.createMessage();

        if (message instanceof XMLTextMessage) {
            rval = hermes.createTextMessage();

            XMLTextMessage textMessage = (XMLTextMessage) message;
            TextMessage textRval = (TextMessage) rval;

            if (BASE64_CODEC.equals(textMessage.getCodec())) {
                byte[] bytes = base64EncoderTL.get().decode(textMessage.getText().getBytes());
                textRval.setText(new String(bytes, "ASCII"));
            } else {
                textRval.setText(textMessage.getText());
            }
        } else if (message instanceof XMLMapMessage) {
            rval = hermes.createMapMessage();

            XMLMapMessage mapMessage = (XMLMapMessage) message;
            MapMessage mapRval = (MapMessage) rval;

            for (Iterator iter = mapMessage.getBodyProperty().iterator(); iter.hasNext();) {
                final Property property = (Property) iter.next();

                if (property.getValue() == null) {
                    mapRval.setObject(property.getName(), null);
                } else if (property.getType().equals(String.class.getName())) {
                    mapRval.setString(property.getName(), property.getValue());
                } else if (property.getType().equals(Long.class.getName())) {
                    mapRval.setLong(property.getName(), Long.parseLong(property.getValue()));
                } else if (property.getType().equals(Double.class.getName())) {
                    mapRval.setDouble(property.getName(), Double.parseDouble(property.getValue()));
                } else if (property.getType().equals(Boolean.class.getName())) {
                    mapRval.setBoolean(property.getName(), Boolean.getBoolean(property.getValue()));
                } else if (property.getType().equals(Character.class.getName())) {
                    mapRval.setChar(property.getName(), property.getValue().charAt(0));
                } else if (property.getType().equals(Short.class.getName())) {
                    mapRval.setShort(property.getName(), Short.parseShort(property.getValue()));
                } else if (property.getType().equals(Integer.class.getName())) {
                    mapRval.setInt(property.getName(), Integer.parseInt(property.getValue()));
                }
            }
        } else if (message instanceof XMLBytesMessage) {
            rval = hermes.createBytesMessage();

            XMLBytesMessage bytesMessage = (XMLBytesMessage) message;
            BytesMessage bytesRval = (BytesMessage) rval;

            bytesRval.writeBytes(base64EncoderTL.get().decode(bytesMessage.getBytes().getBytes()));
        } else if (message instanceof XMLObjectMessage) {
            rval = hermes.createObjectMessage();

            XMLObjectMessage objectMessage = (XMLObjectMessage) message;
            ObjectMessage objectRval = (ObjectMessage) rval;
            ByteArrayInputStream bistream = new ByteArrayInputStream(
                    base64EncoderTL.get().decode(objectMessage.getObject().getBytes()));

            ObjectInputStream oistream = new ObjectInputStream(bistream);

            objectRval.setObject((Serializable) oistream.readObject());
        }

        //
        // JMS Header properties

        try {
            rval.setJMSDeliveryMode(message.getJMSDeliveryMode());
        } catch (JMSException ex) {
            log.error("unable to set JMSDeliveryMode to " + message.getJMSDeliveryMode() + ": "
                    + ex.getMessage());
        }

        try {
            rval.setJMSMessageID(message.getJMSMessageID());
        } catch (JMSException ex) {
            log.error("unable to set JMSMessageID: " + ex.getMessage(), ex);
        }

        try {
            if (message.getJMSExpiration() != null) {
                rval.setJMSExpiration(message.getJMSExpiration());
            }
        } catch (JMSException ex) {
            log.error("unable to set JMSExpiration: " + ex.getMessage(), ex);
        }

        try {
            if (message.getJMSPriority() != null) {
                rval.setJMSPriority(message.getJMSPriority());
            }
        } catch (JMSException ex) {
            log.error("unable to set JMSPriority: " + ex.getMessage(), ex);
        }

        try {
            if (message.getJMSTimestamp() != null) {
                rval.setJMSTimestamp(message.getJMSTimestamp());
            }
        } catch (JMSException ex) {
            log.error("unable to set JMSTimestamp:" + ex.getMessage(), ex);
        }

        if (message.getJMSCorrelationID() != null) {
            rval.setJMSCorrelationID(message.getJMSCorrelationID());
        }

        if (message.getJMSReplyTo() != null && !message.getJMSReplyTo().equals("null")) {
            rval.setJMSReplyTo(hermes.getDestination(message.getJMSReplyTo(),
                    Domain.getDomain(message.getJMSReplyToDomain())));
        }

        if (message.getJMSType() != null) {
            rval.setJMSType(message.getJMSType());
        }

        if (message.getJMSDestination() != null) {
            if (message.isFromQueue()) {
                rval.setJMSDestination(hermes.getDestination(message.getJMSDestination(), Domain.QUEUE));
            } else {
                rval.setJMSDestination(hermes.getDestination(message.getJMSDestination(), Domain.TOPIC));
            }
        }

        for (Iterator iter = message.getHeaderProperty().iterator(); iter.hasNext();) {
            Property property = (Property) iter.next();

            if (property.getValue() == null) {
                rval.setObjectProperty(property.getName(), null);
            } else if (property.getType().equals(String.class.getName())) {
                rval.setStringProperty(property.getName(), StringEscapeUtils.unescapeXml(property.getValue()));
            } else if (property.getType().equals(Long.class.getName())) {
                rval.setLongProperty(property.getName(), Long.parseLong(property.getValue()));
            } else if (property.getType().equals(Double.class.getName())) {
                rval.setDoubleProperty(property.getName(), Double.parseDouble(property.getValue()));
            } else if (property.getType().equals(Boolean.class.getName())) {
                rval.setBooleanProperty(property.getName(), Boolean.parseBoolean(property.getValue()));
            } else if (property.getType().equals(Short.class.getName())) {
                rval.setShortProperty(property.getName(), Short.parseShort(property.getValue()));
            } else if (property.getType().equals(Integer.class.getName())) {
                rval.setIntProperty(property.getName(), Integer.parseInt(property.getValue()));
            }
        }

        return rval;
    } catch (NamingException e) {
        throw new HermesException(e);
    }

}

From source file:eu.domibus.submission.transformer.impl.JMSMessageTransformer.java

/**
 * Transforms {@link eu.domibus.submission.Submission} to {@link javax.jms.MapMessage}
 *
 * @param submission the message to be transformed     *
 * @return result of the transformation as {@link javax.jms.MapMessage}
 *//* w  w  w  .ja va2  s . c o m*/
@Override
public MapMessage transformFromSubmission(final Submission submission, final MapMessage messageOut) {

    try {
        messageOut.setStringProperty(JMSMessageTransformer.SUBMISSION_JMS_MAPMESSAGE_ACTION,
                submission.getAction());
        messageOut.setStringProperty(JMSMessageTransformer.SUBMISSION_JMS_MAPMESSAGE_SERVICE,
                submission.getService());
        messageOut.setStringProperty(JMSMessageTransformer.SUBMISSION_JMS_MAPMESSAGE_CONVERSATION_ID,
                submission.getConversationId());

        for (final Submission.Party fromParty : submission.getFromParties()) {
            messageOut.setStringProperty(JMSMessageTransformer.SUBMISSION_JMS_MAPMESSAGE_FROM_PARTY_ID,
                    fromParty.getPartyId());
            messageOut.setStringProperty(JMSMessageTransformer.SUBMISSION_JMS_MAPMESSAGE_FROM_PARTY_TYPE,
                    fromParty.getPartyIdType());
        }

        for (final Submission.Party toParty : submission.getToParties()) {
            messageOut.setStringProperty(JMSMessageTransformer.SUBMISSION_JMS_MAPMESSAGE_TO_PARTY_ID,
                    toParty.getPartyId());
            messageOut.setStringProperty(JMSMessageTransformer.SUBMISSION_JMS_MAPMESSAGE_TO_PARTY_TYPE,
                    toParty.getPartyIdType());
        }

        messageOut.setStringProperty(JMSMessageTransformer.SUBMISSION_JMS_MAPMESSAGE_FROM_ROLE,
                submission.getFromRole());
        messageOut.setStringProperty(JMSMessageTransformer.SUBMISSION_JMS_MAPMESSAGE_TO_ROLE,
                submission.getToRole());

        for (final Map.Entry p : submission.getMessageProperties().entrySet()) {
            if (p.getKey().equals(JMSMessageTransformer.SUBMISSION_JMS_MAPMESSAGE_PROPERTY_ORIGINAL_SENDER)) {
                messageOut.setStringProperty(
                        JMSMessageTransformer.SUBMISSION_JMS_MAPMESSAGE_PROPERTY_ORIGINAL_SENDER,
                        p.getValue().toString());
            }

            if (p.getKey().equals(JMSMessageTransformer.SUBMISSION_JMS_MAPMESSAGE_PROPERTY_ENDPOINT)) {
                messageOut.setStringProperty(JMSMessageTransformer.SUBMISSION_JMS_MAPMESSAGE_PROPERTY_ENDPOINT,
                        p.getValue().toString());
            }

            if (p.getKey().equals(JMSMessageTransformer.SUBMISSION_JMS_MAPMESSAGE_PROPERTY_FINAL_RECIPIENT)) {
                messageOut.setStringProperty(
                        JMSMessageTransformer.SUBMISSION_JMS_MAPMESSAGE_PROPERTY_FINAL_RECIPIENT,
                        p.getValue().toString());
            }
        }

        messageOut.setStringProperty(JMSMessageTransformer.SUBMISSION_JMS_MAPMESSAGE_PROTOCOL, "AS4");
        messageOut.setStringProperty(JMSMessageTransformer.SUBMISSION_JMS_MAPMESSAGE_AGREEMENT_REF,
                submission.getAgreementRef());
        messageOut.setStringProperty(JMSMessageTransformer.SUBMISSION_JMS_MAPMESSAGE_REF_TO_MESSAGE_ID,
                submission.getRefToMessageId());

        int counter = 2;

        for (final Submission.Payload p : submission.getPayloads()) {

            if (p.isInBody()) {
                messageOut.setBytes(
                        MessageFormat
                                .format(JMSMessageTransformer.SUBMISSION_JMS_MAPMESSAGE_PAYLOAD_NAME_FORMAT, 1),
                        p.getPayloadData());
                messageOut.setStringProperty(
                        MessageFormat.format(
                                JMSMessageTransformer.SUBMISSION_JMS_MAPMESSAGE_PAYLOAD_MIME_TYPE_FORMAT, 1),
                        p.getPayloadProperties().getProperty(Property.MIME_TYPE));
                messageOut.setStringProperty(MessageFormat.format(
                        JMSMessageTransformer.SUBMISSION_JMS_MAPMESSAGE_PAYLOAD_MIME_CONTENT_ID_FORMAT, 1),
                        p.getContentId());
                if (p.getDescription() != null) {
                    messageOut.setStringProperty(MessageFormat.format(
                            JMSMessageTransformer.SUBMISSION_JMS_MAPMESSAGE_PAYLOAD_DESCRIPTION_FORMAT, 1),
                            p.getDescription());
                }
            } else {

                final String payContID = String.valueOf(MessageFormat.format(
                        JMSMessageTransformer.SUBMISSION_JMS_MAPMESSAGE_PAYLOAD_MIME_CONTENT_ID_FORMAT,
                        counter));
                final String payDescrip = String.valueOf(MessageFormat.format(
                        JMSMessageTransformer.SUBMISSION_JMS_MAPMESSAGE_PAYLOAD_DESCRIPTION_FORMAT, counter));
                final String propPayload = String.valueOf(MessageFormat
                        .format(JMSMessageTransformer.SUBMISSION_JMS_MAPMESSAGE_PAYLOAD_NAME_FORMAT, counter));
                final String payMimeTypeProp = String.valueOf(MessageFormat.format(
                        JMSMessageTransformer.SUBMISSION_JMS_MAPMESSAGE_PAYLOAD_MIME_TYPE_FORMAT, counter));
                messageOut.setBytes(propPayload, p.getPayloadData());
                messageOut.setStringProperty(payMimeTypeProp,
                        p.getPayloadProperties().getProperty(Property.MIME_TYPE));
                messageOut.setStringProperty(payContID, p.getContentId());

                if (p.getDescription() != null) {
                    messageOut.setStringProperty(payDescrip, p.getDescription());
                }
                counter++;
            }
        }
        messageOut.setInt(JMSMessageTransformer.SUBMISSION_JMS_MAPMESSAGE_TOTAL_NUMBER_OF_PAYLOADS,
                submission.getPayloads().size());
    } catch (final JMSException ex) {
        JMSMessageTransformer.LOG.error("Error while filling the MapMessage", ex);
    }

    return messageOut;
}

From source file:org.jbpm.bpel.tutorial.invoice.ComputePricePT_Impl.java

protected void sendInvoiceMessage(float shippingPrice) throws JMSException {
    ServletContext servletContext = endpointContext.getServletContext();
    Integer orderId = (Integer) servletContext.getAttribute(ORDER_ID_ATTR);
    Float linePrice = (Float) servletContext.getAttribute(LINE_PRICE_ATTR);
    float amount = linePrice.floatValue() + shippingPrice;

    // create a session
    Session jmsSession = jmsConnection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
    try {//from  w  w w. jav  a 2s  . co  m
        // create the message
        MapMessage invoiceMessage = jmsSession.createMapMessage();
        invoiceMessage.setInt("orderId", orderId.intValue());
        invoiceMessage.setFloat("amount", amount);

        // send it!
        MessageProducer producer = jmsSession.createProducer(invoiceDestination);
        producer.send(invoiceMessage);

        log.debug("Sent invoice message: orderId=" + orderId + ", amount=" + amount);
    } finally {
        jmsSession.close();
    }
}