|
24 | 24 |
|
25 | 25 | package saga;
|
26 | 26 |
|
| 27 | +import com.alibaba.fastjson.JSONObject; |
| 28 | +import common.constant.Constant; |
| 29 | +import common.constant.ParamFieldConstant; |
| 30 | +import common.model.DtmServerInfo; |
| 31 | +import common.utils.HttpUtil; |
| 32 | +import common.enums.TransTypeEnum; |
| 33 | +import common.model.TransBase; |
| 34 | + |
| 35 | +import java.util.ArrayList; |
| 36 | +import java.util.HashMap; |
| 37 | +import java.util.Map; |
| 38 | + |
27 | 39 | public class Saga {
|
28 | 40 |
|
| 41 | + private static final String ORDERS = "orders"; |
| 42 | + |
| 43 | + private static final String CONCURRENT = "concurrent"; |
| 44 | + |
| 45 | + private TransBase transBase; |
| 46 | + |
| 47 | + private DtmServerInfo dtmServerInfo; |
| 48 | + |
| 49 | + private boolean concurrent; |
| 50 | + |
| 51 | + private Map<String, ArrayList<Integer>> orders; |
| 52 | + |
| 53 | + public Saga(String ipPort, String gid) { |
| 54 | + this.dtmServerInfo = new DtmServerInfo(ipPort); |
| 55 | + this.transBase = new TransBase(TransTypeEnum.SAGA, gid, false); |
| 56 | + this.concurrent = false; |
| 57 | + this.orders = new HashMap<>(); |
| 58 | + } |
| 59 | + |
| 60 | + public Saga add(String action, String compensate, Object postData) { |
| 61 | + HashMap<String, String> step = new HashMap<>(); |
| 62 | + step.put(ParamFieldConstant.ACTION, action); |
| 63 | + step.put(ParamFieldConstant.COMPENSATE, compensate); |
| 64 | + transBase.getSteps().add(step); |
| 65 | + transBase.getPayloads().add(JSONObject.toJSONString(postData)); |
| 66 | + return this; |
| 67 | + } |
| 68 | + |
| 69 | + public Saga addBranchOrder(Integer branch, ArrayList<Integer> preBranches) { |
| 70 | + orders.put(branch.toString(), preBranches); |
| 71 | + return this; |
| 72 | + } |
| 73 | + |
| 74 | + public Saga enableConcurrent() { |
| 75 | + concurrent = true; |
| 76 | + return this; |
| 77 | + } |
| 78 | + |
| 79 | + public void submit() throws Exception { |
| 80 | + addConcurrentContext(); |
| 81 | + HashMap<String, Object> mapParam = new HashMap<>(Constant.DEFAULT_INITIAL_CAPACITY); |
| 82 | + mapParam.put(ParamFieldConstant.GID, transBase.getGid()); |
| 83 | + mapParam.put(ParamFieldConstant.TRANS_TYPE, TransTypeEnum.SAGA.getValue()); |
| 84 | + mapParam.put(ParamFieldConstant.STEPS, transBase.getSteps()); |
| 85 | + mapParam.put(ParamFieldConstant.PAYLOADS, transBase.getPayloads()); |
| 86 | + mapParam.put(ParamFieldConstant.CUSTOM_DATA, transBase.getCustomData()); |
| 87 | + mapParam.put(ParamFieldConstant.WAIT_RESULT, transBase.isWaitResult()); |
| 88 | + mapParam.put(ParamFieldConstant.TIMEOUT_TO_FAIL, transBase.getTimeoutToFail()); |
| 89 | + mapParam.put(ParamFieldConstant.RETRY_INTERVAL, transBase.getRetryInterval()); |
| 90 | + mapParam.put(ParamFieldConstant.PASSTHROGH_HEADERS, transBase.getPassthroughHeaders()); |
| 91 | + mapParam.put(ParamFieldConstant.BRANCH_HEADERS, transBase.getBranchHeaders()); |
| 92 | + |
| 93 | + HttpUtil.post(dtmServerInfo.submit(), JSONObject.toJSONString(mapParam)); |
| 94 | + } |
| 95 | + |
| 96 | + public Saga enableWaitResult() { |
| 97 | + transBase.setWaitResult(true); |
| 98 | + return this; |
| 99 | + } |
| 100 | + |
| 101 | + public Saga setTimeoutToFail(long timeoutToFail) { |
| 102 | + transBase.setTimeoutToFail(timeoutToFail); |
| 103 | + return this; |
| 104 | + } |
| 105 | + |
| 106 | + public Saga setRetryInterval(long retryInterval) { |
| 107 | + transBase.setRetryInterval(retryInterval); |
| 108 | + return this; |
| 109 | + } |
| 110 | + |
| 111 | + public Saga setBranchHeaders(Map<String, String> headers) { |
| 112 | + transBase.setBranchHeaders(headers); |
| 113 | + return this; |
| 114 | + } |
| 115 | + |
| 116 | + public Saga setPassthroughHeaders(ArrayList<String> passthroughHeaders) { |
| 117 | + transBase.setPassthroughHeaders(passthroughHeaders); |
| 118 | + return this; |
| 119 | + } |
| 120 | + |
| 121 | + private void addConcurrentContext() { |
| 122 | + if (concurrent) { |
| 123 | + HashMap<String, Object> data = new HashMap<>(); |
| 124 | + data.put(ORDERS, orders); |
| 125 | + data.put(CONCURRENT, true); |
| 126 | + transBase.setCustomData(JSONObject.toJSONString(data)); |
| 127 | + } |
| 128 | + } |
29 | 129 | }
|
0 commit comments