feat:OkHttpClient

This commit is contained in:
苏竹红
2025-11-11 18:16:21 +08:00
parent bb1939b42a
commit 7f342a168d
3 changed files with 31 additions and 20 deletions

View File

@@ -23,8 +23,10 @@ import org.springframework.web.bind.annotation.RequestParam;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.io.IOException; import java.io.IOException;
import java.net.SocketTimeoutException;
import java.text.MessageFormat; import java.text.MessageFormat;
import java.util.*; import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors; import java.util.stream.Collectors;
/** /**
@@ -68,6 +70,12 @@ public class XinFaDeviceService {
private final Map<String, HuoMaAccountDTO> accountMap = new HashMap<>(); private final Map<String, HuoMaAccountDTO> accountMap = new HashMap<>();
private final OkHttpClient httpClient = new OkHttpClient.Builder()
.connectTimeout(120, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.build();
@PostConstruct @PostConstruct
public void initAccountMap() { public void initAccountMap() {
accountMap.put("restaurant", new HuoMaAccountDTO(huoMaRestaurantStoresAccount, huoMaRestaurantStoresPassword)); accountMap.put("restaurant", new HuoMaAccountDTO(huoMaRestaurantStoresAccount, huoMaRestaurantStoresPassword));
@@ -102,7 +110,7 @@ public class XinFaDeviceService {
public List<StoreEquipmentDTO> getStoreEquipmentDataByStoreNumList(List<String> storeNumList, String token) { public List<StoreEquipmentDTO> getStoreEquipmentDataByStoreNumList(List<String> storeNumList, String token) {
Map<String,List<String>> requestBody = new HashMap<>(); Map<String,List<String>> requestBody = new HashMap<>();
requestBody.put("codeList", storeNumList); requestBody.put("codeList", storeNumList);
String responseBody = sendPostRequestByToken(JSONObject.toJSONString(requestBody), huoMaGetPointTerminalUrl,token); String responseBody = sendPostRequestNoRetryByToken(JSONObject.toJSONString(requestBody), huoMaGetPointTerminalUrl,token);
try{ try{
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper();
JsonNode rootNode = mapper.readTree(responseBody); JsonNode rootNode = mapper.readTree(responseBody);
@@ -127,7 +135,7 @@ public class XinFaDeviceService {
} }
} }
StoreRequestDTO requestBody = new StoreRequestDTO("point_report", 0, 10, storeNum); StoreRequestDTO requestBody = new StoreRequestDTO("point_report", 0, 10, storeNum);
String responseBody = sendPostRequestByToken(JSONObject.toJSONString(requestBody), huoMaGetStoreIdUrl,token); String responseBody = sendPostRequestNoRetryByToken(JSONObject.toJSONString(requestBody), huoMaGetStoreIdUrl,token);
try{ try{
Integer storeId = extractIdsFromResponse(responseBody); Integer storeId = extractIdsFromResponse(responseBody);
redisUtilPool.hashSet(RedisConstant.HUO_MA_STORE_ID, storeNum, storeId.toString()); redisUtilPool.hashSet(RedisConstant.HUO_MA_STORE_ID, storeNum, storeId.toString());
@@ -186,7 +194,7 @@ public class XinFaDeviceService {
TagDTO tagDTO = new TagDTO("DEFAULT", 0, 30,"program"); TagDTO tagDTO = new TagDTO("DEFAULT", 0, 30,"program");
String responseBody = null; String responseBody = null;
try{ try{
responseBody = sendPostRequestByToken(JSONObject.toJSONString(tagDTO), huoMaGetTagUrl,token); responseBody = sendPostRequestNoRetryByToken(JSONObject.toJSONString(tagDTO), huoMaGetTagUrl,token);
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper();
JsonNode rootNode = mapper.readTree(responseBody); JsonNode rootNode = mapper.readTree(responseBody);
@@ -238,7 +246,7 @@ public class XinFaDeviceService {
String token = getStoreToken(huoMaAccountDTO.getAccount(), huoMaAccountDTO.getPassword()); String token = getStoreToken(huoMaAccountDTO.getAccount(), huoMaAccountDTO.getPassword());
String responseBody = null; String responseBody = null;
try{ try{
responseBody = sendPostRequestByToken(JSONObject.toJSONString(programReqDTO), huoMaGetProgramUrl,token); responseBody = sendPostRequestNoRetryByToken(JSONObject.toJSONString(programReqDTO), huoMaGetProgramUrl,token);
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper();
JsonNode rootNode = mapper.readTree(responseBody); JsonNode rootNode = mapper.readTree(responseBody);
@@ -270,7 +278,7 @@ public class XinFaDeviceService {
String token = getStoreToken(huoMaAccountDTO.getAccount(), huoMaAccountDTO.getPassword()); String token = getStoreToken(huoMaAccountDTO.getAccount(), huoMaAccountDTO.getPassword());
String responseBody = null; String responseBody = null;
try{ try{
responseBody = sendPostRequestByToken(JSONObject.toJSONString(publishDTO), huoMaGetPublishUrl,token); responseBody = sendPostRequestNoRetryByToken(JSONObject.toJSONString(publishDTO), huoMaGetPublishUrl,token);
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper();
JsonNode rootNode = mapper.readTree(responseBody); JsonNode rootNode = mapper.readTree(responseBody);
int code = rootNode.get("code").asInt(); int code = rootNode.get("code").asInt();
@@ -294,7 +302,7 @@ public class XinFaDeviceService {
StoreXinFaDetailRequestDTO storeXinFaDetailRequestDTO = new StoreXinFaDetailRequestDTO(0, 10, pointId); StoreXinFaDetailRequestDTO storeXinFaDetailRequestDTO = new StoreXinFaDetailRequestDTO(0, 10, pointId);
String responseBody = null; String responseBody = null;
try{ try{
responseBody = sendPostRequestByToken(JSONObject.toJSONString(storeXinFaDetailRequestDTO), huoMaGetStoreXinFaDeviceDetailUrl,token); responseBody = sendPostRequestNoRetryByToken(JSONObject.toJSONString(storeXinFaDetailRequestDTO), huoMaGetStoreXinFaDeviceDetailUrl,token);
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper();
JsonNode rootNode = mapper.readTree(responseBody); JsonNode rootNode = mapper.readTree(responseBody);
@@ -502,8 +510,8 @@ public class XinFaDeviceService {
} }
@NotNull @NotNull
private static String sendPost(String requestUrl, Request request) { private String sendPost(String requestUrl, Request request) {
try (Response response = new OkHttpClient().newCall(request).execute()) { try (Response response = httpClient.newCall(request).execute()) {
log.info("发起请求 time{}", System.currentTimeMillis()); log.info("发起请求 time{}", System.currentTimeMillis());
if (!response.isSuccessful()) { if (!response.isSuccessful()) {
log.info("HTTP请求失败msg: " + response.message()); log.info("HTTP请求失败msg: " + response.message());
@@ -513,6 +521,9 @@ public class XinFaDeviceService {
String responseBody = response.body().string(); String responseBody = response.body().string();
log.info("请求成功responseBody:{}", JSONObject.toJSONString(responseBody)); log.info("请求成功responseBody:{}", JSONObject.toJSONString(responseBody));
return responseBody; return responseBody;
} catch (SocketTimeoutException e) {
log.error("API调用超时 - URL: {}, 错误: {}", requestUrl, e.getMessage(), e);
throw new ServiceException(ErrorCodeEnum.THIRD_API_ERROR, "接口调用超时: " + e.getMessage());
} catch (ServiceException e) { } catch (ServiceException e) {
throw e; throw e;
} catch (Exception e) { } catch (Exception e) {

View File

@@ -150,9 +150,9 @@ huoMa.get.point.terminal.url = https://www.huoMayunping.com/api/terminal/getPoin
huoMa.get.tag.url = https://www.huomayunping.com/api/tag/search huoMa.get.tag.url = https://www.huomayunping.com/api/tag/search
huoMa.get.program.url = https://www.huomayunping.com/api/program/search huoMa.get.program.url = https://www.huomayunping.com/api/program/search
huoMa.get.publish.url = https://www.huomayunping.com/api/channelPublish/target/v2/quick-publish huoMa.get.publish.url = https://www.huomayunping.com/api/channelPublish/target/v2/quick-publish
huoMa.direct.stores.account = 15370309163 huoMa.direct.stores.account = 18375320931
huoMa.direct.stores.password = Zx@123456. huoMa.direct.stores.password = Huoma@123456.
huoMa.franchise.stores.account = 13563273279 huoMa.franchise.stores.account = 18375320931
huoMa.franchise.stores.password = Zx@123456. huoMa.franchise.stores.password = Huoma@123456.
huoMa.restaurant.stores.account = 18656552865 huoMa.restaurant.stores.account = 15167817007
huoMa.restaurant.stores.password = ZX123456 huoMa.restaurant.stores.password = Huoma@123456.

View File

@@ -163,9 +163,9 @@ huoMa.get.point.terminal.url = https://www.huoMayunping.com/api/terminal/getPoin
huoMa.get.tag.url = https://www.huomayunping.com/api/tag/search huoMa.get.tag.url = https://www.huomayunping.com/api/tag/search
huoMa.get.program.url = https://www.huomayunping.com/api/program/search huoMa.get.program.url = https://www.huomayunping.com/api/program/search
huoMa.get.publish.url = https://www.huomayunping.com/api/channelPublish/target/v2/quick-publish huoMa.get.publish.url = https://www.huomayunping.com/api/channelPublish/target/v2/quick-publish
huoMa.direct.stores.account = 15370309163 huoMa.direct.stores.account = 18375320931
huoMa.direct.stores.password = Zx@123456. huoMa.direct.stores.password = Huoma@123456.
huoMa.franchise.stores.account = 13563273279 huoMa.franchise.stores.account = 13345565081
huoMa.franchise.stores.password = Zx@123456. huoMa.franchise.stores.password = Huoma@123456.
huoMa.restaurant.stores.account = 18656552865 huoMa.restaurant.stores.account = 15167817007
huoMa.restaurant.stores.password = ZX123456 huoMa.restaurant.stores.password = Huoma@123456.