|
| 1 | +# rxjava-apache-http |
| 2 | + |
| 3 | +Observable API for Apache [HttpAsyncClient](http://hc.apache.org/httpcomponents-asyncclient-dev/) |
| 4 | + |
| 5 | +It is aware of Content-Type `text/event-stream` and will stream each event via `Observer.onNext`. |
| 6 | + |
| 7 | +Other Content-Types will be returned as a single call to `Observer.onNext`. |
| 8 | + |
| 9 | +Main Classes: |
| 10 | + |
| 11 | +- [ObservableHttp](https://github.com/Netflix/RxJava/blob/master/rxjava-contrib/rxjava-apache-http/src/main/java/rx/apache/http/ObservableHttp.java) |
| 12 | +- [ObservableHttpResponse](https://github.com/Netflix/RxJava/blob/master/rxjava-contrib/rxjava-apache-http/src/main/java/rx/apache/http/ObservableHttpResponse.java) |
| 13 | + |
| 14 | + |
| 15 | +# Binaries |
| 16 | + |
| 17 | +Binaries and dependency information for Maven, Ivy, Gradle and others can be found at [http://search.maven.org](http://search.maven.org/#search%7Cga%7C1%7Ccom.netflix.rxjava). |
| 18 | + |
| 19 | +Example for [Maven](http://search.maven.org/#search%7Cga%7C1%7Ca%3A%22rxjava-apache-http%22): |
| 20 | + |
| 21 | +```xml |
| 22 | +<dependency> |
| 23 | + <groupId>com.netflix.rxjava</groupId> |
| 24 | + <artifactId>rxjava-apache-http</artifactId> |
| 25 | + <version>x.y.z</version> |
| 26 | +</dependency> |
| 27 | +``` |
| 28 | + |
| 29 | +and for Ivy: |
| 30 | + |
| 31 | +```xml |
| 32 | +<dependency org="com.netflix.rxjava" name="rxjava-apache-http" rev="x.y.z" /> |
| 33 | +``` |
| 34 | + |
| 35 | +# Sample Usage |
| 36 | + |
| 37 | +### Create a Request |
| 38 | + |
| 39 | +```java |
| 40 | +ObservableHttp.createGet("http://www.wikipedia.com", httpClient).toObservable(); |
| 41 | +ObservableHttp.createRequest(HttpAsyncMethods.createGet("http://www.wikipedia.com"), httpClient).toObservable(); |
| 42 | +``` |
| 43 | + |
| 44 | +### Http Client |
| 45 | + |
| 46 | +A basic default client: |
| 47 | + |
| 48 | +```java |
| 49 | +CloseableHttpAsyncClient httpClient = HttpAsyncClients.createDefault(); |
| 50 | +``` |
| 51 | + |
| 52 | +or a custom client with configuration options: |
| 53 | + |
| 54 | +```java |
| 55 | +final RequestConfig requestConfig = RequestConfig.custom() |
| 56 | + .setSocketTimeout(3000) |
| 57 | + .setConnectTimeout(500).build(); |
| 58 | +final CloseableHttpAsyncClient httpclient = HttpAsyncClients.custom() |
| 59 | + .setDefaultRequestConfig(requestConfig) |
| 60 | + .setMaxConnPerRoute(20) |
| 61 | + .setMaxConnTotal(50) |
| 62 | + .build(); |
| 63 | +``` |
| 64 | + |
| 65 | +### Normal Http GET |
| 66 | + |
| 67 | +Execute a request and transform the `byte[]` reponse to a `String`: |
| 68 | + |
| 69 | +```groovy |
| 70 | + ObservableHttp.createRequest(HttpAsyncMethods.createGet("http://www.wikipedia.com"), client) |
| 71 | + .toObservable() |
| 72 | + .flatMap({ ObservableHttpResponse response -> |
| 73 | + return response.getContent().map({ byte[] bb -> |
| 74 | + return new String(bb); |
| 75 | + }); |
| 76 | + }) |
| 77 | + .toBlockingObservable() |
| 78 | + .forEach({ String resp -> |
| 79 | + // this will be invoked once with the response |
| 80 | + println(resp); |
| 81 | + }); |
| 82 | +``` |
| 83 | + |
| 84 | +### Streaming Http GET with [Server-Sent Events (text/event-stream)](http://www.w3.org/TR/eventsource/) Response |
| 85 | + |
| 86 | +Execute a request and transform the `byte[]` response of each event to a `String`: |
| 87 | + |
| 88 | +```groovy |
| 89 | + ObservableHttp.createRequest(HttpAsyncMethods.createGet("http://hostname/event.stream"), client) |
| 90 | + .toObservable() |
| 91 | + .flatMap({ ObservableHttpResponse response -> |
| 92 | + return response.getContent().map({ byte[] bb -> |
| 93 | + return new String(bb); |
| 94 | + }); |
| 95 | + }) |
| 96 | + .toBlockingObservable() |
| 97 | + .forEach({ String resp -> |
| 98 | + // this will be invoked for each event |
| 99 | + println(resp); |
| 100 | + }); |
| 101 | +``` |
| 102 | + |
| 103 | +An example event-stream is from [Hystrix](https://github.com/Netflix/Hystrix/tree/master/hystrix-contrib/hystrix-metrics-event-stream) used for streaming metrics. An [example webapp](https://github.com/Netflix/Hystrix/tree/master/hystrix-examples-webapp) can be used to test. |
| 104 | + |
| 105 | +Output looks like: |
| 106 | + |
| 107 | +``` |
| 108 | +data: {"type":"HystrixCommand","name":"CreditCardCommand","group":"CreditCard","currentTime":1379823924934,"isCircuitBreakerOpen":false,"errorPercentage":0,"errorCount":0,"requestCount":0,"rollingCountCollapsedRequests":0,"rollingCountExceptionsThrown":0,"rollingCountFailure":0,"rollingCountFallbackFailure":0,"rollingCountFallbackRejection":0,"rollingCountFallbackSuccess":0,"rollingCountResponsesFromCache":0,"rollingCountSemaphoreRejected":0,"rollingCountShortCircuited":0,"rollingCountSuccess":0,"rollingCountThreadPoolRejected":0,"rollingCountTimeout":0,"currentConcurrentExecutionCount":0,"latencyExecute_mean":0,"latencyExecute":{"0":0,"25":0,"50":0,"75":0,"90":0,"95":0,"99":0,"99.5":0,"100":0},"latencyTotal_mean":0,"latencyTotal":{"0":0,"25":0,"50":0,"75":0,"90":0,"95":0,"99":0,"99.5":0,"100":0},"propertyValue_circuitBreakerRequestVolumeThreshold":20,"propertyValue_circuitBreakerSleepWindowInMilliseconds":5000,"propertyValue_circuitBreakerErrorThresholdPercentage":50,"propertyValue_circuitBreakerForceOpen":false,"propertyValue_circuitBreakerForceClosed":false,"propertyValue_circuitBreakerEnabled":true,"propertyValue_executionIsolationStrategy":"THREAD","propertyValue_executionIsolationThreadTimeoutInMilliseconds":3000,"propertyValue_executionIsolationThreadInterruptOnTimeout":true,"propertyValue_executionIsolationThreadPoolKeyOverride":null,"propertyValue_executionIsolationSemaphoreMaxConcurrentRequests":10,"propertyValue_fallbackIsolationSemaphoreMaxConcurrentRequests":10,"propertyValue_metricsRollingStatisticalWindowInMilliseconds":10000,"propertyValue_requestCacheEnabled":true,"propertyValue_requestLogEnabled":true,"reportingHosts":1} |
| 109 | +data: {"type":"HystrixCommand","name":"GetPaymentInformationCommand","group":"PaymentInformation","currentTime":1379823924934,"isCircuitBreakerOpen":false,"errorPercentage":0,"errorCount":0,"requestCount":0,"rollingCountCollapsedRequests":0,"rollingCountExceptionsThrown":0,"rollingCountFailure":0,"rollingCountFallbackFailure":0,"rollingCountFallbackRejection":0,"rollingCountFallbackSuccess":0,"rollingCountResponsesFromCache":0,"rollingCountSemaphoreRejected":0,"rollingCountShortCircuited":0,"rollingCountSuccess":0,"rollingCountThreadPoolRejected":0,"rollingCountTimeout":0,"currentConcurrentExecutionCount":0,"latencyExecute_mean":0,"latencyExecute":{"0":0,"25":0,"50":0,"75":0,"90":0,"95":0,"99":0,"99.5":0,"100":0},"latencyTotal_mean":0,"latencyTotal":{"0":0,"25":0,"50":0,"75":0,"90":0,"95":0,"99":0,"99.5":0,"100":0},"propertyValue_circuitBreakerRequestVolumeThreshold":20,"propertyValue_circuitBreakerSleepWindowInMilliseconds":5000,"propertyValue_circuitBreakerErrorThresholdPercentage":50,"propertyValue_circuitBreakerForceOpen":false,"propertyValue_circuitBreakerForceClosed":false,"propertyValue_circuitBreakerEnabled":true,"propertyValue_executionIsolationStrategy":"THREAD","propertyValue_executionIsolationThreadTimeoutInMilliseconds":1000,"propertyValue_executionIsolationThreadInterruptOnTimeout":true,"propertyValue_executionIsolationThreadPoolKeyOverride":null,"propertyValue_executionIsolationSemaphoreMaxConcurrentRequests":10,"propertyValue_fallbackIsolationSemaphoreMaxConcurrentRequests":10,"propertyValue_metricsRollingStatisticalWindowInMilliseconds":10000,"propertyValue_requestCacheEnabled":true,"propertyValue_requestLogEnabled":true,"reportingHosts":1} |
| 110 | +``` |
| 111 | + |
0 commit comments