rocketmq-spring icon indicating copy to clipboard operation
rocketmq-spring copied to clipboard

RocketMQ Consumer Serialize Only Support JSON ?

Open chenghm123 opened this issue 4 years ago • 2 comments

public class DefaultRocketMQListenerContainer implements InitializingBean,
    RocketMQListenerContainer, SmartLifecycle, ApplicationContextAware {
    // ……

    private Object doConvertMessage(MessageExt messageExt) {
        if (Objects.equals(messageType, MessageExt.class) || Objects.equals(messageType, org.apache.rocketmq.common.message.Message.class)) {
            return messageExt;
        } else {
            String str = new String(messageExt.getBody(), Charset.forName(charset));
            if (Objects.equals(messageType, String.class)) {
                return str;
            } else {
                // If msgType not string, use objectMapper change it.
                try {
                    if (messageType instanceof Class) {
                        //if the messageType has not Generic Parameter
                        return this.getMessageConverter().fromMessage(MessageBuilder.withPayload(str).build(), (Class<?>) messageType);
                    } else {
                        //if the messageType has Generic Parameter, then use SmartMessageConverter#fromMessage with third parameter "conversionHint".
                        //we have validate the MessageConverter is SmartMessageConverter in this#getMethodParameter.
                        return ((SmartMessageConverter) this.getMessageConverter()).fromMessage(MessageBuilder.withPayload(str).build(), (Class<?>) ((ParameterizedType) messageType).getRawType(), methodParameter);
                    }
                } catch (Exception e) {
                    log.info("convert failed. str:{}, msgType:{}", str, messageType);
                    throw new RuntimeException("cannot convert message to " + messageType, e);
                }
            }
        }
    }

   // ……
}

这个直接就new String()了,后面MessageConverter发挥不出更大的作用,万一序列化用的是hession或者kryo就会失败。 顺便是否可以扩展一下不通的消费实例,不同的RocketMQTemplate设置不同的MessageConverter,来兼容很多公司的一些现状。

chenghm123 avatar Mar 24 '21 14:03 chenghm123

这个直接就new String()了,后面MessageConverter发挥不出更大的作用,万一序列化用的是hession或者kryo就会失败。 顺便是否可以扩展一下不通的消费实例,不同的RocketMQTemplate设置不同的MessageConverter,来兼容很多公司的一些现状。

chenghm123 avatar Mar 24 '21 14:03 chenghm123

Very nice suggestion. Message Schema really needs to support cross-industry. For now, would you like to help to draft this improvement proposal (pr)? I am thinking to support here using OpenSchema implementation in the near future.

vongosling avatar Apr 22 '21 11:04 vongosling