Skip to content

Commit fbeeb15

Browse files
committed
调整优化
1 parent a43316f commit fbeeb15

3 files changed

Lines changed: 48 additions & 50 deletions

File tree

tx-manager/src/main/java/com/codingapi/tm/compensate/service/impl/CompensateServiceImpl.java

Lines changed: 44 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -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

tx-manager/src/main/java/com/codingapi/tm/manager/service/impl/TxManagerServiceImpl.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,12 @@ public int cleanNotifyTransaction(String groupId, String taskId) {
9797
String key = configReader.getKeyPrefix() + groupId;
9898
TxGroup txGroup = redisServerService.getTxGroupByKey(key);
9999
if (txGroup==null) {
100+
logger.info("cleanNotifyTransaction - > txGroup is null ");
100101
return res;
101102
}
102103

103-
if(txGroup.getState()==0){
104+
if(txGroup.getHasOver()==0){
105+
logger.info("cleanNotifyTransaction - > groupId "+groupId+" not over !");
104106
return 0;
105107
}
106108

@@ -150,6 +152,7 @@ public boolean closeTransactionGroup(String groupId,int state) {
150152
}
151153
txGroup.setState(state);
152154
txGroup.setHasOver(1);
155+
redisServerService.saveTransaction(key,txGroup.toJsonString());
153156
return transactionConfirmService.confirm(txGroup);
154157
}
155158

tx-manager/src/main/java/com/codingapi/tm/netty/service/impl/ActionCTGServiceImpl.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.alibaba.fastjson.JSONObject;
44
import com.codingapi.tm.manager.service.TxManagerService;
5-
import com.codingapi.tm.netty.model.TxGroup;
65
import com.codingapi.tm.netty.service.IActionService;
76
import org.springframework.beans.factory.annotation.Autowired;
87
import org.springframework.stereotype.Service;

0 commit comments

Comments
 (0)