在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:ReactiveX/RxApacheHttp开源软件地址:https://github.com/ReactiveX/RxApacheHttp开源编程语言:Java 98.6%开源软件介绍:rxjava-apache-httpObservable API for Apache HttpAsyncClient It is aware of Content-Type Other Content-Types will be returned as a single call to Main Classes: BinariesBinaries and dependency information for Maven, Ivy, Gradle and others can be found at http://search.maven.org. Example for Maven: <dependency>
<groupId>com.netflix.rxjava</groupId>
<artifactId>rxjava-apache-http</artifactId>
<version>x.y.z</version>
</dependency> and for Ivy: <dependency org="com.netflix.rxjava" name="rxjava-apache-http" rev="x.y.z" /> Sample UsageCreate a RequestObservableHttp.createGet("http://www.wikipedia.com", httpClient).toObservable();
ObservableHttp.createRequest(HttpAsyncMethods.createGet("http://www.wikipedia.com"), httpClient).toObservable(); Http ClientA basic default client: CloseableHttpAsyncClient httpClient = HttpAsyncClients.createDefault(); or a custom client with configuration options: final RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(3000)
.setConnectTimeout(500).build();
final CloseableHttpAsyncClient httpclient = HttpAsyncClients.custom()
.setDefaultRequestConfig(requestConfig)
.setMaxConnPerRoute(20)
.setMaxConnTotal(50)
.build(); Normal Http GETExecute a request and transform the ObservableHttp.createRequest(HttpAsyncMethods.createGet("http://www.wikipedia.com"), client)
.toObservable()
.flatMap({ ObservableHttpResponse response ->
return response.getContent().map({ byte[] bb ->
return new String(bb);
});
})
.toBlockingObservable()
.forEach({ String resp ->
// this will be invoked once with the response
println(resp);
}); Server-Sent Events (text/event-stream) ResponseStreaming Http GET withExecute a request and transform the ObservableHttp.createRequest(HttpAsyncMethods.createGet("http://hostname/event.stream"), client)
.toObservable()
.flatMap({ ObservableHttpResponse response ->
return response.getContent().map({ byte[] bb ->
return new String(bb);
});
})
.toBlockingObservable()
.forEach({ String resp ->
// this will be invoked for each event
println(resp);
}); An example event-stream is from Hystrix used for streaming metrics. An example webapp can be used to test. Output looks like:
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论