Java CloseableHttpClient执行耗时太多
当我调用CloseableHttpClient.execute方法时,第一次调用它需要花费很多时间。例如,如果我在For bucle中调用一个API调用10次,那么第一次调用比其余调用花费的时间多得多,我不知道原因。 如果有人能帮忙,我将不胜感激。 问候Java CloseableHttpClient执行耗时太多,java,rest,Java,Rest,当我调用CloseableHttpClient.execute方法时,第一次调用它需要花费很多时间。例如,如果我在For bucle中调用一个API调用10次,那么第一次调用比其余调用花费的时间多得多,我不知道原因。 如果有人能帮忙,我将不胜感激。 问候 publicstaticvoidmain(字符串[]args)抛出KeyStoreException、IOException、nosuchalgorithexception、certificateeexception、Exception { A
publicstaticvoidmain(字符串[]args)抛出KeyStoreException、IOException、nosuchalgorithexception、certificateeexception、Exception
{
AcanoAPICallsTest2测试=新的AcanoAPICallsTest2();
AcanoAPIHandler clientHandler=test.getClientHandler();
if(clientHandler.getConnectCode()==HttpStatus.SC\u确定){
int statusCode=clientHandler.executeMethod(CommonSettings.GET,“/api/xxx);
}
}
clientHandler.shutDownClient();
}
公共类AcanoAPIHandler扩展了ClientHandler
{
受保护的记录器=Logger.getLogger(this.getClass());
私有最终字符串LOCATION=“LOCATION”;
私有字符串位置;
//private int connectCode=HttpStatus.SC\u服务\u不可用;
/**
*返回响应头的“位置”字段(如果存在)
*
*@返回
*/
公共字符串getLocationHeaderResponse()
{
返回位置;
}
//默认构造函数
公共AcanoAPIHandler()
{
超级();
}
公共AcanoAPIHandler(字符串协议、字符串主机、整数端口、字符串用户名、字符串密码)
{
超级(协议、主机、端口、用户名、密码);
}
@凌驾
public int executeMethod(字符串类型、字符串路径、列表…nvps)
{
int statusCode=super.executeMethod(类型、路径、nvps);
this.location=null;
if(type.equalsIgnoreCase(CommonSettings.POST)| type.equalsIgnoreCase(CommonSettings.PUT))
{
//如果statusCode为200,则设置位置标题
if(statusCode==HttpStatus.SC\u OK)
{
Header[]h=this.getResponse().getAllHeaders();
对于(int i=0;i0)
{
this.location=split[split.length-1];
打破
}
}
}
}
}
返回状态码;
}
}
ClientHandler.executeMethod
public int executeMethod(String type, String path, List<BasicNameValuePair>... nvps)
{
int statusCode = -1;
HttpUriRequest request = createUriRequest(type, path);
this.responseContent = null;
this.response = null;
try
{
if (nvps.length > 0)
{
if (type.equalsIgnoreCase(CommonSettings.POST))
{
((HttpPost) request).setEntity(new UrlEncodedFormEntity(nvps[0], "UTF-8"));
}
else if (type.equalsIgnoreCase(CommonSettings.PUT))
{
((HttpPut) request).setEntity(new UrlEncodedFormEntity(nvps[0], "UTF-8"));
}
else
{
logger.warn("Can only set entity on POST/PUT operation, ignoring nvps");
}
}
}
catch (UnsupportedEncodingException ex)
{
java.util.logging.Logger.getLogger(ClientHandler.class.getName()).log(Level.SEVERE, null, ex);
}
if (this.httpclient != null)
{
try
{
long start = System.currentTimeMillis();
this.response = this.httpclient.execute(request);
long end = System.currentTimeMillis();
long res = end - start;
System.out.println("httpclient.execute " + " seconds: "+res/1000);
statusCode = response.getStatusLine().getStatusCode();
HttpEntity entity = response.getEntity();
if (entity != null)
{
InputStream fis = entity.getContent();
this.responseContent = convertStreamToString(fis);
EntityUtils.consume(entity);
fis.close();
}
}
catch (IOException ex)
{
java.util.logging.Logger.getLogger(ClientHandler.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
return SERVER_ERROR;
}
finally
{
// release connection
if (type.equalsIgnoreCase(CommonSettings.GET))
{
((HttpGet) request).releaseConnection();
}
else if (type.equalsIgnoreCase(CommonSettings.POST))
{
((HttpPost) request).releaseConnection();
}
else if (type.equalsIgnoreCase(CommonSettings.PUT))
{
((HttpPut) request).releaseConnection();
}
else if (type.equalsIgnoreCase(CommonSettings.DELETE))
{
((HttpDelete) request).releaseConnection();
}
// close the response
try
{
if (this.response != null)
{
this.response.close();
}
}
catch (IOException ex)
{
java.util.logging.Logger.getLogger(ClientHandler.class.getName()).log(Level.SEVERE, null, ex);
return SERVER_ERROR;
}
}
}
return statusCode;
}
public int executeMethod(字符串类型、字符串路径、列表…nvps)
{
int statusCode=-1;
HttpUriRequest请求=createUriRequest(类型、路径);
this.responseContent=null;
this.response=null;
尝试
{
如果(nvps.length>0)
{
if(类型相等信号案例(CommonSettings.POST))
{
setEntity(新的UrlEncodedFormEntity(nvps[0],“UTF-8”);
}
else if(type.equalsIgnoreCase(CommonSettings.PUT))
{
setEntity(新的UrlEncodedFormEntity(nvps[0],“UTF-8”);
}
其他的
{
logger.warn(“只能在POST/PUT操作上设置实体,忽略nvps”);
}
}
}
捕获(不支持DencodingException ex)
{
getLogger(ClientHandler.class.getName()).log(Level.SEVERE,null,ex);
}
if(this.httpclient!=null)
{
尝试
{
长启动=System.currentTimeMillis();
this.response=this.httpclient.execute(请求);
long end=System.currentTimeMillis();
长res=结束-开始;
System.out.println(“httpclient.execute”+”秒:“+res/1000”);
statusCode=response.getStatusLine().getStatusCode();
HttpEntity=response.getEntity();
如果(实体!=null)
{
InputStream fis=entity.getContent();
this.responseContent=convertStreamToString(fis);
EntityUtils.consume(实体);
fis.close();
}
}
捕获(IOEX异常)
{
getLogger(ClientHandler.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
返回服务器错误;
}
最后
{
//释放连接
if(type.equalsIgnoreCase(CommonSettings.GET))
{
((HttpGet)请求).releaseConnection();
}
else if(type.equalsIgnoreCase(CommonSettings.POST))
{
((HttpPost)请求).releaseConnection();
}
else if(type.equalsIgnoreCase(CommonSettings.PUT))
{
((HttpPut)请求).releaseConnection();
}
else if(type.equalsIgnoreCase(CommonSettings.DELETE))
{
((HttpDelete)请求).releaseConnection();
}
//关闭响应
尝试
{
if(this.response!=null)
{
this.response.close();
}
}
捕获(IOEX异常)
{
getLogger(ClientHandler.class.getName()).log(Level.SEVERE,null,ex);
返回服务器错误;
}
}
}
返回状态码;
}
我不知道这个.httpclient
是如何在ClientHandler
类中初始化的,但通常在执行对远离您的主机的请求并使用可重用的http连接时会发生这种情况(这就是为什么第一个请求明显很慢的原因)
public int executeMethod(String type, String path, List<BasicNameValuePair>... nvps)
{
int statusCode = -1;
HttpUriRequest request = createUriRequest(type, path);
this.responseContent = null;
this.response = null;
try
{
if (nvps.length > 0)
{
if (type.equalsIgnoreCase(CommonSettings.POST))
{
((HttpPost) request).setEntity(new UrlEncodedFormEntity(nvps[0], "UTF-8"));
}
else if (type.equalsIgnoreCase(CommonSettings.PUT))
{
((HttpPut) request).setEntity(new UrlEncodedFormEntity(nvps[0], "UTF-8"));
}
else
{
logger.warn("Can only set entity on POST/PUT operation, ignoring nvps");
}
}
}
catch (UnsupportedEncodingException ex)
{
java.util.logging.Logger.getLogger(ClientHandler.class.getName()).log(Level.SEVERE, null, ex);
}
if (this.httpclient != null)
{
try
{
long start = System.currentTimeMillis();
this.response = this.httpclient.execute(request);
long end = System.currentTimeMillis();
long res = end - start;
System.out.println("httpclient.execute " + " seconds: "+res/1000);
statusCode = response.getStatusLine().getStatusCode();
HttpEntity entity = response.getEntity();
if (entity != null)
{
InputStream fis = entity.getContent();
this.responseContent = convertStreamToString(fis);
EntityUtils.consume(entity);
fis.close();
}
}
catch (IOException ex)
{
java.util.logging.Logger.getLogger(ClientHandler.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
return SERVER_ERROR;
}
finally
{
// release connection
if (type.equalsIgnoreCase(CommonSettings.GET))
{
((HttpGet) request).releaseConnection();
}
else if (type.equalsIgnoreCase(CommonSettings.POST))
{
((HttpPost) request).releaseConnection();
}
else if (type.equalsIgnoreCase(CommonSettings.PUT))
{
((HttpPut) request).releaseConnection();
}
else if (type.equalsIgnoreCase(CommonSettings.DELETE))
{
((HttpDelete) request).releaseConnection();
}
// close the response
try
{
if (this.response != null)
{
this.response.close();
}
}
catch (IOException ex)
{
java.util.logging.Logger.getLogger(ClientHandler.class.getName()).log(Level.SEVERE, null, ex);
return SERVER_ERROR;
}
}
}
return statusCode;
}