Skip to content

Commit ec33fb3

Browse files
committed
add SampleApp
1 parent 9c3b491 commit ec33fb3

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.wego.httpcache.samples;
2+
3+
import com.google.inject.AbstractModule;
4+
import com.google.inject.assistedinject.FactoryModuleBuilder;
5+
import com.wego.httpcache.dao.CachedResponseDao;
6+
import com.wego.httpcache.dao.impl.guava.GuavaCachedResponseImpl;
7+
import com.wego.httpcache.services.AsyncHttpCacheService;
8+
import com.wego.httpcache.services.CachedResponseService;
9+
import com.wego.httpcache.services.factories.AsyncHttpCacheServiceFactory;
10+
import com.wego.httpcache.services.impl.AsyncHttpCacheServiceImpl;
11+
import com.wego.httpcache.services.impl.CachedResponseServiceImpl;
12+
13+
public class SampleModule extends AbstractModule {
14+
15+
@Override
16+
protected void configure() {
17+
install(
18+
new FactoryModuleBuilder()
19+
.implement(AsyncHttpCacheService.class, AsyncHttpCacheServiceImpl.class)
20+
.build(AsyncHttpCacheServiceFactory.class));
21+
bind(CachedResponseService.class).to(CachedResponseServiceImpl.class);
22+
bind(CachedResponseDao.class).to(GuavaCachedResponseImpl.class);
23+
}
24+
}

0 commit comments

Comments
 (0)