apache / apache/rocketmq-spring
Cannot implement parent class encapsulation to handle the same business logic in a unified way
- Dominant language
- Java
- Stars
- 2.3k
- Forks
- 943
- PR merge metrics
- No merged PRs in 30d
Description
I want to realize that sending a message is passing the current login context information to the message consumer
```java
// This is the code for the parent class
public abstract class BaseMQConsumer {
/**
* Used to store the user who sent the current message
*/
private static final ThreadLocal SEND_MSG_USER = new ThreadLocal<>();
/**
* Enhancements to the default message listener methods to set the global user that sends the current message.
*/
public final void onMessage(T message) {
try {
SEND_MSG_USER.set(message.getUser());
this.onMessage(message, message.getUser());
} finally {
SEND_MSG_USER.remove();
}
}
/**
* Execute the onMessage method
* @param message Original message
* @param sendMsgUser The user who sent the current message
*/
public abstract void onMessage(T message, User sendMsgUser);
/**
* Get the user who sent the current message
*/
public final User getCurrentUser() {
return SEND_MSG_USER.get();
}
}
```
```java
// Here's the code for the subclass
@Slf4j
@Component
@RequiredArgsConstructor
@RocketMQMessageListener(consumerGroup = "${rocketmq.group}" + "-group-sync-order", topic = "${rocketmq.group}" + MqTopic.SYNC_ORDER_TOPIC, consumeMode = ConsumeMode.ORDERLY)
public class TmsSyncOrderConsumer extends BaseMQConsumer implements RocketMQListener {
private final RedisTemplate redisTemplate;
@Override
public void onMessage(SonMQMessage message, User sendMsgUser) {
User currentUser = SecurityUtils.getCurrentUser();
String msgText = message.getMsgText();
SysOrderVO sysOrderVO = JSON.parseObject(msgText, SysOrderVO.class);
try {
log.info("[onMessage] message:{}, sendMsgUser:{}, currentUser:{}", message, sendMsgUser, currentUser);
} finally {
RedisKey redisKey = RedisKey.STORE_SYNC_ORDER_LOCK;
String key = redisKey.getKey(sysOrderVO.getShopId());
redisTemplate.delete(key);
}
}
}
```
```java
// This is the parent message object
@Getter
@Setter
public class MQMessage implements Serializable {
private static final long serialVersionUID = 1463117417381786448L;
private User user;
public MQMessage() {
this.user = SecurityUtils.getCurrentUser();
}
private String msgId;
private Integer msgType;
private String msgText;
private Long channelInfoId;
}
```
```java
// This is the subclass message object
public class SonMQMessage extends MQMessage {
private static final long serialVersionUID = -6645128888099960442L;
private String username;
}
```
Running error: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.apache.rocketmq.spring.support.DefaultRocketMQListenerContainer_5': Invocation of init method failed; nested exception is java.lang.RuntimeException: parameterType:class com.shenzhen.common.rocketmq.model.SonMQMessage of onMessage method is not supported
But I have implemented the onMessage method in the parent class. It's just that my parameter type is dynamic
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.