private void send(List<ALF> alfs) throws IllegalStateException, IOException, InterruptedException, ExecutionException, TimeoutException {
String data = config.getGson().toJson(alfs);
LOGGER.debug(String.format("sending batch of (%d) ALFs", alfs.size()));
HttpPost post = new HttpPost(config.getAnalyticsServerURI());
post.addHeader(HTTP.CONTENT_TYPE, ContentType.APPLICATION_JSON.toString());
post.setEntity(new StringEntity(data, "UTF-8"));
HttpRequestFutureTask<Boolean> futureTask = config.getFutureRequestExecutor().execute(post, null, new AnalyticsResponseHandler(), new AnalyticsResponseCallback());
post.releaseConnection();
}
java类org.apache.http.impl.client.HttpRequestFutureTask的实例源码
AnalyticsBatchRequest.java 文件源码
项目:galileo-agent-java
阅读 16
收藏 0
点赞 0
评论 0
ClientWithRequestFuture.java 文件源码
项目:purecloud-iot
阅读 22
收藏 0
点赞 0
评论 0
public static void main(String[] args) throws Exception {
// the simplest way to create a HttpAsyncClientWithFuture
HttpClient httpclient = HttpClientBuilder.create()
.setMaxConnPerRoute(5)
.setMaxConnTotal(5).build();
ExecutorService execService = Executors.newFixedThreadPool(5);
FutureRequestExecutionService requestExecService = new FutureRequestExecutionService(
httpclient, execService);
try {
// Because things are asynchronous, you must provide a ResponseHandler
ResponseHandler<Boolean> handler = new ResponseHandler<Boolean>() {
@Override
public Boolean handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
// simply return true if the status was OK
return response.getStatusLine().getStatusCode() == 200;
}
};
// Simple request ...
HttpGet request1 = new HttpGet("http://httpbin.org/get");
HttpRequestFutureTask<Boolean> futureTask1 = requestExecService.execute(request1,
HttpClientContext.create(), handler);
Boolean wasItOk1 = futureTask1.get();
System.out.println("It was ok? " + wasItOk1);
// Cancel a request
try {
HttpGet request2 = new HttpGet("http://httpbin.org/get");
HttpRequestFutureTask<Boolean> futureTask2 = requestExecService.execute(request2,
HttpClientContext.create(), handler);
futureTask2.cancel(true);
Boolean wasItOk2 = futureTask2.get();
System.out.println("It was cancelled so it should never print this: " + wasItOk2);
} catch (CancellationException e) {
System.out.println("We cancelled it, so this is expected");
}
// Request with a timeout
HttpGet request3 = new HttpGet("http://httpbin.org/get");
HttpRequestFutureTask<Boolean> futureTask3 = requestExecService.execute(request3,
HttpClientContext.create(), handler);
Boolean wasItOk3 = futureTask3.get(10, TimeUnit.SECONDS);
System.out.println("It was ok? " + wasItOk3);
FutureCallback<Boolean> callback = new FutureCallback<Boolean>() {
@Override
public void completed(Boolean result) {
System.out.println("completed with " + result);
}
@Override
public void failed(Exception ex) {
System.out.println("failed with " + ex.getMessage());
}
@Override
public void cancelled() {
System.out.println("cancelled");
}
};
// Simple request with a callback
HttpGet request4 = new HttpGet("http://httpbin.org/get");
// using a null HttpContext here since it is optional
// the callback will be called when the task completes, fails, or is cancelled
HttpRequestFutureTask<Boolean> futureTask4 = requestExecService.execute(request4,
HttpClientContext.create(), handler, callback);
Boolean wasItOk4 = futureTask4.get(10, TimeUnit.SECONDS);
System.out.println("It was ok? " + wasItOk4);
} finally {
requestExecService.close();
}
}
ClientWithRequestFuture.java 文件源码
项目:Analogy-and-Assessment
阅读 18
收藏 0
点赞 0
评论 0
public static void main(String[] args) throws Exception {
// the simplest way to create a HttpAsyncClientWithFuture
HttpClient httpclient = HttpClientBuilder.create()
.setMaxConnPerRoute(5)
.setMaxConnTotal(5).build();
ExecutorService execService = Executors.newFixedThreadPool(5);
FutureRequestExecutionService requestExecService = new FutureRequestExecutionService(
httpclient, execService);
try {
// Because things are asynchronous, you must provide a ResponseHandler
ResponseHandler<Boolean> handler = new ResponseHandler<Boolean>() {
public Boolean handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
// simply return true if the status was OK
return response.getStatusLine().getStatusCode() == 200;
}
};
// Simple request ...
HttpGet request1 = new HttpGet("http://google.com");
HttpRequestFutureTask<Boolean> futureTask1 = requestExecService.execute(request1,
HttpClientContext.create(), handler);
Boolean wasItOk1 = futureTask1.get();
System.out.println("It was ok? " + wasItOk1);
// Cancel a request
try {
HttpGet request2 = new HttpGet("http://google.com");
HttpRequestFutureTask<Boolean> futureTask2 = requestExecService.execute(request2,
HttpClientContext.create(), handler);
futureTask2.cancel(true);
Boolean wasItOk2 = futureTask2.get();
System.out.println("It was cancelled so it should never print this: " + wasItOk2);
} catch (CancellationException e) {
System.out.println("We cancelled it, so this is expected");
}
// Request with a timeout
HttpGet request3 = new HttpGet("http://google.com");
HttpRequestFutureTask<Boolean> futureTask3 = requestExecService.execute(request3,
HttpClientContext.create(), handler);
Boolean wasItOk3 = futureTask3.get(10, TimeUnit.SECONDS);
System.out.println("It was ok? " + wasItOk3);
FutureCallback<Boolean> callback = new FutureCallback<Boolean>() {
public void completed(Boolean result) {
System.out.println("completed with " + result);
}
public void failed(Exception ex) {
System.out.println("failed with " + ex.getMessage());
}
public void cancelled() {
System.out.println("cancelled");
}
};
// Simple request with a callback
HttpGet request4 = new HttpGet("http://google.com");
// using a null HttpContext here since it is optional
// the callback will be called when the task completes, fails, or is cancelled
HttpRequestFutureTask<Boolean> futureTask4 = requestExecService.execute(request4,
HttpClientContext.create(), handler, callback);
Boolean wasItOk4 = futureTask4.get(10, TimeUnit.SECONDS);
System.out.println("It was ok? " + wasItOk4);
} finally {
requestExecService.close();
}
}
ClientWithRequestFuture.java 文件源码
项目:Raven
阅读 22
收藏 0
点赞 0
评论 0
public static void main(String[] args) throws Exception {
// the simplest way to create a HttpAsyncClientWithFuture
HttpClient httpclient = HttpClientBuilder.create()
.setMaxConnPerRoute(5)
.setMaxConnTotal(5).build();
ExecutorService execService = Executors.newFixedThreadPool(5);
FutureRequestExecutionService requestExecService = new FutureRequestExecutionService(
httpclient, execService);
try {
// Because things are asynchronous, you must provide a ResponseHandler
ResponseHandler<Boolean> handler = new ResponseHandler<Boolean>() {
public Boolean handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
// simply return true if the status was OK
return response.getStatusLine().getStatusCode() == 200;
}
};
// Simple request ...
HttpGet request1 = new HttpGet("http://google.com");
HttpRequestFutureTask<Boolean> futureTask1 = requestExecService.execute(request1,
HttpClientContext.create(), handler);
Boolean wasItOk1 = futureTask1.get();
System.out.println("It was ok? " + wasItOk1);
// Cancel a request
try {
HttpGet request2 = new HttpGet("http://google.com");
HttpRequestFutureTask<Boolean> futureTask2 = requestExecService.execute(request2,
HttpClientContext.create(), handler);
futureTask2.cancel(true);
Boolean wasItOk2 = futureTask2.get();
System.out.println("It was cancelled so it should never print this: " + wasItOk2);
} catch (CancellationException e) {
System.out.println("We cancelled it, so this is expected");
}
// Request with a timeout
HttpGet request3 = new HttpGet("http://google.com");
HttpRequestFutureTask<Boolean> futureTask3 = requestExecService.execute(request3,
HttpClientContext.create(), handler);
Boolean wasItOk3 = futureTask3.get(10, TimeUnit.SECONDS);
System.out.println("It was ok? " + wasItOk3);
FutureCallback<Boolean> callback = new FutureCallback<Boolean>() {
public void completed(Boolean result) {
System.out.println("completed with " + result);
}
public void failed(Exception ex) {
System.out.println("failed with " + ex.getMessage());
}
public void cancelled() {
System.out.println("cancelled");
}
};
// Simple request with a callback
HttpGet request4 = new HttpGet("http://google.com");
// using a null HttpContext here since it is optional
// the callback will be called when the task completes, fails, or is cancelled
HttpRequestFutureTask<Boolean> futureTask4 = requestExecService.execute(request4,
HttpClientContext.create(), handler, callback);
Boolean wasItOk4 = futureTask4.get(10, TimeUnit.SECONDS);
System.out.println("It was ok? " + wasItOk4);
} finally {
requestExecService.close();
}
}
ClientWithRequestFuture.java 文件源码
项目:uw-android
阅读 23
收藏 0
点赞 0
评论 0
public static void main(String[] args) throws Exception {
// the simplest way to create a HttpAsyncClientWithFuture
HttpClient httpclient = HttpClientBuilder.create()
.setMaxConnPerRoute(5)
.setMaxConnTotal(5).build();
ExecutorService execService = Executors.newFixedThreadPool(5);
FutureRequestExecutionService requestExecService = new FutureRequestExecutionService(
httpclient, execService);
try {
// Because things are asynchronous, you must provide a ResponseHandler
ResponseHandler<Boolean> handler = new ResponseHandler<Boolean>() {
@Override
public Boolean handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
// simply return true if the status was OK
return response.getStatusLine().getStatusCode() == 200;
}
};
// Simple request ...
HttpGet request1 = new HttpGet("http://google.com");
HttpRequestFutureTask<Boolean> futureTask1 = requestExecService.execute(request1,
HttpClientContext.create(), handler);
Boolean wasItOk1 = futureTask1.get();
System.out.println("It was ok? " + wasItOk1);
// Cancel a request
try {
HttpGet request2 = new HttpGet("http://google.com");
HttpRequestFutureTask<Boolean> futureTask2 = requestExecService.execute(request2,
HttpClientContext.create(), handler);
futureTask2.cancel(true);
Boolean wasItOk2 = futureTask2.get();
System.out.println("It was cancelled so it should never print this: " + wasItOk2);
} catch (CancellationException e) {
System.out.println("We cancelled it, so this is expected");
}
// Request with a timeout
HttpGet request3 = new HttpGet("http://google.com");
HttpRequestFutureTask<Boolean> futureTask3 = requestExecService.execute(request3,
HttpClientContext.create(), handler);
Boolean wasItOk3 = futureTask3.get(10, TimeUnit.SECONDS);
System.out.println("It was ok? " + wasItOk3);
FutureCallback<Boolean> callback = new FutureCallback<Boolean>() {
@Override
public void completed(Boolean result) {
System.out.println("completed with " + result);
}
@Override
public void failed(Exception ex) {
System.out.println("failed with " + ex.getMessage());
}
@Override
public void cancelled() {
System.out.println("cancelled");
}
};
// Simple request with a callback
HttpGet request4 = new HttpGet("http://google.com");
// using a null HttpContext here since it is optional
// the callback will be called when the task completes, fails, or is cancelled
HttpRequestFutureTask<Boolean> futureTask4 = requestExecService.execute(request4,
HttpClientContext.create(), handler, callback);
Boolean wasItOk4 = futureTask4.get(10, TimeUnit.SECONDS);
System.out.println("It was ok? " + wasItOk4);
} finally {
requestExecService.close();
}
}