apache / apache/rocketmq-spring

RocketMQ Consumer Serialize Only Support JSON ?

Open
#359 2 comments 0 reactions 0 assignees View on GitHub
discuss enhancement
Dominant language
Java
Stars
2.3k
Forks
943
PR merge metrics
No merged PRs in 30d

Description

```java
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`,来兼容很多公司的一些现状。

Contributor guide

No contributing guide indexed for this repository

Research direction

Start in DefaultRocketMQListenerContainer, especially doConvertMessage, and inspect how MessageConverter and RocketMQTemplate are currently configured. Determine how non-JSON or binary payload conversion and separate converters per consumer or template should be supported, then add focused coverage showing the intended conversion behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
backend, distributed-systems
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.