Use YDRequestBody in YDRequest for YDJob and CWJob

pull/8040/head
Qiuyi LI 2024-06-27 17:13:34 +02:00
parent 38a832eee4
commit ced711cb03
3 changed files with 19 additions and 25 deletions

View File

@ -1,7 +1,6 @@
package org.jeecg.modules.business.domain.api.yd; package org.jeecg.modules.business.domain.api.yd;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse; import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair; import org.apache.http.NameValuePair;
@ -23,18 +22,16 @@ import java.util.List;
@Slf4j @Slf4j
public class YDRequest { public class YDRequest {
private final static String URL = "http://oms.ydhex.com/webservice/PublicService.asmx/ServiceInterfaceUTF8"; private final static String URL = "http://oms.ydhex.com/webservice/PublicService.asmx/ServiceInterfaceUTF8";
private String appToken; private final String appToken;
private String appKey; private final String appKey;
private final YDRequestBody ydRequestBody;
private static final String SERVICE_METHOD = "gettrack";
private static final RequestConfig REQUEST_CONFIG = RequestConfig.custom().build(); private static final RequestConfig REQUEST_CONFIG = RequestConfig.custom().build();
private final List<String> billCodes; public YDRequest(String appToken, String appKey, YDRequestBody ydRequestBody) {
public YDRequest(String appToken, String appKey, List<String> billCodes) {
this.appToken = appToken; this.appToken = appToken;
this.appKey = appKey; this.appKey = appKey;
this.billCodes = billCodes; this.ydRequestBody = ydRequestBody;
} }
/** /**
@ -53,7 +50,7 @@ public class YDRequest {
request.setEntity(new UrlEncodedFormEntity(generateFormData(), "UTF-8")); request.setEntity(new UrlEncodedFormEntity(generateFormData(), "UTF-8"));
return httpClient.execute(request); return httpClient.execute(request);
} catch (Exception e) { } catch (Exception e) {
log.error("Request failed on attempt n°" + attempts); log.error("Request failed on attempt n°{}", attempts);
} }
} }
return null; return null;
@ -67,18 +64,11 @@ public class YDRequest {
*/ */
private List<NameValuePair> generateFormData() { private List<NameValuePair> generateFormData() {
List<NameValuePair> pairs = new ArrayList<>(); List<NameValuePair> pairs = new ArrayList<>();
String paramsJson = generateJsonString(billCodes);
pairs.add(new BasicNameValuePair("appToken", appToken)); pairs.add(new BasicNameValuePair("appToken", appToken));
pairs.add(new BasicNameValuePair("appKey", appKey)); pairs.add(new BasicNameValuePair("appKey", appKey));
pairs.add(new BasicNameValuePair("serviceMethod", SERVICE_METHOD)); pairs.add(new BasicNameValuePair("serviceMethod", ydRequestBody.getServiceMethod()));
pairs.add(new BasicNameValuePair("paramsJson", paramsJson)); pairs.add(new BasicNameValuePair("paramsJson", ydRequestBody.getParamsJson()));
return pairs; return pairs;
} }
private static String generateJsonString(List<String> billCodes) {
JSONObject param = new JSONObject();
String billCodesWithComas = String.join(",", billCodes);
param.put("tracking_number", billCodesWithComas);
return param.toJSONString();
}
} }

View File

@ -9,8 +9,9 @@ import org.apache.http.util.EntityUtils;
import org.codehaus.jettison.json.JSONArray; import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException; import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject; import org.codehaus.jettison.json.JSONObject;
import org.jeecg.modules.business.domain.api.yd.YDParcelTraceRequestBody;
import org.jeecg.modules.business.domain.api.yd.YDParcelTraceResponse;
import org.jeecg.modules.business.domain.api.yd.YDRequest; import org.jeecg.modules.business.domain.api.yd.YDRequest;
import org.jeecg.modules.business.domain.api.yd.YDResponse;
import org.jeecg.modules.business.domain.api.yd.YDTraceData; import org.jeecg.modules.business.domain.api.yd.YDTraceData;
import org.jeecg.modules.business.service.IParcelService; import org.jeecg.modules.business.service.IParcelService;
import org.jeecg.modules.business.service.IPlatformOrderService; import org.jeecg.modules.business.service.IPlatformOrderService;
@ -99,7 +100,8 @@ public class CWJob implements Job {
List<YDTraceData> parcelTraces = new ArrayList<>(); List<YDTraceData> parcelTraces = new ArrayList<>();
List<YDRequest> ydRequests = new ArrayList<>(); List<YDRequest> ydRequests = new ArrayList<>();
billCodeLists.forEach(billcodeList -> { billCodeLists.forEach(billcodeList -> {
YDRequest ydRequest = new YDRequest(APP_TOKEN, APP_KEY, billcodeList); YDParcelTraceRequestBody ydParcelTraceRequestBody = new YDParcelTraceRequestBody(billcodeList);
YDRequest ydRequest = new YDRequest(APP_TOKEN, APP_KEY, ydParcelTraceRequestBody);
ydRequests.add(ydRequest); ydRequests.add(ydRequest);
}); });
ExecutorService executor = Executors.newFixedThreadPool(DEFAULT_NUMBER_OF_THREADS); ExecutorService executor = Executors.newFixedThreadPool(DEFAULT_NUMBER_OF_THREADS);
@ -110,7 +112,7 @@ public class CWJob implements Job {
try { try {
// String of the response // String of the response
String responseString = EntityUtils.toString(entity, "UTF-8"); String responseString = EntityUtils.toString(entity, "UTF-8");
YDResponse ydResponse = mapper.readValue(responseString, YDResponse.class); YDParcelTraceResponse ydResponse = mapper.readValue(responseString, YDParcelTraceResponse.class);
parcelTraces.addAll(ydResponse.getTraceDataList()); parcelTraces.addAll(ydResponse.getTraceDataList());
success = true; success = true;
} catch (IOException e) { } catch (IOException e) {

View File

@ -9,8 +9,9 @@ import org.apache.http.util.EntityUtils;
import org.codehaus.jettison.json.JSONArray; import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException; import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject; import org.codehaus.jettison.json.JSONObject;
import org.jeecg.modules.business.domain.api.yd.YDParcelTraceRequestBody;
import org.jeecg.modules.business.domain.api.yd.YDParcelTraceResponse;
import org.jeecg.modules.business.domain.api.yd.YDRequest; import org.jeecg.modules.business.domain.api.yd.YDRequest;
import org.jeecg.modules.business.domain.api.yd.YDResponse;
import org.jeecg.modules.business.domain.api.yd.YDTraceData; import org.jeecg.modules.business.domain.api.yd.YDTraceData;
import org.jeecg.modules.business.service.IParcelService; import org.jeecg.modules.business.service.IParcelService;
import org.jeecg.modules.business.service.IPlatformOrderService; import org.jeecg.modules.business.service.IPlatformOrderService;
@ -99,7 +100,8 @@ public class YDJob implements Job {
List<YDTraceData> parcelTraces = new ArrayList<>(); List<YDTraceData> parcelTraces = new ArrayList<>();
List<YDRequest> ydRequests = new ArrayList<>(); List<YDRequest> ydRequests = new ArrayList<>();
billCodeLists.forEach(billcodeList -> { billCodeLists.forEach(billcodeList -> {
YDRequest ydRequest = new YDRequest(APP_TOKEN, APP_KEY, billcodeList); YDParcelTraceRequestBody ydParcelTraceRequestBody = new YDParcelTraceRequestBody(billcodeList);
YDRequest ydRequest = new YDRequest(APP_TOKEN, APP_KEY, ydParcelTraceRequestBody);
ydRequests.add(ydRequest); ydRequests.add(ydRequest);
}); });
ExecutorService executor = Executors.newFixedThreadPool(DEFAULT_NUMBER_OF_THREADS); ExecutorService executor = Executors.newFixedThreadPool(DEFAULT_NUMBER_OF_THREADS);
@ -110,7 +112,7 @@ public class YDJob implements Job {
try { try {
// String of the response // String of the response
String responseString = EntityUtils.toString(entity, "UTF-8"); String responseString = EntityUtils.toString(entity, "UTF-8");
YDResponse ydResponse = mapper.readValue(responseString, YDResponse.class); YDParcelTraceResponse ydResponse = mapper.readValue(responseString, YDParcelTraceResponse.class);
parcelTraces.addAll(ydResponse.getTraceDataList()); parcelTraces.addAll(ydResponse.getTraceDataList());
success = true; success = true;
} catch (IOException e) { } catch (IOException e) {