Search in sources :

Example 1 with JmsBytesMessage

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

the class MessageConverter method getContentFromJms.

public static byte[] getContentFromJms(javax.jms.Message jmsMessage) throws Exception {
    byte[] content;
    if (jmsMessage instanceof TextMessage) {
        if (StringUtils.isEmpty(((TextMessage) jmsMessage).getText())) {
            throw new IllegalArgumentException("Message body length is zero");
        }
        content = MsgConvertUtil.string2Bytes(((TextMessage) jmsMessage).getText(), Charsets.UTF_8.toString());
    } else if (jmsMessage instanceof ObjectMessage) {
        if (((ObjectMessage) jmsMessage).getObject() == null) {
            throw new IllegalArgumentException("Message body length is zero");
        }
        content = MsgConvertUtil.objectSerialize(((ObjectMessage) jmsMessage).getObject());
    } else if (jmsMessage instanceof BytesMessage) {
        JmsBytesMessage bytesMessage = (JmsBytesMessage) jmsMessage;
        if (bytesMessage.getBodyLength() == 0) {
            throw new IllegalArgumentException("Message body length is zero");
        }
        content = bytesMessage.getData();
    } else {
        throw new IllegalArgumentException("Unknown message type " + jmsMessage.getJMSType());
    }
    return content;
}
Also used : JmsBytesMessage(org.apache.rocketmq.jms.domain.message.JmsBytesMessage) JmsObjectMessage(org.apache.rocketmq.jms.domain.message.JmsObjectMessage) ObjectMessage(javax.jms.ObjectMessage) JmsBytesMessage(org.apache.rocketmq.jms.domain.message.JmsBytesMessage) BytesMessage(javax.jms.BytesMessage) JmsTextMessage(org.apache.rocketmq.jms.domain.message.JmsTextMessage) TextMessage(javax.jms.TextMessage)

Example 2 with JmsBytesMessage

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

the class JmsProduceIT method simpleSendTest.

@Test
public void simpleSendTest() throws Exception {
    // Send text message
    jmsTemplate.execute(destination, new ProducerCallback() {

        @Override
        public Object doInJms(Session session, MessageProducer producer) throws JMSException {
            JmsTextMessage message = (JmsTextMessage) session.createTextMessage("hello world,kafka");
            producer.send(destination, message);
            Assert.assertNotNull(message.getJMSMessageID());
            return message;
        }
    });
    // Send object message
    jmsTemplate.execute(destination, new ProducerCallback() {

        @Override
        public Object doInJms(Session session, MessageProducer producer) throws JMSException {
            JmsObjectMessage message = (JmsObjectMessage) session.createObjectMessage(Lists.newArrayList(1, 2, 3));
            producer.send(destination, message);
            Assert.assertNotNull(message.getJMSMessageID());
            return message;
        }
    });
    // Send byte message
    jmsTemplate.execute(destination, new ProducerCallback() {

        @Override
        public Object doInJms(Session session, MessageProducer producer) throws JMSException {
            byte[] ts = "Von,Test".getBytes();
            JmsBytesMessage message = (JmsBytesMessage) session.createBytesMessage();
            message.writeBytes(ts);
            producer.send(destination, message);
            Assert.assertNotNull(message.getJMSMessageID());
            return message;
        }
    });
}
Also used : JmsBytesMessage(org.apache.rocketmq.jms.domain.message.JmsBytesMessage) ProducerCallback(org.springframework.jms.core.ProducerCallback) JMSException(javax.jms.JMSException) MessageProducer(javax.jms.MessageProducer) JmsTextMessage(org.apache.rocketmq.jms.domain.message.JmsTextMessage) JmsObjectMessage(org.apache.rocketmq.jms.domain.message.JmsObjectMessage) Session(javax.jms.Session) Test(org.testng.annotations.Test)

Example 3 with JmsBytesMessage

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

the class JmsBaseMessageProducer method initRocketMQHeaders.

/**
 * Init the OnsMessage Headers.
 * <p/>
 * <P>When converting JmsMessage to OnsMessage, should read from the JmsMessage's Properties and write to the
 * OnsMessage's Properties.
 *
 * @param jmsMsg message
 * @throws javax.jms.JMSException
 */
public static Properties initRocketMQHeaders(JmsBaseMessage jmsMsg, String topic, String messageType) throws JMSException {
    Properties userProperties = new Properties();
    // Jms userProperties to properties
    Map<String, Object> userProps = jmsMsg.getProperties();
    Iterator<Map.Entry<String, Object>> userPropsIter = userProps.entrySet().iterator();
    while (userPropsIter.hasNext()) {
        Map.Entry<String, Object> entry = userPropsIter.next();
        userProperties.setProperty(entry.getKey(), entry.getValue().toString());
    }
    // Jms systemProperties to ROCKETMQ properties
    Map<String, Object> sysProps = jmsMsg.getHeaders();
    Iterator<Map.Entry<String, Object>> sysPropsIter = sysProps.entrySet().iterator();
    while (sysPropsIter.hasNext()) {
        Map.Entry<String, Object> entry = sysPropsIter.next();
        userProperties.setProperty(entry.getKey(), entry.getValue().toString());
    }
    // Jms message Model
    if (jmsMsg instanceof JmsBytesMessage) {
        userProperties.setProperty(MsgConvertUtil.JMS_MSGMODEL, MsgConvertUtil.MSGMODEL_BYTES);
    } else if (jmsMsg instanceof JmsObjectMessage) {
        userProperties.setProperty(MsgConvertUtil.JMS_MSGMODEL, MsgConvertUtil.MSGMODEL_OBJ);
    } else if (jmsMsg instanceof JmsTextMessage) {
        userProperties.setProperty(MsgConvertUtil.JMS_MSGMODEL, MsgConvertUtil.MSGMODEL_TEXT);
    }
    // message topic and tag
    userProperties.setProperty(MsgConvertUtil.MSG_TOPIC, topic);
    userProperties.setProperty(MsgConvertUtil.MSG_TYPE, messageType);
    return userProperties;
}
Also used : JmsBytesMessage(org.apache.rocketmq.jms.domain.message.JmsBytesMessage) Properties(java.util.Properties) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) JmsTextMessage(org.apache.rocketmq.jms.domain.message.JmsTextMessage) JmsObjectMessage(org.apache.rocketmq.jms.domain.message.JmsObjectMessage)

Example 4 with JmsBytesMessage

use of org.apache.rocketmq.jms.domain.message.JmsBytesMessage 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

JmsBytesMessage (org.apache.rocketmq.jms.domain.message.JmsBytesMessage)4 JmsObjectMessage (org.apache.rocketmq.jms.domain.message.JmsObjectMessage)4 JmsTextMessage (org.apache.rocketmq.jms.domain.message.JmsTextMessage)4 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Properties (java.util.Properties)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 BytesMessage (javax.jms.BytesMessage)1 JMSException (javax.jms.JMSException)1 MessageProducer (javax.jms.MessageProducer)1 ObjectMessage (javax.jms.ObjectMessage)1 Session (javax.jms.Session)1 TextMessage (javax.jms.TextMessage)1 JmsBaseTopic (org.apache.rocketmq.jms.domain.JmsBaseTopic)1 JmsBaseMessage (org.apache.rocketmq.jms.domain.message.JmsBaseMessage)1 ProducerCallback (org.springframework.jms.core.ProducerCallback)1 Test (org.testng.annotations.Test)1