Java Apache HttpClient到服务器的多轮询连接策略
我正在为服务器设置一个Java客户端,我会定期轮询该服务器,并根据特定的响应向其发送消息。我在课堂上使用的策略如下:Java Apache HttpClient到服务器的多轮询连接策略,java,apache-httpclient-4.x,Java,Apache Httpclient 4.x,我正在为服务器设置一个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 lat
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();
}
}
}
我可以将异常静音,但我想知道发生了什么。我无法通过谷歌找到任何解决方案。我曾经尝试过使用内容,在send方法中创建一个新的HttpPost
对象,诸如此类的东西,但到目前为止没有任何帮助
在这种情况下,什么是好策略。我目前在
HttpPost
对象中设置了keep-alive
标题,以防出现问题。除此之外,我认为我什么都不做。我认为这与总体战略有关。我不想为每个连接创建新对象,但我也不知道建议使用何种级别的重用。谢谢你的帮助。哦这是HttpClient 4.2.2,这些方法不是同步的,您是从不同的线程调用的。当poll()操作正在进行时,您的代码是否可能尝试调用send()?如果您试图使用同一个对象同时发出两个请求,我想HttpClient不会喜欢它
(如果这是问题,微不足道的解决办法是将同步关键字添加到两种方法中,假设您不介意阻塞调用它们的线程。)
这可能是多余的,但是您可能会考虑有一个专用于HTTP连接处理的线程。此线程将一直处于睡眠状态,直到轮询或发送信号。这样做的好处是只使用了一个连接 我最终使用了下面的代码。它是有效的。。。不知道为什么DefaultHttpClient client = new DefaultHttpClient();
ClientConnectionManager mgr = client.getConnectionManager();
HttpParams params = client.getParams();
client = new DefaultHttpClient(new ThreadSafeClientConnManager(params, mgr.getSchemeRegistry()), params);
@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);
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);