Skip to content

Commit 27fd4c5

Browse files
committed
自定义与TxManager的http请求方式
1 parent 1c6faad commit 27fd4c5

3 files changed

Lines changed: 69 additions & 7 deletions

File tree

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.codingapi.tx.netty.service;
2+
3+
/**
4+
* create by lorne on 2017/11/17
5+
*/
6+
public interface HttpRequestService {
7+
8+
String httpGet(String url);
9+
10+
String httpPost(String url, String params);
11+
12+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.codingapi.tx.netty.service;
2+
3+
4+
import com.lorne.core.framework.utils.http.HttpUtils;
5+
import org.springframework.stereotype.Component;
6+
7+
/**
8+
* create by lorne on 2017/11/17
9+
*/
10+
@Component
11+
public class TxManagerHttpRequestHelper {
12+
13+
14+
private HttpRequestService httpRequestService;
15+
16+
public void setHttpRequestService(HttpRequestService httpRequestService) {
17+
this.httpRequestService = httpRequestService;
18+
}
19+
20+
private void reloadHttpRequestService(){
21+
if(httpRequestService==null){
22+
httpRequestService = new HttpRequestService() {
23+
@Override
24+
public String httpGet(String url) {
25+
return HttpUtils.get(url);
26+
}
27+
28+
@Override
29+
public String httpPost(String url, String params) {
30+
return HttpUtils.post(url, params);
31+
}
32+
};
33+
}
34+
}
35+
36+
public String httpGet(String url) {
37+
reloadHttpRequestService();
38+
return httpRequestService.httpGet(url);
39+
}
40+
41+
public String httpPost(String url, String params) {
42+
reloadHttpRequestService();
43+
return httpRequestService.httpPost(url,params);
44+
}
45+
46+
47+
}

tx-client/src/main/java/com/codingapi/tx/netty/service/impl/MQTxManagerServiceImpl.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
import com.codingapi.tx.model.Request;
1313
import com.codingapi.tx.model.TxGroup;
1414
import com.codingapi.tx.netty.service.MQTxManagerService;
15+
import com.codingapi.tx.netty.service.TxManagerHttpRequestHelper;
1516
import com.lorne.core.framework.utils.encode.Base64Utils;
16-
import com.lorne.core.framework.utils.http.HttpUtils;
1717
import org.springframework.beans.factory.annotation.Autowired;
1818
import org.springframework.stereotype.Service;
1919

@@ -33,6 +33,8 @@ public class MQTxManagerServiceImpl implements MQTxManagerService {
3333
@Autowired
3434
private CompensateService compensateService;
3535

36+
@Autowired
37+
private TxManagerHttpRequestHelper managerHelper;
3638

3739

3840
@Override
@@ -102,7 +104,8 @@ public int checkTransactionInfo(String groupId, String taskId) {
102104

103105
@Override
104106
public int getTransaction(String groupId, String waitTaskId) {
105-
String json = HttpUtils.get(configReader.getTxUrl() + "getTransaction?groupId=" + groupId + "&taskId=" + waitTaskId);
107+
108+
String json = managerHelper.httpGet(configReader.getTxUrl() + "getTransaction?groupId=" + groupId + "&taskId=" + waitTaskId);
106109
if (json == null) {
107110
return -2;
108111
}
@@ -117,8 +120,8 @@ public int getTransaction(String groupId, String waitTaskId) {
117120

118121
@Override
119122
public int clearTransaction(String groupId, String waitTaskId, boolean isGroup) {
120-
String murl = configReader.getTxUrl() + "clearTransaction?groupId=" + groupId + "&taskId=" + waitTaskId + "&isGroup=" + (isGroup ? 1 : 0);
121-
String clearRes = HttpUtils.get(murl);
123+
String url = configReader.getTxUrl() + "clearTransaction?groupId=" + groupId + "&taskId=" + waitTaskId + "&isGroup=" + (isGroup ? 1 : 0);
124+
String clearRes = managerHelper.httpGet(url);
122125
if(clearRes==null){
123126
return -1;
124127
}
@@ -128,8 +131,8 @@ public int clearTransaction(String groupId, String waitTaskId, boolean isGroup)
128131

129132
@Override
130133
public String httpGetServer() {
131-
String murl = configReader.getTxUrl() + "getServer";
132-
return HttpUtils.get(murl);
134+
String url = configReader.getTxUrl() + "getServer";
135+
return managerHelper.httpGet(url);
133136
}
134137

135138
@Override
@@ -150,7 +153,7 @@ public void sendCompensateMsg(String groupId, long time, TxTransactionInfo info)
150153

151154
CompensateInfo compensateInfo = new CompensateInfo(currentTime, modelName, uniqueKey, data, methodStr, className, groupId, address, time);
152155

153-
String json = HttpUtils.post(configReader.getTxUrl() + "sendCompensateMsg", compensateInfo.toParamsString());
156+
String json = managerHelper.httpPost(configReader.getTxUrl() + "sendCompensateMsg", compensateInfo.toParamsString());
154157

155158
compensateInfo.setResJson(json);
156159

0 commit comments

Comments
 (0)