我有一个高容量的java应用程序,我必须将http帖子发送到另一台服务器。 目前我正在使用 org.apache.commons.httpclient 库:
private static void sendData(String data) {
HttpClient httpclient = new HttpClient();
StringRequestEntity requestEntity;
try {
requestEntity = new StringRequestEntity(data, "application/json", "UTF-8");
String address = "http://<my host>/events/"
PostMethod postMethod = new PostMethod(address);
postMethod.setRequestEntity(requestEntity);
httpclient.executeMethod(postMethod);
} catch (Exception e) {
LOG.error("Failed to send data ", e);
}
}
这意味着我正在同步发送我的http请求,这不适合我的多线程高容量应用。所以我想将这些调用更改为异步非阻塞http调用。
我经历了apache async client和xsocket等众多选项,但无法使其发挥作用。
尝试ning:
private static void sendEventToGrpahiteAsync(String event) {
LOG.info("\n" + "sendEventToGrpahiteAsync");
try (AsyncHttpClient asyncHttpClient = new AsyncHttpClient()) {
BoundRequestBuilder post = asyncHttpClient.preparePost();
post.addHeader("Content-Type", "application/json");
post.setBodyEncoding("UTF-8");
post.setBody(event);
post.execute(new HttpRequestCompletionHandler());
} catch (Exception e) {
LOG.error("Failed to sending event", e);
}
}
我尝试了Apache HttpAsyncClient:
private static void sendEventToGrpahiteAsync(String event) {
LOG.info("\n" + "sendEventToGrpahiteAsync");
try (CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault()) {
httpclient.start();
HttpPost request = new HttpPost(addr);
StringEntity entity = new StringEntity(event, ContentType.create("application/json", Consts.UTF_8));
request.setEntity(entity);
httpclient.execute(request, null);
} catch (Exception e) {
LOG.error("Failed to sending event", e);
}
}
我尝试了xsocket:
private static void sendEventToGrpahiteAsync2(String event) {
LOG.info("\n" + "sendEventToGrpahiteAsync");
try (INonBlockingConnection con = new NonBlockingConnection(<SERVER_IP>, 80);
IHttpClientEndpoint httpClientConnection = new HttpClientConnection(con)) {
IHttpResponseHandler responseHandler = new MyResponseHandler();
IHttpRequest request = new PostRequest(url_address, "application/json", Consts.UTF_8.toString(), event);
request.setTransferEncoding(Consts.UTF_8.toString());
httpClientConnection.send(request, responseHandler);
} catch (Exception e) {
LOG.error("Failed to sending event", e);
}
}
我没有例外但是帖子也没有到达目标。 要清楚,目标是graphite server,所以一旦帖子到达,就会在图表中清楚地看到。同步帖很好用,我可以在图表上看到结果,但我的目标图上没有显示任何异步帖子。
我错过了什么?
由于
答案 0 :(得分:2)
知道了。
我使用的所有库都是使用额外的IO线程实现的,所以我的进程可能在完全握手之前结束。
一旦我在http调用之后添加了Thread.sleep(2000)就行了。 所以对于一个Web应用程序(这是我的情况)我建议的实现很好(但对于java进程,你可能会考虑NickJ的答案)。
答案 1 :(得分:-1)
您可以使用Java Executor框架:
首先,创建一个Callable来完成你的工作:
public class MyCallable implements Callable<MyResult> {
@Override
public MyResult call() throws Exception {
//do stuff
return result;
}
}
获取将运行Callable的Exectutor。有一种方法可以得到一个,这是一个例子:
ExecutorService executor = Executors.newFixedThreadPool(NTHREDS);
最后,运行它:
MyCallable callable = new MyCallable();
Future<MyResult> futureResult = executor.submit(callable);
获得结果:
boolean resultReady = futureResult.isDone(); //is the result ready yet?
Result r = futureResult.get(); //wait for result and return it
try {
Result r = futureResult.get(10, TimeUnit.SECONDS); //wait max. 10 seconds for result
} catch (TimeOutException e) {
//result still not ready after waiting 10 seconds
}