caizhenghao commented on issue #4535:
URL: https://github.com/apache/rocketmq/issues/4535#issuecomment-1236376248
I met this exception too. After I read the code in
DefaultRocketMQListenerContainer.getMethodParameter,I think it not a bug but a
problem in usage.I change my code and avoid this problem.I hope it can be
helpful.
public abstract class AbstractBaseListener<T> implements
RocketMQListener<MessageExt> {
private final Type msgBodyType;
public AbstractBaseListener() {
Type typeAbstractListener = this.getClass().getGenericSuperclass();
Type[] actualTypeArguments = ((ParameterizedType)
typeAbstractListener).getActualTypeArguments();
if (Objects.nonNull(actualTypeArguments) &&
actualTypeArguments.length > 0) {
msgBodyType = actualTypeArguments[0];
} else {
throw new IllegalStateException("can not explain the object");
}
}
@Override
public void onMessage(MessageExt messageExt) {
String bodyStr = new String(messageExt.getBody(),
Charset.forName("UTF-8"));
Object msgBody;
if (msgBodyType == String.class) {
msgBody = bodyStr;
} else {
msgBody = JSON.parseObject(bodyStr, msgBodyType);
}
doConsume((T) msgBody);
}
}
@RocketMQMessageListener(topic=xxx, consumerGroup=xxx)
public class ConsumerA extends AbstractBaseListener<Entity> {
}
--
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.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]