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;
}
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;
}
});
}
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;
}
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;
}
Aggregations