-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#37 : java8 source level, cleanup async bean example (move out the ht…
…tp client code for clarity)
- Loading branch information
Showing
3 changed files
with
170 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
examples/src/main/java/net/sourceforge/stripes/examples/async/AsyncHttpClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package net.sourceforge.stripes.examples.async; | ||
|
||
import org.apache.http.HttpHost; | ||
import org.apache.http.HttpRequest; | ||
import org.apache.http.HttpResponse; | ||
import org.apache.http.client.config.RequestConfig; | ||
import org.apache.http.concurrent.FutureCallback; | ||
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient; | ||
import org.apache.http.impl.nio.client.HttpAsyncClients; | ||
import org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager; | ||
import org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor; | ||
import org.apache.http.message.BasicHttpRequest; | ||
import org.apache.http.nio.reactor.ConnectingIOReactor; | ||
|
||
import java.io.IOException; | ||
import java.util.function.Consumer; | ||
|
||
/** | ||
* Wrapper for non-blocking http client example. Avoids cluttering the action bean's code... | ||
*/ | ||
public class AsyncHttpClient { | ||
|
||
private final CloseableHttpAsyncClient asyncClient; | ||
private final HttpHost host; | ||
|
||
public AsyncHttpClient(HttpHost host) { | ||
this.host = host; | ||
try { | ||
RequestConfig requestConfig = RequestConfig.custom() | ||
.setSocketTimeout(10000) | ||
.setConnectTimeout(10000).build(); | ||
ConnectingIOReactor ioReactor = new DefaultConnectingIOReactor(); | ||
PoolingNHttpClientConnectionManager cm = | ||
new PoolingNHttpClientConnectionManager(ioReactor); | ||
asyncClient = HttpAsyncClients.custom() | ||
.setDefaultRequestConfig(requestConfig) | ||
.setConnectionManager(cm) | ||
.build(); | ||
asyncClient.start(); | ||
} catch(Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
private void close() { | ||
try { | ||
asyncClient.close(); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
public AsyncRequest buildRequest(String uri) { | ||
return new AsyncRequest(uri); | ||
} | ||
|
||
public class AsyncRequest { | ||
|
||
private final String uri; | ||
|
||
private Consumer<HttpResponse> onCompleted; | ||
private Consumer<Exception> onFailed; | ||
private Runnable onCancelled; | ||
|
||
private AsyncRequest(String uri) { | ||
this.uri = uri; | ||
} | ||
|
||
public AsyncRequest completed(Consumer<HttpResponse> c) { | ||
onCompleted = c; | ||
return this; | ||
} | ||
|
||
public AsyncRequest failed(Consumer<Exception> e) { | ||
onFailed = e; | ||
return this; | ||
} | ||
|
||
public AsyncRequest cancelled(Runnable r) { | ||
onCancelled = r; | ||
return this; | ||
} | ||
|
||
public void get() { | ||
HttpRequest r = new BasicHttpRequest("GET", uri); | ||
asyncClient.execute(host, r, new FutureCallback<HttpResponse>() { | ||
public void completed(HttpResponse result) { | ||
if (onCompleted != null) { | ||
onCompleted.accept(result); | ||
} | ||
close(); | ||
} | ||
|
||
public void failed(Exception ex) { | ||
if (onFailed != null) { | ||
onFailed.accept(ex); | ||
} | ||
close(); | ||
} | ||
|
||
public void cancelled() { | ||
if (onCancelled != null) { | ||
onCancelled.run(); | ||
} | ||
close(); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters