|
| 1 | +package com.wego.httpcache.samples; |
| 2 | + |
| 3 | +import com.google.inject.Guice; |
| 4 | +import com.google.inject.Injector; |
| 5 | +import com.ning.http.client.AsyncCompletionHandlerBase; |
| 6 | +import com.ning.http.client.AsyncHttpClient; |
| 7 | +import com.ning.http.client.ListenableFuture; |
| 8 | +import com.ning.http.client.Request; |
| 9 | +import com.ning.http.client.RequestBuilder; |
| 10 | +import com.ning.http.client.Response; |
| 11 | +import com.wego.httpcache.services.AsyncHttpCacheService; |
| 12 | +import com.wego.httpcache.services.factories.AsyncHttpCacheServiceFactory; |
| 13 | +import java.io.IOException; |
| 14 | +import java.util.Optional; |
| 15 | + |
| 16 | +public class SampleApp { |
| 17 | + |
| 18 | + public static AsyncHttpCacheService getAsyncHttpCacheService() { |
| 19 | + Injector injector = Guice.createInjector(new SampleModule()); |
| 20 | + |
| 21 | + AsyncHttpCacheServiceFactory asyncHttpCacheServiceFactory = |
| 22 | + injector.getInstance(AsyncHttpCacheServiceFactory.class); |
| 23 | + |
| 24 | + AsyncHttpClient asyncHttpClient = new AsyncHttpClient(); |
| 25 | + return asyncHttpCacheServiceFactory.create(asyncHttpClient, 10000); |
| 26 | + } |
| 27 | + |
| 28 | + public static void main(String[] args) throws Exception { |
| 29 | + AsyncHttpCacheService asyncHttpCacheService = getAsyncHttpCacheService(); |
| 30 | + |
| 31 | + Request request = new RequestBuilder().setMethod("GET").setUrl("http://wego.com").build(); |
| 32 | + |
| 33 | + AsyncCompletionHandlerBase asyncCompletionHandlerBase = |
| 34 | + new AsyncCompletionHandlerBase() { |
| 35 | + @Override |
| 36 | + public Response onCompleted(Response response) throws IOException { |
| 37 | + System.out.print(response.getStatusCode()); |
| 38 | + return response; |
| 39 | + } |
| 40 | + }; |
| 41 | + |
| 42 | + Optional<ListenableFuture<Response>> responseListenableFuture = |
| 43 | + asyncHttpCacheService.executeRequest(request, asyncCompletionHandlerBase); |
| 44 | + |
| 45 | + responseListenableFuture.get().get(); |
| 46 | + } |
| 47 | +} |
0 commit comments