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