You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@rocketmq.apache.org by GitBox <gi...@apache.org> on 2020/09/02 08:45:16 UTC

[GitHub] [rocketmq-spring] cj-8480 opened a new issue #294: RocketMQUtil中 getAndWrapMessage 在获取 KEYS时缺少前缀

cj-8480 opened a new issue #294:
URL: https://github.com/apache/rocketmq-spring/issues/294


   以master分支代码为例
   1、先使用下图方法进行 Message 转换时 加上了前缀
   ![image](https://user-images.githubusercontent.com/33209018/91959165-dbd0c600-ed3a-11ea-9190-f3bc2a15f62a.png)
        toRocketHeaderKey方法,如下图
   ![image](https://user-images.githubusercontent.com/33209018/91959307-03c02980-ed3b-11ea-88a8-dfbffb090aac.png)
   
   2、RocketMQTemplate.send 方法 传入SpringMessage 后,工具类会调用下图方法进行message转换
   ![image](https://user-images.githubusercontent.com/33209018/91959507-497cf200-ed3b-11ea-9c1b-eee85c7060cc.png)
   此时,会导致KEYS参数丢失,因为缺少前缀
   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [rocketmq-spring] cj-8480 commented on issue #294: GetAndWrapmessage method in rocketmqUtil is missing prefix when getting keys

Posted by GitBox <gi...@apache.org>.
cj-8480 commented on issue #294:
URL: https://github.com/apache/rocketmq-spring/issues/294#issuecomment-693281878


   @RongtongJin OK,已经提交pull request


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [rocketmq-spring] RongtongJin closed issue #294: GetAndWrapmessage method in rocketmqUtil is missing prefix when getting keys

Posted by GitBox <gi...@apache.org>.
RongtongJin closed issue #294:
URL: https://github.com/apache/rocketmq-spring/issues/294


   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [rocketmq-spring] cj-8480 commented on issue #294: GetAndWrapmessage method in rocketmqUtil is missing prefix when getting keys

Posted by GitBox <gi...@apache.org>.
cj-8480 commented on issue #294:
URL: https://github.com/apache/rocketmq-spring/issues/294#issuecomment-688018715


   @RongtongJin 
   直接补充前缀应该会影响到其他正在使用的。
   我在自己程序里面增加下面这个方法暂时先解决了。
   <pre><code>
   private Message<?> convertToSpringMessage(org.apache.rocketmq.common.message.Message message) {
   	Message<?> convertToSpringMessage = RocketMQUtil.convertToSpringMessage(message);
   	// 转换Message补充KEYS参数,解决KEYS空问题
   	// --- start ---
   	Message<?> targetMessage = null;
   	if (convertToSpringMessage instanceof GenericMessage) {
   		GenericMessage<?> sourceMessage = (GenericMessage<?>) convertToSpringMessage;
   		Map<String, Object> headers = new HashMap<>();
   		headers.putAll(sourceMessage.getHeaders());
   		headers.put(RocketMQHeaders.KEYS, message.getKeys());
   		targetMessage = new GenericMessage<>(sourceMessage.getPayload(), headers);
   	}
   	// --- end ---
   
   	return targetMessage;
   }
   </code></pre>


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [rocketmq-spring] cj-8480 commented on issue #294: GetAndWrapmessage method in rocketmqUtil is missing prefix when getting keys

Posted by GitBox <gi...@apache.org>.
cj-8480 commented on issue #294:
URL: https://github.com/apache/rocketmq-spring/issues/294#issuecomment-688206300


   @RongtongJin 我这边应该没有提交权限的
   我改的话,对应方法改改后如下:
   ```Java
   private static Message getAndWrapMessage(String destination, MessageHeaders headers, byte[] payloads) {
       if (destination == null || destination.length() < 1) {
           return null;
       }
       if (payloads == null || payloads.length < 1) {
           return null;
       }
       String[] tempArr = destination.split(":", 2);
       String topic = tempArr[0];
       String tags = "";
       if (tempArr.length > 1) {
           tags = tempArr[1];
       }
       Message rocketMsg = new Message(topic, tags, payloads);
       if (Objects.nonNull(headers) && !headers.isEmpty()) {
   
           // 修改部分 --- start ---
           // 默认先获取不带前缀的keys
           Object keys = headers.get(RocketMQHeaders.KEYS);
   	// 当获取不到再从headers取带前缀的keys的结果
   	if (StringUtils.isEmpty(keys)) {
   	    keys = headers.get(toRocketHeaderKey(RocketMQHeaders.KEYS));
   	}
   	// 修改部分 --- end ---
   
           if (!StringUtils.isEmpty(keys)) { // if headers has 'KEYS', set rocketMQ message key
               rocketMsg.setKeys(keys.toString());
           }
           Object flagObj = headers.getOrDefault("FLAG", "0");
           int flag = 0;
           try {
               flag = Integer.parseInt(flagObj.toString());
           } catch (NumberFormatException e) {
               // Ignore it
               if (log.isInfoEnabled()) {
                   log.info("flag must be integer, flagObj:{}", flagObj);
               }
           }
           rocketMsg.setFlag(flag);
           Object waitStoreMsgOkObj = headers.getOrDefault("WAIT_STORE_MSG_OK", "true");
           rocketMsg.setWaitStoreMsgOK(Boolean.TRUE.equals(waitStoreMsgOkObj));
           headers.entrySet().stream()
               .filter(entry -> !Objects.equals(entry.getKey(), "FLAG")
                   && !Objects.equals(entry.getKey(), "WAIT_STORE_MSG_OK")) // exclude "FLAG", "WAIT_STORE_MSG_OK"
               .forEach(entry -> {
                   if (!MessageConst.STRING_HASH_SET.contains(entry.getKey())) {
                       rocketMsg.putUserProperty(entry.getKey(), String.valueOf(entry.getValue()));
                   }
               });
   
       }
       return rocketMsg;
   }
   ```
   
   修改内容就其中中文标注的部分。
   补充了一段,当原本逻辑获取不到keys的结果,重新按照带前缀的参数获取一次。
   这样能够兼容原本的,而且keys参数能够正常获取。


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [rocketmq-spring] RongtongJin commented on issue #294: GetAndWrapmessage method in rocketmqUtil is missing prefix when getting keys

Posted by GitBox <gi...@apache.org>.
RongtongJin commented on issue #294:
URL: https://github.com/apache/rocketmq-spring/issues/294#issuecomment-687002969


   @cj-8480 Could you help fix the issue and verify it with UT ?


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [rocketmq-spring] RongtongJin commented on issue #294: GetAndWrapmessage method in rocketmqUtil is missing prefix when getting keys

Posted by GitBox <gi...@apache.org>.
RongtongJin commented on issue #294:
URL: https://github.com/apache/rocketmq-spring/issues/294#issuecomment-692497159


   > @RongtongJin 我这边应该没有提交权限的
   > 我改的话,对应方法改改后如下:
   > 
   > ```java
   > private static Message getAndWrapMessage(String destination, MessageHeaders headers, byte[] payloads) {
   >     if (destination == null || destination.length() < 1) {
   >         return null;
   >     }
   >     if (payloads == null || payloads.length < 1) {
   >         return null;
   >     }
   >     String[] tempArr = destination.split(":", 2);
   >     String topic = tempArr[0];
   >     String tags = "";
   >     if (tempArr.length > 1) {
   >         tags = tempArr[1];
   >     }
   >     Message rocketMsg = new Message(topic, tags, payloads);
   >     if (Objects.nonNull(headers) && !headers.isEmpty()) {
   > 
   >         // 修改部分 --- start ---
   >         // 默认先获取不带前缀的keys
   >         Object keys = headers.get(RocketMQHeaders.KEYS);
   > 	// 当获取不到再从headers取带前缀的keys的结果
   > 	if (StringUtils.isEmpty(keys)) {
   > 	    keys = headers.get(toRocketHeaderKey(RocketMQHeaders.KEYS));
   > 	}
   > 	// 修改部分 --- end ---
   > 
   >         if (!StringUtils.isEmpty(keys)) { // if headers has 'KEYS', set rocketMQ message key
   >             rocketMsg.setKeys(keys.toString());
   >         }
   >         Object flagObj = headers.getOrDefault("FLAG", "0");
   >         int flag = 0;
   >         try {
   >             flag = Integer.parseInt(flagObj.toString());
   >         } catch (NumberFormatException e) {
   >             // Ignore it
   >             if (log.isInfoEnabled()) {
   >                 log.info("flag must be integer, flagObj:{}", flagObj);
   >             }
   >         }
   >         rocketMsg.setFlag(flag);
   >         Object waitStoreMsgOkObj = headers.getOrDefault("WAIT_STORE_MSG_OK", "true");
   >         rocketMsg.setWaitStoreMsgOK(Boolean.TRUE.equals(waitStoreMsgOkObj));
   >         headers.entrySet().stream()
   >             .filter(entry -> !Objects.equals(entry.getKey(), "FLAG")
   >                 && !Objects.equals(entry.getKey(), "WAIT_STORE_MSG_OK")) // exclude "FLAG", "WAIT_STORE_MSG_OK"
   >             .forEach(entry -> {
   >                 if (!MessageConst.STRING_HASH_SET.contains(entry.getKey())) {
   >                     rocketMsg.putUserProperty(entry.getKey(), String.valueOf(entry.getValue()));
   >                 }
   >             });
   > 
   >     }
   >     return rocketMsg;
   > }
   > ```
   > 
   > 修改内容就其中中文标注的部分。
   > 补充了一段,当原本逻辑获取不到keys的结果,重新按照带前缀的参数获取一次。
   > 这样能够兼容原本的,而且keys参数能够正常获取。
   
   我认为你的代码是ok,你可以提交一个pull request,我可以帮你review以及合并


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [rocketmq-spring] cj-8480 removed a comment on issue #294: GetAndWrapmessage method in rocketmqUtil is missing prefix when getting keys

Posted by GitBox <gi...@apache.org>.
cj-8480 removed a comment on issue #294:
URL: https://github.com/apache/rocketmq-spring/issues/294#issuecomment-688017664


   直接添加前缀会影响到其他类的使用的。
   我在自己补充了下面的方法,先行解决了
   <pre><code>
   private Message<?> convertToSpringMessage(org.apache.rocketmq.common.message.Message message) {
   	Message<?> convertToSpringMessage = RocketMQUtil.convertToSpringMessage(message);
   	// 转换Message补充KEYS参数,解决KEYS空BUG
   	// --- start ---
   	Message<?> targetMessage = null;
   	if (convertToSpringMessage instanceof GenericMessage) {
   		GenericMessage<?> sourceMessage = (GenericMessage<?>) convertToSpringMessage;
   		Map<String, Object> headers = new HashMap<>();
   		headers.putAll(sourceMessage.getHeaders());
   		headers.put(RocketMQHeaders.KEYS, message.getKeys());
   		targetMessage = new GenericMessage<>(sourceMessage.getPayload(), headers);
   	}
   	// --- end ---
   
   	return targetMessage;
   }
   </code></pre>
   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [rocketmq-spring] cj-8480 commented on issue #294: GetAndWrapmessage method in rocketmqUtil is missing prefix when getting keys

Posted by GitBox <gi...@apache.org>.
cj-8480 commented on issue #294:
URL: https://github.com/apache/rocketmq-spring/issues/294#issuecomment-688017664


   直接添加前缀会影响到其他类的使用的。
   我在自己补充了下面的方法,先行解决了
   <pre><code>
   private Message<?> convertToSpringMessage(org.apache.rocketmq.common.message.Message message) {
   	Message<?> convertToSpringMessage = RocketMQUtil.convertToSpringMessage(message);
   	// 转换Message补充KEYS参数,解决KEYS空BUG
   	// --- start ---
   	Message<?> targetMessage = null;
   	if (convertToSpringMessage instanceof GenericMessage) {
   		GenericMessage<?> sourceMessage = (GenericMessage<?>) convertToSpringMessage;
   		Map<String, Object> headers = new HashMap<>();
   		headers.putAll(sourceMessage.getHeaders());
   		headers.put(RocketMQHeaders.KEYS, message.getKeys());
   		targetMessage = new GenericMessage<>(sourceMessage.getPayload(), headers);
   	}
   	// --- end ---
   
   	return targetMessage;
   }
   </code></pre>
   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org