Skip to content

Commit 0d83c94

Browse files
Merge pull request #846 from johanhaleby/master
Added overloaded createRequest method that takes an HttpContext instance
2 parents 3d3894f + 9e35276 commit 0d83c94

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

rxjava-contrib/rxjava-apache-http/src/main/java/rx/apache/http/ObservableHttp.java

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import org.apache.http.nio.client.HttpAsyncClient;
2222
import org.apache.http.nio.client.methods.HttpAsyncMethods;
2323
import org.apache.http.nio.protocol.HttpAsyncRequestProducer;
24+
import org.apache.http.protocol.Http.HttpContext;
25+
import org.apache.http.protocol.BasicHttpContext;
2426

2527
import rx.Observable;
2628
import rx.Observable.OnSubscribeFunc;
@@ -134,6 +136,42 @@ public static ObservableHttp<ObservableHttpResponse> createGet(String uri, final
134136
* @return
135137
*/
136138
public static ObservableHttp<ObservableHttpResponse> createRequest(final HttpAsyncRequestProducer requestProducer, final HttpAsyncClient client) {
139+
return createRequest(requestProducer, client, new BasicHttpContext());
140+
}
141+
142+
/**
143+
* Execute request using {@link HttpAsyncRequestProducer} to define HTTP Method, URI and payload (if applicable).
144+
* <p>
145+
* If the response is chunked (or flushed progressively such as with <i>text/event-stream</i> <a href="http://www.w3.org/TR/2009/WD-eventsource-20091029/">Server-Sent Events</a>) this will call
146+
* {@link Observer#onNext} multiple times.
147+
* <p>
148+
* Use {@code HttpAsyncMethods.create* } factory methods to create {@link HttpAsyncRequestProducer} instances.
149+
* <p>
150+
* A client can be retrieved like this:
151+
* <p>
152+
* <pre> {@code CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault(); } </pre>
153+
* <p>
154+
* A client with custom configurations can be created like this:
155+
* </p>
156+
* <pre> {@code
157+
* final RequestConfig requestConfig = RequestConfig.custom()
158+
* .setSocketTimeout(3000)
159+
* .setConnectTimeout(3000).build();
160+
* final CloseableHttpAsyncClient httpclient = HttpAsyncClients.custom()
161+
* .setDefaultRequestConfig(requestConfig)
162+
* .setMaxConnPerRoute(20)
163+
* .setMaxConnTotal(50)
164+
* .build();
165+
* httpclient.start();
166+
* }</pre>
167+
*
168+
*
169+
* @param requestProducer
170+
* @param client
171+
* @param context The HttpContext
172+
* @return
173+
*/
174+
public static ObservableHttp<ObservableHttpResponse> createRequest(final HttpAsyncRequestProducer requestProducer, final HttpAsyncClient client, final HttpContext context) {
137175

138176
return ObservableHttp.create(new OnSubscribeFunc<ObservableHttpResponse>() {
139177

@@ -144,7 +182,7 @@ public Subscription onSubscribe(final Observer<? super ObservableHttpResponse> o
144182

145183
// return a Subscription that wraps the Future so it can be cancelled
146184
parentSubscription.add(Subscriptions.from(client.execute(requestProducer, new ResponseConsumerDelegate(observer, parentSubscription),
147-
new FutureCallback<HttpResponse>() {
185+
context, new FutureCallback<HttpResponse>() {
148186

149187
@Override
150188
public void completed(HttpResponse result) {

0 commit comments

Comments
 (0)