Search in sources :

Example 1 with JmsObjectMessage

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

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

use of org.apache.rocketmq.jms.domain.message.JmsObjectMessage 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)3 JmsObjectMessage (org.apache.rocketmq.jms.domain.message.JmsObjectMessage)3 JmsTextMessage (org.apache.rocketmq.jms.domain.message.JmsTextMessage)3 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Properties (java.util.Properties)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 JMSException (javax.jms.JMSException)1 MessageProducer (javax.jms.MessageProducer)1 Session (javax.jms.Session)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