@@ -57,68 +57,64 @@ public boolean saveCompensateMsg(TransactionCompensateMsg transactionCompensateM
5757 String key = configReader .getKeyPrefix () + transactionCompensateMsg .getGroupId ();
5858 TxGroup txGroup = redisServerService .getTxGroupByKey (key );
5959 if (txGroup == null ) {
60- // key = configReader.getKeyPrefixNotify() + transactionCompensateMsg.getGroupId();
61- // txGroup = redisServerService.getTxGroupByKey(key);
62- //todo 待完善
60+ return false ;
6361 }
64- if (txGroup !=null ) {
65- redisServerService .deleteKey (key );
66-
67- //已经全部通知的模块不做补偿处理
68- boolean hasNoNotify = false ;
69- for (TxInfo txInfo :txGroup .getList ()){
70- if (txInfo .getNotify ()==0 ){
71- hasNoNotify = true ;
72- }
73- }
7462
75- if (!hasNoNotify ){
76- //事务已经执行完毕的
77- return true ;
78- }
63+ redisServerService .deleteKey (key );
7964
65+ //已经全部通知的模块不做补偿处理
66+ boolean hasNoNotify = false ;
8067
68+ for (TxInfo txInfo :txGroup .getList ()){
69+ if (txInfo .getNotify ()==0 ){
70+ hasNoNotify = true ;
71+ }
72+ }
73+
74+ if (!hasNoNotify ){
75+ //事务已经执行完毕的
76+ logger .info ("TxGroup had notify ! " );
77+ return true ;
78+ }
8179
82- transactionCompensateMsg .setTxGroup (txGroup );
8380
84- final String json = JSON . toJSONString ( transactionCompensateMsg );
81+ transactionCompensateMsg . setTxGroup ( txGroup );
8582
86- logger . info ( "补偿->" + json );
83+ final String json = JSON . toJSONString ( transactionCompensateMsg );
8784
88- final String compensateKey = compensateDao . saveCompensateMsg ( transactionCompensateMsg );
85+ logger . info ( "补偿->" + json );
8986
90- //调整自动补偿机制,若开启了自动补偿,需要通知业务返回success,方可执行自动补偿
91- threadPool .execute (new Runnable () {
92- @ Override
93- public void run () {
94- try {
95- String groupId = transactionCompensateMsg .getGroupId ();
96- JSONObject requestJson = new JSONObject ();
97- requestJson .put ("action" , "compensate" );
98- requestJson .put ("groupId" , groupId );
99- requestJson .put ("json" , json );
87+ final String compensateKey = compensateDao .saveCompensateMsg (transactionCompensateMsg );
10088
101- String url = configReader .getCompensateNotifyUrl ();
102- logger .error ("补偿回调地址->" + url );
103- String res = HttpUtils .postJson (url , requestJson .toJSONString ());
104- logger .error ("补偿回调结果->" + res );
105- if (configReader .isCompensateAuto ()) {
106- //自动补偿,是否自动执行补偿
107- if (res .contains ("success" )||res .contains ("SUCCESS" )) {
108- //自动补偿
109- autoCompensate (compensateKey , transactionCompensateMsg );
110- }
89+ //调整自动补偿机制,若开启了自动补偿,需要通知业务返回success,方可执行自动补偿
90+ threadPool .execute (new Runnable () {
91+ @ Override
92+ public void run () {
93+ try {
94+ String groupId = transactionCompensateMsg .getGroupId ();
95+ JSONObject requestJson = new JSONObject ();
96+ requestJson .put ("action" , "compensate" );
97+ requestJson .put ("groupId" , groupId );
98+ requestJson .put ("json" , json );
99+
100+ String url = configReader .getCompensateNotifyUrl ();
101+ logger .error ("补偿回调地址->" + url );
102+ String res = HttpUtils .postJson (url , requestJson .toJSONString ());
103+ logger .error ("补偿回调结果->" + res );
104+ if (configReader .isCompensateAuto ()) {
105+ //自动补偿,是否自动执行补偿
106+ if (res .contains ("success" )||res .contains ("SUCCESS" )) {
107+ //自动补偿
108+ autoCompensate (compensateKey , transactionCompensateMsg );
111109 }
112- } catch (Exception e ) {
113- logger .error ("补偿回调失败->" + e .getMessage ());
114110 }
111+ } catch (Exception e ) {
112+ logger .error ("补偿回调失败->" + e .getMessage ());
115113 }
116- });
114+ }
115+ });
117116
118- return StringUtils .isNotEmpty (compensateKey );
119- }else {
120- return false ;
121- }
117+ return StringUtils .isNotEmpty (compensateKey );
122118
123119 }
124120
0 commit comments