Example usage for javax.jms Message setJMSMessageID

List of usage examples for javax.jms Message setJMSMessageID

Introduction

In this page you can find the example usage for javax.jms Message setJMSMessageID.

Prototype


void setJMSMessageID(String id) throws JMSException;

Source Link

Document

Sets the message ID.

Usage

From source file:io.galeb.health.util.CallBackQueue.java

public void update(Target target) {
    String targetStr = new Gson().toJson(target, Target.class);
    jmsTemplate.send(HEALTH_CALLBACK_QUEUE, session -> {
        Message message = session.createObjectMessage(targetStr);
        String uniqueId = "ID:" + target.getName() + "-" + System.currentTimeMillis();
        message.setStringProperty("_HQ_DUPL_ID", uniqueId);
        message.setJMSMessageID(uniqueId);
        message.setStringProperty(HDR_DUPLICATE_DETECTION_ID.toString(), uniqueId);

        logger.info("JMSMessageID: " + uniqueId + " - Target " + target.getName());
        return message;
    });/*from  w w w  . j  a  v  a 2  s .  c om*/
}

From source file:jms.PublisherThread.java

private void publish() {
    long messageCount = jmsPublisher.getConfigs().getMessageCount();
    String publisherID = jmsPublisher.getConfigs().getId();

    log.info("Starting publisher to send " + messageCount + " messages. Publisher ID: " + publisherID);
    Message message = null;

    try {//  www  .ja v a2  s.  co  m
        for (int i = 1; i <= messageCount; i++) {

            message = jmsPublisher.createTextMessage(i + " Publisher: " + publisherID);
            message.setJMSMessageID(Integer.toString(i));
            jmsPublisher.send(message);

            if (log.isTraceEnabled()) {
                log.trace("message published: " + message);
            }
            sentCount.incrementAndGet();
            publishRate.mark();

        }

        log.info("Stopping publisher. [ Publisher ID: " + jmsPublisher.getConfigs().getId() + "  ]");
        jmsPublisher.close();
    } catch (JMSException e) {
        log.error("Exception occurred while publishing. " + "\n\tPublisher ID: " + publisherID + "\n\tMessage: "
                + message, e);
    }

    log.info("Stopped publisher. [ Publisher ID: " + jmsPublisher.getConfigs().getId() + "  ]");
}

From source file:jms.PublisherThread.java

private void transactionalPublish() {
    long messageCount = jmsPublisher.getConfigs().getMessageCount();
    String publisherID = jmsPublisher.getConfigs().getId();

    log.info("Starting transactional publisher to send " + messageCount + " messages. Publisher ID: "
            + publisherID);/*w ww.  j av  a 2s.co m*/
    Message message = null;
    int batchSize = jmsPublisher.getConfigs().getTransactionBatchSize();
    List<Message> currentBatch = new ArrayList<Message>(batchSize);

    for (int i = 1; i <= messageCount; i++) {
        try {
            message = jmsPublisher.createTextMessage(i + " Publisher: " + publisherID);
            message.setJMSMessageID(Integer.toString(i));
            jmsPublisher.send(message);
            currentBatch.add(message);

            if (log.isTraceEnabled()) {
                log.trace("message enqueued for transaction: " + message);
            }

            //logs the messages that are enqueued
            log.debug(((TextMessage) message).getText());

            if ((currentBatch.size() == batchSize) || (i == messageCount)) {

                jmsPublisher.commit();
                sentCount.addAndGet(currentBatch.size());
                publishRate.mark(currentBatch.size());
                currentBatch.clear();
            }
        } catch (JMSException e) {
            log.error("Exception occurred while transactional publishing", e);
            resend(currentBatch);
        }
    }

    log.info("Stopping transactional publisher. [ Publisher ID: " + jmsPublisher.getConfigs().getId() + "  ]");
    try {
        jmsPublisher.close();
    } catch (JMSException e) {
        log.error("Exception occurred while closing transactional publisher " + publisherID, e);
    }

    log.info("Stopped publisher. [ Publisher ID: " + jmsPublisher.getConfigs().getId() + "  ]");
}

From source file:hermes.store.jdbc.MessageResultSetHandler.java

public Message getNextMessage() throws JMSException, SQLException, NamingException {
    if (cachedMessages.size() > 0) {
        return cachedMessages.remove(0);
    } else {//w w  w  .j a v  a2  s  . c  o m
        if (resultSet.next()) {
            final Collection<Message> c = xmlHelper.fromXML(messageFactory,
                    resultSet.getClob(3).getAsciiStream());

            for (Message m : c) {
                if (headerPolicy != MessageStore.HeaderPolicy.NO_HEADER) {
                    Destination d = messageFactory.getDestination(resultSet.getString(1),
                            Domain.getDomain(resultSet.getInt(2)));

                    if (headerPolicy == MessageStore.HeaderPolicy.DESTINATION_ONLY) {
                        m.setJMSDestination(d);
                    } else if (headerPolicy == MessageStore.HeaderPolicy.MESSAGEID_ONLY) {
                        m.setJMSMessageID(resultSet.getString(4));
                    } else if (headerPolicy == MessageStore.HeaderPolicy.MESSAGEID_AND_DESTINATION) {
                        m.setJMSDestination(d);
                        m.setJMSMessageID(resultSet.getString(4));
                    }
                }

                cachedMessages.add(m);
            }

            if (cachedMessages.size() > 0) {
                return cachedMessages.remove(0);
            } else {
                return null;
            }
        } else {
            resultSetEmpty = true;
            close();
            return null;
        }
    }
}

From source file:com.amazon.sqs.javamessaging.SQSMessageProducer.java

void sendInternal(Queue queue, Message message) throws JMSException {
    checkClosed();//  w  w  w.  ja va2  s . c  om
    String sqsMessageBody = null;
    String messageType = null;
    if (message instanceof SQSMessage) {
        message.setJMSDestination(queue);
        if (message instanceof SQSBytesMessage) {
            sqsMessageBody = Base64.encodeAsString(((SQSBytesMessage) message).getBodyAsBytes());
            messageType = SQSMessage.BYTE_MESSAGE_TYPE;
        } else if (message instanceof SQSObjectMessage) {
            sqsMessageBody = ((SQSObjectMessage) message).getMessageBody();
            messageType = SQSMessage.OBJECT_MESSAGE_TYPE;
        } else if (message instanceof SQSTextMessage) {
            sqsMessageBody = ((SQSTextMessage) message).getText();
            messageType = SQSMessage.TEXT_MESSAGE_TYPE;
        }
    } else {
        throw new MessageFormatException(
                "Unrecognized message type. Messages have to be one of: SQSBytesMessage, SQSObjectMessage, or SQSTextMessage");
    }
    if (sqsMessageBody == null || sqsMessageBody.isEmpty()) {
        throw new JMSException("Message body cannot be null or empty");
    }
    Map<String, MessageAttributeValue> messageAttributes = propertyToMessageAttribute((SQSMessage) message);
    addMessageTypeReservedAttribute(messageAttributes, (SQSMessage) message, messageType);
    SendMessageRequest sendMessageRequest = new SendMessageRequest(((SQSQueueDestination) queue).getQueueUrl(),
            sqsMessageBody);
    sendMessageRequest.setMessageAttributes(messageAttributes);

    String messageId = amazonSQSClient.sendMessage(sendMessageRequest).getMessageId();
    LOG.info("Message sent to SQS with SQS-assigned messageId: " + messageId);
    /** TODO: Do not support disableMessageID for now.*/
    message.setJMSMessageID(String.format(SQSMessagingClientConstants.MESSAGE_ID_FORMAT, messageId));
    ((SQSMessage) message).setSQSMessageId(messageId);
}

From source file:hermes.impl.DefaultXMLHelper.java

public Message createMessage(MessageFactory hermes, XMLMessage message)
        throws JMSException, IOException, ClassNotFoundException, DecoderException {
    try {// ww  w  .j  av  a  2 s . com
        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:org.apache.axis2.transport.jms.JMSUtils.java

/**
 * Set transport headers from the axis message context, into the JMS message
 *
 * @param msgContext the axis message context
 * @param message the JMS Message//from   www  . j  av  a 2s. c om
 * @throws JMSException on exception
 */
public static void setTransportHeaders(MessageContext msgContext, Message message) throws JMSException {

    Map<?, ?> headerMap = (Map<?, ?>) msgContext.getProperty(MessageContext.TRANSPORT_HEADERS);

    if (headerMap == null) {
        return;
    }

    for (Object headerName : headerMap.keySet()) {

        String name = (String) headerName;

        if (name.startsWith(JMSConstants.JMSX_PREFIX)
                && !(name.equals(JMSConstants.JMSX_GROUP_ID) || name.equals(JMSConstants.JMSX_GROUP_SEQ))) {
            continue;
        }

        if (JMSConstants.JMS_COORELATION_ID.equals(name)) {
            message.setJMSCorrelationID((String) headerMap.get(JMSConstants.JMS_COORELATION_ID));
        } else if (JMSConstants.JMS_DELIVERY_MODE.equals(name)) {
            Object o = headerMap.get(JMSConstants.JMS_DELIVERY_MODE);
            if (o instanceof Integer) {
                message.setJMSDeliveryMode((Integer) o);
            } else if (o instanceof String) {
                try {
                    message.setJMSDeliveryMode(Integer.parseInt((String) o));
                } catch (NumberFormatException nfe) {
                    log.warn("Invalid delivery mode ignored : " + o, nfe);
                }
            } else {
                log.warn("Invalid delivery mode ignored : " + o);
            }

        } else if (JMSConstants.JMS_EXPIRATION.equals(name)) {
            message.setJMSExpiration(Long.parseLong((String) headerMap.get(JMSConstants.JMS_EXPIRATION)));
        } else if (JMSConstants.JMS_MESSAGE_ID.equals(name)) {
            message.setJMSMessageID((String) headerMap.get(JMSConstants.JMS_MESSAGE_ID));
        } else if (JMSConstants.JMS_PRIORITY.equals(name)) {
            message.setJMSPriority(Integer.parseInt((String) headerMap.get(JMSConstants.JMS_PRIORITY)));
        } else if (JMSConstants.JMS_TIMESTAMP.equals(name)) {
            message.setJMSTimestamp(Long.parseLong((String) headerMap.get(JMSConstants.JMS_TIMESTAMP)));
        } else if (JMSConstants.JMS_MESSAGE_TYPE.equals(name)) {
            message.setJMSType((String) headerMap.get(JMSConstants.JMS_MESSAGE_TYPE));

        } else {
            Object value = headerMap.get(name);
            if (value instanceof String) {
                message.setStringProperty(name, (String) value);
            } else if (value instanceof Boolean) {
                message.setBooleanProperty(name, (Boolean) value);
            } else if (value instanceof Integer) {
                message.setIntProperty(name, (Integer) value);
            } else if (value instanceof Long) {
                message.setLongProperty(name, (Long) value);
            } else if (value instanceof Double) {
                message.setDoubleProperty(name, (Double) value);
            } else if (value instanceof Float) {
                message.setFloatProperty(name, (Float) value);
            }
        }
    }
}

From source file:org.apache.synapse.transport.jms.JMSUtils.java

/**
 * Set transport headers from the axis message context, into the JMS message
 *
 * @param msgContext the axis message context
 * @param message the JMS Message/*  w w  w.j av a  2  s  . co m*/
 * @throws JMSException on exception
 */
public static void setTransportHeaders(MessageContext msgContext, Message message) throws JMSException {

    Map headerMap = (Map) msgContext.getProperty(MessageContext.TRANSPORT_HEADERS);

    if (headerMap == null) {
        return;
    }

    Iterator iter = headerMap.keySet().iterator();
    while (iter.hasNext()) {

        String name = (String) iter.next();

        if (JMSConstants.JMS_COORELATION_ID.equals(name)) {
            message.setJMSCorrelationID((String) headerMap.get(JMSConstants.JMS_COORELATION_ID));
        } else if (JMSConstants.JMS_DELIVERY_MODE.equals(name)) {
            Object o = headerMap.get(JMSConstants.JMS_DELIVERY_MODE);
            if (o instanceof Integer) {
                message.setJMSDeliveryMode(((Integer) o).intValue());
            } else if (o instanceof String) {
                try {
                    message.setJMSDeliveryMode(Integer.parseInt((String) o));
                } catch (NumberFormatException nfe) {
                    log.warn("Invalid delivery mode ignored : " + o, nfe);
                }
            } else {
                log.warn("Invalid delivery mode ignored : " + o);
            }
        } else if (JMSConstants.JMS_EXPIRATION.equals(name)) {
            message.setJMSExpiration(Long.parseLong((String) headerMap.get(JMSConstants.JMS_EXPIRATION)));
        } else if (JMSConstants.JMS_MESSAGE_ID.equals(name)) {
            message.setJMSMessageID((String) headerMap.get(JMSConstants.JMS_MESSAGE_ID));
        } else if (JMSConstants.JMS_PRIORITY.equals(name)) {
            message.setJMSPriority(Integer.parseInt((String) headerMap.get(JMSConstants.JMS_PRIORITY)));
        } else if (JMSConstants.JMS_TIMESTAMP.equals(name)) {
            message.setJMSTimestamp(Long.parseLong((String) headerMap.get(JMSConstants.JMS_TIMESTAMP)));
        } else if (JMSConstants.JMS_MESSAGE_TYPE.equals(name)) {
            message.setJMSType((String) headerMap.get(JMSConstants.JMS_MESSAGE_TYPE));
        } else {
            Object value = headerMap.get(name);
            if (value instanceof String) {
                message.setStringProperty(name, (String) value);
            } else if (value instanceof Boolean) {
                message.setBooleanProperty(name, ((Boolean) value).booleanValue());
            } else if (value instanceof Integer) {
                message.setIntProperty(name, ((Integer) value).intValue());
            } else if (value instanceof Long) {
                message.setLongProperty(name, ((Long) value).longValue());
            } else if (value instanceof Double) {
                message.setDoubleProperty(name, ((Double) value).doubleValue());
            } else if (value instanceof Float) {
                message.setFloatProperty(name, ((Float) value).floatValue());
            }
        }
    }
}

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

public static Message copyJMSProperties(Message from, Message to, JmsConnector connector) throws JMSException {
    if (connector.supportsProperty(JmsConstants.JMS_CORRELATION_ID)) {
        to.setJMSCorrelationID(from.getJMSCorrelationID());
    }/*from   w  w  w. j  av  a  2  s .c om*/
    if (connector.supportsProperty(JmsConstants.JMS_DELIVERY_MODE)) {
        to.setJMSDeliveryMode(from.getJMSDeliveryMode());
    }
    if (connector.supportsProperty(JmsConstants.JMS_DESTINATION)) {
        to.setJMSDestination(from.getJMSDestination());
    }
    if (connector.supportsProperty(JmsConstants.JMS_EXPIRATION)) {
        to.setJMSExpiration(from.getJMSExpiration());
    }
    if (connector.supportsProperty(JmsConstants.JMS_MESSAGE_ID)) {
        to.setJMSMessageID(from.getJMSMessageID());
    }
    if (connector.supportsProperty(JmsConstants.JMS_PRIORITY)) {
        to.setJMSPriority(from.getJMSPriority());
    }
    if (connector.supportsProperty(JmsConstants.JMS_REDELIVERED)) {
        to.setJMSRedelivered(from.getJMSRedelivered());
    }
    if (connector.supportsProperty(JmsConstants.JMS_REPLY_TO)) {
        to.setJMSReplyTo(from.getJMSReplyTo());
    }
    if (connector.supportsProperty(JmsConstants.JMS_TIMESTAMP)) {
        to.setJMSTimestamp(from.getJMSTimestamp());
    }
    if (connector.supportsProperty(JmsConstants.JMS_TYPE)) {
        to.setJMSType(from.getJMSType());
    }
    return to;
}

From source file:org.wso2.carbon.apimgt.jms.listener.utils.JMSUtils.java

/**
 * Set transport headers from the axis message context, into the JMS message
 *
 * @param messageConfiguration the adaptor message context
 * @param message              the JMS Message
 * @throws JMSException on exception/*from w  ww  .  j  a va2 s . c  o  m*/
 */
public static void setTransportHeaders(Map<String, String> messageConfiguration, Message message)
        throws JMSException {

    if (messageConfiguration == null) {
        return;
    }

    for (String name : messageConfiguration.keySet()) {

        if (name.startsWith(JMSConstants.JMSX_PREFIX)
                && !(name.equals(JMSConstants.JMSX_GROUP_ID) || name.equals(JMSConstants.JMSX_GROUP_SEQ))) {
            continue;
        }

        if (JMSConstants.JMS_COORELATION_ID.equals(name)) {
            message.setJMSCorrelationID(messageConfiguration.get(JMSConstants.JMS_COORELATION_ID));
        } else if (JMSConstants.JMS_DELIVERY_MODE.equals(name)) {
            String mode = messageConfiguration.get(JMSConstants.JMS_DELIVERY_MODE);
            try {
                message.setJMSDeliveryMode(Integer.parseInt(mode));
            } catch (NumberFormatException nfe) {
                log.warn("Invalid delivery mode ignored : " + mode, nfe);
            }

        } else if (JMSConstants.JMS_EXPIRATION.equals(name)) {
            message.setJMSExpiration(Long.parseLong(messageConfiguration.get(JMSConstants.JMS_EXPIRATION)));
        } else if (JMSConstants.JMS_MESSAGE_ID.equals(name)) {
            message.setJMSMessageID(messageConfiguration.get(JMSConstants.JMS_MESSAGE_ID));
        } else if (JMSConstants.JMS_PRIORITY.equals(name)) {
            message.setJMSPriority(Integer.parseInt(messageConfiguration.get(JMSConstants.JMS_PRIORITY)));
        } else if (JMSConstants.JMS_TIMESTAMP.equals(name)) {
            message.setJMSTimestamp(Long.parseLong(messageConfiguration.get(JMSConstants.JMS_TIMESTAMP)));
        } else if (JMSConstants.JMS_MESSAGE_TYPE.equals(name)) {
            message.setJMSType(messageConfiguration.get(JMSConstants.JMS_MESSAGE_TYPE));

        } else {

            //TODO currently only string is supported  in messageConfiguration
            Object value = messageConfiguration.get(name);
            if (value instanceof String) {
                message.setStringProperty(name, (String) value);
            } else if (value instanceof Boolean) {
                message.setBooleanProperty(name, (Boolean) value);
            } else if (value instanceof Integer) {
                message.setIntProperty(name, (Integer) value);
            } else if (value instanceof Long) {
                message.setLongProperty(name, (Long) value);
            } else if (value instanceof Double) {
                message.setDoubleProperty(name, (Double) value);
            } else if (value instanceof Float) {
                message.setFloatProperty(name, (Float) value);
            }
        }
    }
}