Example usage for javax.jms MapMessage setString

List of usage examples for javax.jms MapMessage setString

Introduction

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

Prototype


void setString(String name, String value) throws JMSException;

Source Link

Document

Sets a String value with the specified name into the Map.

Usage

From source file:org.atmars.service.impls.Jmail.java

public boolean checkLink(String email, String nickname, Date date, String encrypted)
        throws UnsupportedEncodingException {

    String plain = email + nickname + date.getTime();

    BasicPasswordEncryptor passwordEncryptor = new BasicPasswordEncryptor();
    if (!passwordEncryptor.checkPassword(plain, encrypted)) {
        return false;
    }//  www  . j av  a  2 s . c om
    Date now = new Date();
    if (now.getTime() - now.getTime() > 86400000) {
        return false;
    }
    final String subject = "Your New Account in AtMars Microblog has been Activated";
    final String content = "<div style='width:713px; background-color:#A1E0E9; padding-top:30px; padding-bottom:30px; margin: 0 auto;'><img src='"
            + serverURL + logo_img + "' width='160' /><img src='" + serverURL + poster_img
            + "' width='100%' /><div style='background-color:#FFFFFF; border-radius:3px; box-shadow:0 1px 3px rgba(0,0,0,0.25); border:1px solid #CCC; padding:20px 20px 20px 20px; margin-left:30px; margin-right:30px; margin-top:30px;'><p><b>Welcome to AtMars MicroBlog</b></p><p>Congratulations to you! Your email account "
            + email
            + " is activated. Thank you for registering AtMars MicroBlog.</p><p>Try to use AtMars to follow your friends:</p><p>&nbsp;&nbsp;&nbsp;&nbsp;<a href='"
            + serverURL + "'>" + serverURL
            + "</a></p><p>We are glad to meet you in AtMars MicroBlog.</p></div></div>";
    final String to = email;

    jmsTemplate.send(destination, new MessageCreator() {

        @Override
        public Message createMessage(Session session) throws JMSException {
            MapMessage message = session.createMapMessage();

            message.setString("To", to);
            message.setString("Subject", subject);
            message.setString("Content", content);

            return message;
        }

    });
    return true;
}

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());
            }//from ww  w.j  a v a 2s .c  o  m
            return mapMessage;
        }
    });

}

From source file:com.adaptris.core.jms.MapMessageTranslator.java

/**
 * <p>/*from w w  w.  j  a va 2  s .  c  o m*/
 * Translates an {@link com.adaptris.core.AdaptrisMessage} into a MapMessage.
 * </p>
 *
 * @param msg the <code>AdaptrisMessage</code> to translate
 * @return a new <code>MapMessage</code>
 * @throws JMSException
 */
public Message translate(AdaptrisMessage msg) throws JMSException {
    MapMessage jmsMsg = session.createMapMessage();
    jmsMsg.setString(getKeyForPayload(), msg.getContent());
    if (treatMetadataAsPartOfMessage()) {
        Set metadata = msg.getMetadata();
        for (Iterator itr = metadata.iterator(); itr.hasNext();) {
            MetadataElement element = (MetadataElement) itr.next();
            if (!isReserved(element.getKey())) {
                jmsMsg.setString(element.getKey(), element.getValue());
            }
        }
    }
    return helper.moveMetadata(msg, jmsMsg);

}

From source file:Supplier.java

public void run() {
    ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(user, password, url);
    Session session = null;/* w  w  w  . jav  a  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:com.datatorrent.lib.io.jms.JMSObjectInputOperatorTest.java

private void createMapMsgs(int numMessages) throws Exception {
    for (int i = 0; i < numMessages; i++) {
        MapMessage message = testMeta.session.createMapMessage();
        message.setString("userId", "abc" + i);
        message.setString("chatMessage", "hi" + i);
        testMeta.producer.send(message);
    }/*from ww  w .j  av a  2  s. c  o m*/
}

From source file:eu.domibus.submission.jms.BackendJMSImpl.java

private Boolean submitErrorMessage(String messageId) {
    Connection connection;/*from   w ww  . j a va  2s.  co m*/
    MessageProducer producer;
    List<ErrorLogEntry> errors = this.errorLogDao.getUnnotifiedErrorsForMessage(messageId);

    try {
        connection = this.cf.createConnection();
        final Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        producer = session.createProducer(this.receivingQueue);
        producer.setDeliveryMode(DeliveryMode.PERSISTENT);
        final MapMessage resMessage = session.createMapMessage();
        for (int i = 0; i < errors.size(); ++i) {
            resMessage.setString(String.valueOf(i), errors.get(i).toString());
            errors.get(i).setNotified(new Date());
            this.errorLogDao.update(errors.get(i));
        }

        producer.send(resMessage);
        producer.close();
        session.close();

        connection.close();
    } catch (JMSException e) {
        BackendJMSImpl.LOG.error("", e);
        return false;
    }
    return true;
}

From source file:eu.domibus.submission.jms.BackendJMSImpl.java

/**
 * This method is called when a message was received at the incoming queue
 *
 * @param message The incoming JMS Message
 *///www.  j ava2s .c  o  m
@Override
public void onMessage(final Message message) {
    final MapMessage map = (MapMessage) message;
    try {
        final Connection con = this.cf.createConnection();
        final Session session = con.createSession(false, Session.AUTO_ACKNOWLEDGE);
        final MapMessage res = session.createMapMessage();
        try {
            final String messageID = this.submit(map);
            res.setStringProperty("messageId", messageID);
        } catch (final TransformationException | ValidationException e) {
            BackendJMSImpl.LOG.error("Exception occurred: ", e);
            res.setString("ErrorMessage", e.getMessage());
        }

        res.setJMSCorrelationID(map.getJMSCorrelationID());
        final MessageProducer sender = session.createProducer(this.outQueue);
        sender.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
        sender.send(res);
        sender.close();
        session.close();
        con.close();

    } catch (final JMSException ex) {
        BackendJMSImpl.LOG.error("Error while sending response to queue", ex);

    }
}

From source file:Vendor.java

public void run() {
    ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(user, password, url);
    Session session = null;//  w  w w  .jav a 2s .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:hermes.impl.DefaultXMLHelper.java

public Message createMessage(MessageFactory hermes, XMLMessage message)
        throws JMSException, IOException, ClassNotFoundException, DecoderException {
    try {//from w w w . j a  v a2  s  .  c  om
        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: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 {/*from  ww w.  jav a 2  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;
}