spring事务手动回滚

时间:2023-12-10 14:12:50
@Transactional(rollbackFor = { Exception.class })
public JSONObject preSendMsg(AuthInfo authInfo, Message message) {
JSONObject jsonObject = new ResultJSONObject("000", "消息预发送成功");
try {
// 判断参数有效性
if (null == authInfo || StringUtil.isEmpty(authInfo.getUsername()) || StringUtil.isEmpty(authInfo.getPassword())
|| null == message || StringUtil.isEmpty(message.getQueueName())
|| StringUtil.isEmpty(message.getExchange()) || StringUtil.isEmpty(message.getContent())) {
return new ResultJSONObject("001", "重要参数不可为空!");
}
// 检查消息操作的权限信息
// 检查队列配置是否存在 1
JSONObject checkAuthJson = this.checkAuthInfo(authInfo, message.getQueueName());
if (!"000".equals((String) checkAuthJson.get("resultCode"))) {
return checkAuthJson;
}
Map<String, Object> mqInfo = checkAuthJson.getJSONObject("mqInfo"); // 检查消息的发送频率,控制流速...TODO
// 消息去重,相同消息返回相同的msgId 2
if (!chechMsgUnique(mqInfo, message)) {
return new ResultJSONObject("021", "消息内容重复!");
}
// 保存消息 3
MessageTpl messageTpl = saveMessage(mqInfo, message);
if(null==messageTpl){
return new ResultJSONObject("031", "消息保存失败!");
}
jsonObject.put("msgId", messageTpl.getMsgId().toString());
} catch (Exception e) {
logger.error("preSendMsg_Exception ", e);
jsonObject=new ResultJSONObject("preSendMsg_Exception", "消息预发送异常");
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动开启事务回滚
}
return jsonObject;
}