spring事務手動回滾


@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;
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM