@Test public void testPayload() { String charset = "UTF-8"; String destination = "test-topic"; Message msgWithStringPayload = MessageBuilder.withPayload("test").build(); org.apache.rocketmq.common.message.Message rocketMsg1 = RocketMQUtil.convertToRocketMessage(objectMapper, charset, destination, msgWithStringPayload); Message msgWithBytePayload = MessageBuilder.withPayload("test".getBytes()).build(); org.apache.rocketmq.common.message.Message rocketMsg2 = RocketMQUtil.convertToRocketMessage(objectMapper, charset, destination, msgWithBytePayload); assertTrue(Arrays.equals(((String)msgWithStringPayload.getPayload()).getBytes(), rocketMsg1.getBody())); assertTrue(Arrays.equals((byte[])msgWithBytePayload.getPayload(), rocketMsg2.getBody())); }
public static org.apache.rocketmq.common.message.Message convertToRocketMessage( ObjectMapper objectMapper, String charset, String destination, org.springframework.messaging.Message<?> message) { Object payloadObj = message.getPayload(); byte[] payloads; if (payloadObj instanceof String) { payloads = ((String) payloadObj).getBytes(Charset.forName(charset)); } else if (payloadObj instanceof byte[]) { payloads = (byte[]) message.getPayload(); } else { try { String jsonObj = objectMapper.writeValueAsString(payloadObj); payloads = jsonObj.getBytes(Charset.forName(charset)); } catch (Exception e) { throw new RuntimeException("convert to RocketMQ message failed.", e); } } String[] tempArr = destination.split(":", 2); String topic = tempArr[0]; String tags = ""; if (tempArr.length > 1) { tags = tempArr[1]; } org.apache.rocketmq.common.message.Message rocketMsg = new org.apache.rocketmq.common.message.Message(topic, tags, payloads); MessageHeaders headers = message.getHeaders(); if (Objects.nonNull(headers) && !headers.isEmpty()) { Object keys = headers.get(RocketMQHeaders.KEYS); if (!StringUtils.isEmpty(keys)) { rocketMsg.setKeys(keys.toString()); } Object flagObj = headers.getOrDefault("FLAG", "0"); int flag = 0; try { flag = Integer.parseInt(flagObj.toString()); } catch (NumberFormatException e) { log.info("flag must be integer, flagObj:{}", flagObj); } rocketMsg.setFlag(flag); Object waitStoreMsgOkObj = headers.getOrDefault("WAIT_STORE_MSG_OK", "true"); boolean waitStoreMsgOK = Boolean.TRUE.equals(waitStoreMsgOkObj); rocketMsg.setWaitStoreMsgOK(waitStoreMsgOK); headers.entrySet().stream() .filter(entry -> !Objects.equals(entry.getKey(), "FLAG") && !Objects.equals(entry.getKey(), "WAIT_STORE_MSG_OK")) .forEach(entry -> { if (!MessageConst.STRING_HASH_SET.contains(entry.getKey())) { rocketMsg.putUserProperty(entry.getKey(), String.valueOf(entry.getValue())); } }); } return rocketMsg; }
RocketMQUtil { public static org.apache.rocketmq.common.message.Message convertToRocketMessage( ObjectMapper objectMapper, String charset, String destination, org.springframework.messaging.Message<?> message) { Object payloadObj = message.getPayload(); byte[] payloads; if (payloadObj instanceof String) { payloads = ((String) payloadObj).getBytes(Charset.forName(charset)); } else if (payloadObj instanceof byte[]) { payloads = (byte[]) message.getPayload(); } else { try { String jsonObj = objectMapper.writeValueAsString(payloadObj); payloads = jsonObj.getBytes(Charset.forName(charset)); } catch (Exception e) { throw new RuntimeException("convert to RocketMQ message failed.", e); } } String[] tempArr = destination.split(":", 2); String topic = tempArr[0]; String tags = ""; if (tempArr.length > 1) { tags = tempArr[1]; } org.apache.rocketmq.common.message.Message rocketMsg = new org.apache.rocketmq.common.message.Message(topic, tags, payloads); MessageHeaders headers = message.getHeaders(); if (Objects.nonNull(headers) && !headers.isEmpty()) { Object keys = headers.get(RocketMQHeaders.KEYS); if (!StringUtils.isEmpty(keys)) { rocketMsg.setKeys(keys.toString()); } Object flagObj = headers.getOrDefault("FLAG", "0"); int flag = 0; try { flag = Integer.parseInt(flagObj.toString()); } catch (NumberFormatException e) { log.info("flag must be integer, flagObj:{}", flagObj); } rocketMsg.setFlag(flag); Object waitStoreMsgOkObj = headers.getOrDefault("WAIT_STORE_MSG_OK", "true"); boolean waitStoreMsgOK = Boolean.TRUE.equals(waitStoreMsgOkObj); rocketMsg.setWaitStoreMsgOK(waitStoreMsgOK); headers.entrySet().stream() .filter(entry -> !Objects.equals(entry.getKey(), "FLAG") && !Objects.equals(entry.getKey(), "WAIT_STORE_MSG_OK")) .forEach(entry -> { if (!MessageConst.STRING_HASH_SET.contains(entry.getKey())) { rocketMsg.putUserProperty(entry.getKey(), String.valueOf(entry.getValue())); } }); } return rocketMsg; } }
RocketMQUtil { public static org.apache.rocketmq.common.message.Message convertToRocketMessage( ObjectMapper objectMapper, String charset, String destination, org.springframework.messaging.Message<?> message) { Object payloadObj = message.getPayload(); byte[] payloads; if (payloadObj instanceof String) { payloads = ((String) payloadObj).getBytes(Charset.forName(charset)); } else if (payloadObj instanceof byte[]) { payloads = (byte[]) message.getPayload(); } else { try { String jsonObj = objectMapper.writeValueAsString(payloadObj); payloads = jsonObj.getBytes(Charset.forName(charset)); } catch (Exception e) { throw new RuntimeException("convert to RocketMQ message failed.", e); } } String[] tempArr = destination.split(":", 2); String topic = tempArr[0]; String tags = ""; if (tempArr.length > 1) { tags = tempArr[1]; } org.apache.rocketmq.common.message.Message rocketMsg = new org.apache.rocketmq.common.message.Message(topic, tags, payloads); MessageHeaders headers = message.getHeaders(); if (Objects.nonNull(headers) && !headers.isEmpty()) { Object keys = headers.get(RocketMQHeaders.KEYS); if (!StringUtils.isEmpty(keys)) { rocketMsg.setKeys(keys.toString()); } Object flagObj = headers.getOrDefault("FLAG", "0"); int flag = 0; try { flag = Integer.parseInt(flagObj.toString()); } catch (NumberFormatException e) { log.info("flag must be integer, flagObj:{}", flagObj); } rocketMsg.setFlag(flag); Object waitStoreMsgOkObj = headers.getOrDefault("WAIT_STORE_MSG_OK", "true"); boolean waitStoreMsgOK = Boolean.TRUE.equals(waitStoreMsgOkObj); rocketMsg.setWaitStoreMsgOK(waitStoreMsgOK); headers.entrySet().stream() .filter(entry -> !Objects.equals(entry.getKey(), "FLAG") && !Objects.equals(entry.getKey(), "WAIT_STORE_MSG_OK")) .forEach(entry -> { if (!MessageConst.STRING_HASH_SET.contains(entry.getKey())) { rocketMsg.putUserProperty(entry.getKey(), String.valueOf(entry.getValue())); } }); } return rocketMsg; }  }
RocketMQUtil { public static org.apache.rocketmq.common.message.Message convertToRocketMessage( ObjectMapper objectMapper, String charset, String destination, org.springframework.messaging.Message<?> message) { Object payloadObj = message.getPayload(); byte[] payloads; if (payloadObj instanceof String) { payloads = ((String) payloadObj).getBytes(Charset.forName(charset)); } else if (payloadObj instanceof byte[]) { payloads = (byte[]) message.getPayload(); } else { try { String jsonObj = objectMapper.writeValueAsString(payloadObj); payloads = jsonObj.getBytes(Charset.forName(charset)); } catch (Exception e) { throw new RuntimeException("convert to RocketMQ message failed.", e); } } String[] tempArr = destination.split(":", 2); String topic = tempArr[0]; String tags = ""; if (tempArr.length > 1) { tags = tempArr[1]; } org.apache.rocketmq.common.message.Message rocketMsg = new org.apache.rocketmq.common.message.Message(topic, tags, payloads); MessageHeaders headers = message.getHeaders(); if (Objects.nonNull(headers) && !headers.isEmpty()) { Object keys = headers.get(RocketMQHeaders.KEYS); if (!StringUtils.isEmpty(keys)) { rocketMsg.setKeys(keys.toString()); } Object flagObj = headers.getOrDefault("FLAG", "0"); int flag = 0; try { flag = Integer.parseInt(flagObj.toString()); } catch (NumberFormatException e) { log.info("flag must be integer, flagObj:{}", flagObj); } rocketMsg.setFlag(flag); Object waitStoreMsgOkObj = headers.getOrDefault("WAIT_STORE_MSG_OK", "true"); boolean waitStoreMsgOK = Boolean.TRUE.equals(waitStoreMsgOkObj); rocketMsg.setWaitStoreMsgOK(waitStoreMsgOK); headers.entrySet().stream() .filter(entry -> !Objects.equals(entry.getKey(), "FLAG") && !Objects.equals(entry.getKey(), "WAIT_STORE_MSG_OK")) .forEach(entry -> { if (!MessageConst.STRING_HASH_SET.contains(entry.getKey())) { rocketMsg.putUserProperty(entry.getKey(), String.valueOf(entry.getValue())); } }); } return rocketMsg; }  static TransactionListener convert(RocketMQLocalTransactionListener listener); static MessagingException convert(MQClientException e); static org.springframework.messaging.Message convertToSpringMessage(
        org.apache.rocketmq.common.message.MessageExt message); static String toRocketHeaderKey(String rawKey); static org.springframework.messaging.Message convertToSpringMessage(
        org.apache.rocketmq.common.message.Message message); static org.apache.rocketmq.common.message.Message convertToRocketMessage(
            ObjectMapper objectMapper, String charset,
            String destination, org.springframework.messaging.Message<?> message); static RPCHook getRPCHookByAkSk(Environment env, String accessKeyOrExpr, String secretKeyOrExpr); static String getInstanceName(RPCHook rpcHook, String identify); }
RocketMQUtil { public static org.apache.rocketmq.common.message.Message convertToRocketMessage( ObjectMapper objectMapper, String charset, String destination, org.springframework.messaging.Message<?> message) { Object payloadObj = message.getPayload(); byte[] payloads; if (payloadObj instanceof String) { payloads = ((String) payloadObj).getBytes(Charset.forName(charset)); } else if (payloadObj instanceof byte[]) { payloads = (byte[]) message.getPayload(); } else { try { String jsonObj = objectMapper.writeValueAsString(payloadObj); payloads = jsonObj.getBytes(Charset.forName(charset)); } catch (Exception e) { throw new RuntimeException("convert to RocketMQ message failed.", e); } } String[] tempArr = destination.split(":", 2); String topic = tempArr[0]; String tags = ""; if (tempArr.length > 1) { tags = tempArr[1]; } org.apache.rocketmq.common.message.Message rocketMsg = new org.apache.rocketmq.common.message.Message(topic, tags, payloads); MessageHeaders headers = message.getHeaders(); if (Objects.nonNull(headers) && !headers.isEmpty()) { Object keys = headers.get(RocketMQHeaders.KEYS); if (!StringUtils.isEmpty(keys)) { rocketMsg.setKeys(keys.toString()); } Object flagObj = headers.getOrDefault("FLAG", "0"); int flag = 0; try { flag = Integer.parseInt(flagObj.toString()); } catch (NumberFormatException e) { log.info("flag must be integer, flagObj:{}", flagObj); } rocketMsg.setFlag(flag); Object waitStoreMsgOkObj = headers.getOrDefault("WAIT_STORE_MSG_OK", "true"); boolean waitStoreMsgOK = Boolean.TRUE.equals(waitStoreMsgOkObj); rocketMsg.setWaitStoreMsgOK(waitStoreMsgOK); headers.entrySet().stream() .filter(entry -> !Objects.equals(entry.getKey(), "FLAG") && !Objects.equals(entry.getKey(), "WAIT_STORE_MSG_OK")) .forEach(entry -> { if (!MessageConst.STRING_HASH_SET.contains(entry.getKey())) { rocketMsg.putUserProperty(entry.getKey(), String.valueOf(entry.getValue())); } }); } return rocketMsg; }  static TransactionListener convert(RocketMQLocalTransactionListener listener); static MessagingException convert(MQClientException e); static org.springframework.messaging.Message convertToSpringMessage(
        org.apache.rocketmq.common.message.MessageExt message); static String toRocketHeaderKey(String rawKey); static org.springframework.messaging.Message convertToSpringMessage(
        org.apache.rocketmq.common.message.Message message); static org.apache.rocketmq.common.message.Message convertToRocketMessage(
            ObjectMapper objectMapper, String charset,
            String destination, org.springframework.messaging.Message<?> message); static RPCHook getRPCHookByAkSk(Environment env, String accessKeyOrExpr, String secretKeyOrExpr); static String getInstanceName(RPCHook rpcHook, String identify);  }