forked from dtm-labs/dtmcli-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTcc.java
More file actions
146 lines (123 loc) · 5.47 KB
/
Tcc.java
File metadata and controls
146 lines (123 loc) · 5.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
* MIT License
*
* Copyright (c) 2021 yedf
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package tcc;
import com.alibaba.fastjson.JSONObject;
import common.constant.Constant;
import common.model.DtmConsumer;
import common.model.DtmServerInfo;
import common.utils.HttpUtil;
import common.utils.BranchIdGeneratorUtil;
import common.constant.ParamFieldConstant;
import common.enums.TransTypeEnum;
import common.model.TransBase;
import exception.FailureException;
import okhttp3.Response;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.HashMap;
/**
* @author lixiaoshuang
*/
public class Tcc {
private static final String DEFAULT_STATUS = "prepared";
private static final String OP = "try";
private static final String FAIL_RESULT = "FAILURE";
Logger log = LoggerFactory.getLogger(Tcc.class);
/**
* 事务信息
*/
private TransBase transBase;
/**
* server 信息
*/
private DtmServerInfo dtmServerInfo;
/**
* id 生成器
*/
private BranchIdGeneratorUtil branchIdGeneratorUtil;
public Tcc(String ipPort, String gid) {
this.dtmServerInfo = new DtmServerInfo(ipPort);
this.branchIdGeneratorUtil = new BranchIdGeneratorUtil("");
this.transBase = new TransBase(TransTypeEnum.TCC, gid, false);
}
public void tccGlobalTransaction(DtmConsumer<Tcc> consumer) throws Exception {
HashMap<String, Object> paramMap = new HashMap<>(Constant.DEFAULT_INITIAL_CAPACITY);
paramMap.put(ParamFieldConstant.GID, transBase.getGid());
paramMap.put(ParamFieldConstant.TRANS_TYPE, TransTypeEnum.TCC.getValue());
try {
Response response = HttpUtil.post(dtmServerInfo.prepare(), JSONObject.toJSONString(paramMap));
this.checkResult(response);
} catch (FailureException failureException) {
log.info("tccGlobalTransaction fail message:{}" + failureException.getLocalizedMessage());
throw failureException;
}
try {
consumer.accept(this);
HttpUtil.post(dtmServerInfo.submit(), JSONObject.toJSONString(paramMap));
} catch (Exception e) {
HttpUtil.post(dtmServerInfo.abort(), JSONObject.toJSONString(paramMap));
throw e;
}
}
public Response callBranch(Object body, String tryUrl, String confirmUrl, String cancelUrl) throws Exception {
String branchId = branchIdGeneratorUtil.genBranchId();
HashMap<String, Object> registerParam = new HashMap<>(Constant.DEFAULT_INITIAL_CAPACITY);
registerParam.put(ParamFieldConstant.GID, transBase.getGid());
registerParam.put(ParamFieldConstant.BRANCH_ID, branchId);
registerParam.put(ParamFieldConstant.TRANS_TYPE, TransTypeEnum.TCC.getValue());
registerParam.put(ParamFieldConstant.STATUS, DEFAULT_STATUS);
registerParam.put(ParamFieldConstant.DATA, JSONObject.toJSONString(body));
registerParam.put(ParamFieldConstant.TRY, tryUrl);
registerParam.put(ParamFieldConstant.CONFIRM, confirmUrl);
registerParam.put(ParamFieldConstant.CANCEL, cancelUrl);
Response registerResponse = HttpUtil
.post(dtmServerInfo.registerTccBranch(), JSONObject.toJSONString(registerParam));
this.checkResult(registerResponse);
Response tryResponse = HttpUtil
.post(splicingTryUrl(tryUrl, transBase.getGid(), TransTypeEnum.TCC.getValue(), branchId, OP),
JSONObject.toJSONString(body));
this.checkResult(tryResponse);
return tryResponse;
}
/**
* 和go 保持同样的发送方式 参数拼在url后边
*/
private String splicingTryUrl(String tryUrl, String gid, String transType, String branchId, String op) {
return tryUrl + "?gid=" + gid + "&trans_type=" + transType + "&branch_id=" + branchId + "&op=" + op;
}
public void checkResult(Response response)throws Exception {
int errorCode = 400;
if (response.code() >= errorCode){
throw new FailureException(response.message());
}
String result = response.body().string();
if (StringUtils.isBlank(result)) {
throw new FailureException("response is null");
}
if (result.contains(FAIL_RESULT)){
throw new FailureException("Service returned failed");
}
}
}