gpt4 book ai didi

java - Apache HttpClient 多个轮询连接到服务器的策略

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:56:05 24 4
gpt4 key购买 nike

我正在为我定期轮询并根据特定响应向其发送消息的服务器设置 Java 客户端。我在类里面使用的策略如下:

public class PollingClient {
private HttpClient client = getHttpClient(); // I get a DefaultHttpClient this way so it's easier to add connection manager and strategy etc to the client later
private HttpPost httpPost = getHttpPost(); // same idea, I set headers there

public String poll () {
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
formparams.add(new BasicNameValuePair("id", someId));

String responseString = null;

try {
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(formparams, "UTF-8");
httppost.setURI(polluri));
httppost.setEntity(formEntity);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();


if (entity != null) {
responseString = EntityUtils.toString(entity);
}

EntityUtils.consume(entity);

} catch (Exception e) {
e.printStackTrace();
}
}

public void send(String msg) {
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
formparams.add(new BasicNameValuePair("msg", msg));
formparams.add(new BasicNameValuePair("id", someId));

String responseString = null;

try {
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(formparams, "UTF-8");

httppost.setURI(new URI(URL + "send"));
httppost.setEntity(formEntity);

HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();


if (entity != null) {
responseString = EntityUtils.toString(entity);
}

EntityUtils.consume(entity);

} catch (Exception e) {
e.printStackTrace();
}
}
}

我启动了一个线程,该线程在 3 秒左右进行轮询。我可以根据轮询结果从主线程发送消息。该代码有效,但一直给我以下两个异常(exception),但一直在两者之间工作。

java.lang.IllegalStateException: Invalid use of BasicClientConnManager: connection still allocated.
Make sure to release the connection before allocating another one.

org.apache.http.NoHttpResponseException: The target server failed to respond.

我可以忽略异常,但我想知道发生了什么。我无法通过谷歌找到任何解决方案。我尝试使用内容,在发送方法中创建一个新的 HttpPost 对象,诸如此类,但到目前为止没有任何帮助。

对于这种情况,什么是好的策略。我目前在 HttpPost 对象中设置了 keep-alive header 以防万一。除此之外,我认为我没有做任何事情。我认为这与整体战略有关。我不想为每个连接创建新对象,但我也不知道建议使用何种级别的重用。谢谢你的帮助。哦..这是 HttpClient 4.2.2

最佳答案

@Saad 的回答非常有帮助,但我在尝试对具有许多并发请求的单个 URL 进行负载测试时遇到了延迟。

这是解决问题的更新版本。

    DefaultHttpClient client = new DefaultHttpClient();
ClientConnectionManager mgr = client.getConnectionManager();
HttpParams params = client.getParams();
int maxConnections = 100;
params.setParameter(ConnManagerPNames.MAX_CONNECTIONS_PER_ROUTE, new ConnPerRouteBean(maxConnections));
params.setIntParameter(ConnManagerPNames.MAX_TOTAL_CONNECTIONS, maxConnections);
ThreadSafeClientConnManager conman = new ThreadSafeClientConnManager(params, mgr.getSchemeRegistry());
client = new DefaultHttpClient(conman, params);

关于java - Apache HttpClient 多个轮询连接到服务器的策略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13424975/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com