Skip to content

Commit 7e0b5a6

Browse files
committed
合并修好.
1 parent fbeeb15 commit 7e0b5a6

1 file changed

Lines changed: 50 additions & 62 deletions

File tree

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

Lines changed: 50 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -57,80 +57,68 @@ public boolean saveCompensateMsg(TransactionCompensateMsg transactionCompensateM
5757
String key = configReader.getKeyPrefix() + transactionCompensateMsg.getGroupId();
5858
TxGroup txGroup = redisServerService.getTxGroupByKey(key);
5959
if (txGroup == null) {
60-
return false;
61-
}
62-
63-
redisServerService.deleteKey(key);
64-
65-
//已经全部通知的模块不做补偿处理
66-
boolean hasNoNotify = false;
67-
68-
for(TxInfo txInfo:txGroup.getList()){
69-
if(txInfo.getNotify()==0){
70-
hasNoNotify = true;
71-
}
60+
// key = configReader.getKeyPrefixNotify() + transactionCompensateMsg.getGroupId();
61+
// txGroup = redisServerService.getTxGroupByKey(key);
7262
}
73-
74-
if(!hasNoNotify){
75-
//事务已经执行完毕的
76-
logger.info("TxGroup had notify ! ");
77-
return true;
78-
}
79-
80-
81-
transactionCompensateMsg.setTxGroup(txGroup);
82-
83-
final String json = JSON.toJSONString(transactionCompensateMsg);
84-
85-
logger.info("补偿->" + json);
86-
87-
final String compensateKey = compensateDao.saveCompensateMsg(transactionCompensateMsg);
88-
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);
63+
if(txGroup!=null) {
64+
redisServerService.deleteKey(key);
65+
66+
transactionCompensateMsg.setTxGroup(txGroup);
67+
68+
final String json = JSON.toJSONString(transactionCompensateMsg);
69+
70+
logger.info("Compensate->" + json);
71+
72+
final String compensateKey = compensateDao.saveCompensateMsg(transactionCompensateMsg);
73+
74+
//调整自动补偿机制,若开启了自动补偿,需要通知业务返回success,方可执行自动补偿
75+
threadPool.execute(new Runnable() {
76+
@Override
77+
public void run() {
78+
try {
79+
String groupId = transactionCompensateMsg.getGroupId();
80+
JSONObject requestJson = new JSONObject();
81+
requestJson.put("action", "compensate");
82+
requestJson.put("groupId", groupId);
83+
requestJson.put("json", json);
84+
85+
String url = configReader.getCompensateNotifyUrl();
86+
logger.error("Compensate Callback Address->" + url);
87+
String res = HttpUtils.postJson(url, requestJson.toJSONString());
88+
logger.error("Compensate Callback Result->" + res);
89+
if (configReader.isCompensateAuto()) {
90+
//自动补偿,是否自动执行补偿
91+
if (res.contains("success")||res.contains("SUCCESS")) {
92+
//自动补偿
93+
autoCompensate(compensateKey, transactionCompensateMsg);
94+
}
10995
}
96+
} catch (Exception e) {
97+
logger.error("Compensate Callback Fails->" + e.getMessage());
11098
}
111-
} catch (Exception e) {
112-
logger.error("补偿回调失败->" + e.getMessage());
11399
}
114-
}
115-
});
100+
});
116101

117-
return StringUtils.isNotEmpty(compensateKey);
102+
return StringUtils.isNotEmpty(compensateKey);
103+
}else {
104+
return false;
105+
}
118106

119107
}
120108

121109

122110
public void autoCompensate(final String compensateKey, TransactionCompensateMsg transactionCompensateMsg) {
123111
final String json = JSON.toJSONString(transactionCompensateMsg);
124-
logger.info("自动补偿->" + json);
112+
logger.info("Auto Compensate->" + json);
125113
//自动补偿业务执行...
126114
final int tryTime = configReader.getCompensateTryTime();
127115
boolean autoExecuteRes = false;
128116
try {
129117
int executeCount = 0;
130118
autoExecuteRes = _executeCompensate(json);
131-
logger.info("自动补偿结果->" + autoExecuteRes + ",json->" + json);
119+
logger.info("Automatic Compensate Result->" + autoExecuteRes + ",json->" + json);
132120
while (!autoExecuteRes) {
133-
logger.info("try补偿(补偿失败,进入补偿队列)->" + autoExecuteRes + ",json->" + json);
121+
logger.info("Compensate Failure, Entering Compensate Queue->" + autoExecuteRes + ",json->" + json);
134122
executeCount++;
135123
if(executeCount==3){
136124
autoExecuteRes = false;
@@ -150,7 +138,7 @@ public void autoCompensate(final String compensateKey, TransactionCompensateMsg
150138
}
151139

152140
}catch (Exception e){
153-
logger.error("自动补偿失败,msg:"+e.getLocalizedMessage());
141+
logger.error("Auto Compensate Fails,msg:"+e.getLocalizedMessage());
154142
//推送数据给第三方通知
155143
autoExecuteRes = false;
156144
}
@@ -163,9 +151,9 @@ public void autoCompensate(final String compensateKey, TransactionCompensateMsg
163151
requestJson.put("resState",autoExecuteRes);
164152

165153
String url = configReader.getCompensateNotifyUrl();
166-
logger.error("补偿结果回调地址->" + url);
154+
logger.error("Compensate Result Callback Address->" + url);
167155
String res = HttpUtils.postJson(url, requestJson.toJSONString());
168-
logger.error("补偿结果回调结果->" + res);
156+
logger.error("Compensate Result Callback Result->" + res);
169157

170158
}
171159

@@ -276,7 +264,7 @@ public void reloadCompensate(TxGroup txGroup) {
276264
}
277265
}
278266

279-
logger.info("加载补偿以后->"+JSON.toJSONString(txGroup));
267+
logger.info("Compensate Loaded->"+JSON.toJSONString(txGroup));
280268
}
281269

282270
private TxGroup getCompensateByGroupId(String groupId) {
@@ -295,7 +283,7 @@ public boolean executeCompensate(String path) throws ServiceException {
295283

296284
String json = compensateDao.getCompensate(path);
297285
if (json == null) {
298-
throw new ServiceException("不存在该数据");
286+
throw new ServiceException("no data existing");
299287
}
300288

301289
boolean hasOk = _executeCompensate(json);
@@ -316,7 +304,7 @@ private boolean _executeCompensate(String json) throws ServiceException {
316304

317305
ModelInfo modelInfo = ModelInfoManager.getInstance().getModelByModel(model);
318306
if (modelInfo == null) {
319-
throw new ServiceException("当前模块不在线.");
307+
throw new ServiceException("current model offline.");
320308
}
321309

322310
String data = jsonObject.getString("data");

0 commit comments

Comments
 (0)