问题遇到的现象和发生背景
Java在调用接口,当请求过于频繁时,有的请求会响应超时,有的请求正常。
问题相关代码,请勿粘贴截图
package guide.iface.oa.webservice;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.rmi.RemoteException;
import java.util.*;
/**
2022/4/22 新建
/
public class OaWebService {public static void main(String[] args) throws RemoteException {
String jsonText = "{'aaa':'1111','bbb':'22222'}"; getOfficeAuto(jsonText);
}
/**
- 默认参数设置 setConnectTimeout:设置连接超时时间,单位毫秒。
- setConnectionRequestTimeout:设置从connect Manager获取Connection 超时时间,单位毫秒。
- setSocketTimeout:请求获取数据的超时时间,单位毫秒。访问一个接口,多少时间内无法返回数据,就直接放弃此次调用。 暂时定义15分钟
- /
private static RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(600000).setConnectTimeout(600000).setConnectionRequestTimeout(600000).build();
public synchronized static LinkedHashMap<String, String> getOfficeAuto(String jsonText) throws RemoteException {
String url = "xxxxxxxx"; String xml = jsonToXml(jsonText); return sendHttpPost(url, xml);
}
/**
- @Description json 字符串转 xml报文
- @Param [jsonText]
- @Return java.lang.String
- @Date 2022/5/5 9:09
- /
public static String jsonToXml(String jsonText) {
String xml = "";
xml = "<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope//%22 xmlns:web="webservices.ofs.weaver.com.cn">\n"
return xml;+ " <soapenv:Header/>\n" + " <soapenv:Body>\n" + " <web:receiveRequestInfoByJson>\n" + " <web:in0>" + jsonText + "</web:in0>\n" + " </web:receiveRequestInfoByJson>\n" + " </soapenv:Body>\n" + "</soapenv:Envelope>";
}
/**
- 发送 post请求
- @param httpUrl 地址
- @param params 参数(格式:key1=value1&key2=value2)
- /
public synchronized static LinkedHashMap<String, String> sendHttpPost(String httpUrl, String params) {
HttpPost httpPost = new HttpPost(httpUrl);// 创建httpPost
try {
} catch (Exception e) {// 设置参数 StringEntity stringEntity = new StringEntity(params, "UTF-8"); stringEntity.setContentType("application/json"); httpPost.setEntity(stringEntity);
}e.printStackTrace();
return sendHttpPost(httpPost);
}
/**
- 发送Post请求
- @param httpPost
- @return
- /
private synchronized static LinkedHashMap<String, String> sendHttpPost(HttpPost httpPost) {
CloseableHttpClient httpClient = null;
CloseableHttpResponse response = null;
HttpEntity entity = null;
LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
String responseContent = null;
try {
} catch (Exception e) {// 创建默认的httpClient实例 httpClient = HttpClients.createDefault(); httpPost.setConfig(requestConfig); // 执行请求 response = httpClient.execute(httpPost); entity = response.getEntity(); responseContent = EntityUtils.toString(entity, "UTF-8");// 返回json格式 map.put("status", response.getStatusLine().getStatusCode() + ""); map.put("message", StringEscapeUtils.unescapeHtml3(responseContent));
} finally {map.put("status", "599"); if (e.toString().length() > 200) { map.put("message", e.toString().substring(0, 200)); } else { map.put("message", e.toString()); } e.printStackTrace();
}try { // 关闭连接,释放资源 if (response != null) { response.close(); } if (httpClient != null) { httpClient.close(); } } catch (IOException e) { e.printStackTrace(); }
return map;
}
}
运行结果及报错内容
java.net.UnknownHostException: urlxx
接口响应有时正常,有时连接超时。超时情况大约在集中请求的时候。
我的解答思路和尝试过的方法
- 使用ping 未看出有丢包的情况
- 尝试使用同步和睡眠方法还是不行
我想要达到的结果
响应无连接超时,数据请求返回正常