Search in sources :

Example 1 with JmsBaseMessage

use of org.apache.rocketmq.jms.domain.message.JmsBaseMessage in project rocketmq-externals by apache.

the class JmsBaseMessageProducer method send.

/**
 * Send the message to the defined Destination success---return normally. Exception---throw out JMSException.
 *
 * @param destination see <CODE>Destination</CODE>
 * @param message the message to be sent.
 * @throws javax.jms.JMSException
 */
@Override
public void send(Destination destination, javax.jms.Message message) throws JMSException {
    JmsBaseMessage jmsMsg = (JmsBaseMessage) message;
    initJMSHeaders(jmsMsg, destination);
    try {
        if (context == null) {
            throw new IllegalStateException("Context should be inited");
        }
        org.apache.rocketmq.common.message.Message rocketmqMsg = MessageConverter.convert2RMQMessage(jmsMsg);
        MQProducer producer = producerMap.get(context.getProducerId());
        if (producer == null) {
            throw new Exception("producer is null ");
        }
        SendResult sendResult = producer.send(rocketmqMsg);
        if (sendResult != null && sendResult.getSendStatus() == SendStatus.SEND_OK) {
            jmsMsg.setHeader(JmsBaseConstant.JMS_MESSAGE_ID, "ID:" + sendResult.getMsgId());
        } else {
            throw new Exception("SendResult is " + (sendResult == null ? "null" : sendResult.toString()));
        }
    } catch (Exception e) {
        logger.error("Send rocketmq message failure !", e);
        // if fail to send the message, throw out JMSException
        JMSException jmsException = new JMSException("Send rocketmq message failure!");
        jmsException.setLinkedException(e);
        throw jmsException;
    }
}
Also used : MQProducer(org.apache.rocketmq.client.producer.MQProducer) DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer) JmsBaseMessage(org.apache.rocketmq.jms.domain.message.JmsBaseMessage) SendResult(org.apache.rocketmq.client.producer.SendResult) JMSException(javax.jms.JMSException) JMSException(javax.jms.JMSException) MQClientException(org.apache.rocketmq.client.exception.MQClientException)

Example 2 with JmsBaseMessage

use of org.apache.rocketmq.jms.domain.message.JmsBaseMessage in project rocketmq-externals by apache.

the class MessageConverter method convert2RMQMessage.

public static Message convert2RMQMessage(JmsBaseMessage jmsMsg) throws Exception {
    Message rocketmqMsg = new MessageExt();
    // 1. Transform message body
    rocketmqMsg.setBody(MessageConverter.getContentFromJms(jmsMsg));
    // 2. Transform topic and messageType
    JmsBaseTopic destination = (JmsBaseTopic) jmsMsg.getHeaders().get(JmsBaseConstant.JMS_DESTINATION);
    String topic = destination.getMessageTopic();
    rocketmqMsg.setTopic(topic);
    String messageType = destination.getMessageType();
    Preconditions.checkState(!messageType.contains("||"), "'||' can not be in the destination when sending a message");
    rocketmqMsg.setTags(messageType);
    // 3. Transform message properties
    Properties properties = initRocketMQHeaders(jmsMsg, topic, messageType);
    for (String name : properties.stringPropertyNames()) {
        String value = properties.getProperty(name);
        if (MessageConst.PROPERTY_KEYS.equals(name)) {
            rocketmqMsg.setKeys(value);
        } else if (MessageConst.PROPERTY_TAGS.equals(name)) {
            rocketmqMsg.setTags(value);
        } else if (MessageConst.PROPERTY_DELAY_TIME_LEVEL.equals(name)) {
            rocketmqMsg.setDelayTimeLevel(Integer.parseInt(value));
        } else if (MessageConst.PROPERTY_WAIT_STORE_MSG_OK.equals(name)) {
            rocketmqMsg.setWaitStoreMsgOK(Boolean.parseBoolean(value));
        } else if (MessageConst.PROPERTY_BUYER_ID.equals(name)) {
            rocketmqMsg.setBuyerId(value);
        } else {
            rocketmqMsg.putUserProperty(name, value);
        }
    }
    return rocketmqMsg;
}
Also used : MessageExt(org.apache.rocketmq.common.message.MessageExt) JmsObjectMessage(org.apache.rocketmq.jms.domain.message.JmsObjectMessage) JmsBytesMessage(org.apache.rocketmq.jms.domain.message.JmsBytesMessage) Message(org.apache.rocketmq.common.message.Message) JmsTextMessage(org.apache.rocketmq.jms.domain.message.JmsTextMessage) TextMessage(javax.jms.TextMessage) ObjectMessage(javax.jms.ObjectMessage) BytesMessage(javax.jms.BytesMessage) JmsBaseMessage(org.apache.rocketmq.jms.domain.message.JmsBaseMessage) JmsBaseTopic(org.apache.rocketmq.jms.domain.JmsBaseTopic) Properties(java.util.Properties)

Example 3 with JmsBaseMessage

use of org.apache.rocketmq.jms.domain.message.JmsBaseMessage in project rocketmq-externals by apache.

the class MessageConverter method convert2JMSMessage.

public static JmsBaseMessage convert2JMSMessage(MessageExt msg) throws Exception {
    JmsBaseMessage message;
    if (MsgConvertUtil.MSGMODEL_BYTES.equals(msg.getUserProperty(MsgConvertUtil.JMS_MSGMODEL))) {
        message = new JmsBytesMessage(msg.getBody());
    } else if (MsgConvertUtil.MSGMODEL_OBJ.equals(msg.getUserProperty(MsgConvertUtil.JMS_MSGMODEL))) {
        message = new JmsObjectMessage(MsgConvertUtil.objectDeserialize(msg.getBody()));
    } else if (MsgConvertUtil.MSGMODEL_TEXT.equals(msg.getUserProperty(MsgConvertUtil.JMS_MSGMODEL))) {
        message = new JmsTextMessage(MsgConvertUtil.bytes2String(msg.getBody(), Charsets.UTF_8.toString()));
    } else {
        // rocketmq producer sends bytesMessage without setting JMS_MSGMODEL.
        message = new JmsBytesMessage(msg.getBody());
    }
    // -------------------------set headers-------------------------
    Map<String, Object> properties = new HashMap<String, Object>();
    message.setHeader(JmsBaseConstant.JMS_MESSAGE_ID, "ID:" + msg.getMsgId());
    if (msg.getReconsumeTimes() > 0) {
        message.setHeader(JmsBaseConstant.JMS_REDELIVERED, Boolean.TRUE);
    } else {
        message.setHeader(JmsBaseConstant.JMS_REDELIVERED, Boolean.FALSE);
    }
    Map<String, String> propertiesMap = msg.getProperties();
    if (propertiesMap != null) {
        for (String properName : propertiesMap.keySet()) {
            String properValue = propertiesMap.get(properName);
            if (JmsBaseConstant.JMS_DESTINATION.equals(properName)) {
                String destinationStr = properValue;
                if (null != destinationStr) {
                    List<String> msgTuple = Arrays.asList(destinationStr.split(":"));
                    message.setHeader(JmsBaseConstant.JMS_DESTINATION, new JmsBaseTopic(msgTuple.get(0), msgTuple.get(1)));
                }
            } else if (JmsBaseConstant.JMS_DELIVERY_MODE.equals(properName) || JmsBaseConstant.JMS_PRIORITY.equals(properName)) {
                message.setHeader(properName, properValue);
            } else if (JmsBaseConstant.JMS_TIMESTAMP.equals(properName) || JmsBaseConstant.JMS_EXPIRATION.equals(properName)) {
                message.setHeader(properName, properValue);
            } else if (JmsBaseConstant.JMS_CORRELATION_ID.equals(properName) || JmsBaseConstant.JMS_TYPE.equals(properName)) {
                message.setHeader(properName, properValue);
            } else if (JmsBaseConstant.JMS_MESSAGE_ID.equals(properName) || JmsBaseConstant.JMS_REDELIVERED.equals(properName)) {
                // JMS_MESSAGE_ID should set by msg.getMsgID()
                continue;
            } else {
                properties.put(properName, properValue);
            }
        }
    }
    // Handle System properties, put into header.
    // add what?
    message.setProperties(properties);
    return message;
}
Also used : JmsBytesMessage(org.apache.rocketmq.jms.domain.message.JmsBytesMessage) JmsBaseMessage(org.apache.rocketmq.jms.domain.message.JmsBaseMessage) HashMap(java.util.HashMap) JmsBaseTopic(org.apache.rocketmq.jms.domain.JmsBaseTopic) JmsTextMessage(org.apache.rocketmq.jms.domain.message.JmsTextMessage) JmsObjectMessage(org.apache.rocketmq.jms.domain.message.JmsObjectMessage)

Aggregations

JmsBaseMessage (org.apache.rocketmq.jms.domain.message.JmsBaseMessage)3 JmsBaseTopic (org.apache.rocketmq.jms.domain.JmsBaseTopic)2 JmsBytesMessage (org.apache.rocketmq.jms.domain.message.JmsBytesMessage)2 JmsObjectMessage (org.apache.rocketmq.jms.domain.message.JmsObjectMessage)2 JmsTextMessage (org.apache.rocketmq.jms.domain.message.JmsTextMessage)2 HashMap (java.util.HashMap)1 Properties (java.util.Properties)1 BytesMessage (javax.jms.BytesMessage)1 JMSException (javax.jms.JMSException)1 ObjectMessage (javax.jms.ObjectMessage)1 TextMessage (javax.jms.TextMessage)1 MQClientException (org.apache.rocketmq.client.exception.MQClientException)1 DefaultMQProducer (org.apache.rocketmq.client.producer.DefaultMQProducer)1 MQProducer (org.apache.rocketmq.client.producer.MQProducer)1 SendResult (org.apache.rocketmq.client.producer.SendResult)1 Message (org.apache.rocketmq.common.message.Message)1 MessageExt (org.apache.rocketmq.common.message.MessageExt)1