reactive-programming

Exception handling in rxjava

大憨熊 提交于 2019-12-06 00:48:34
I am trying to get accustomed to rxjava and I am trying to call the below QuoteReader in an Observable. I am not sure how to handle the exception thrown, public class QuoteReader { public Map<String, Object> getQuote() throws IOException{ OkHttpClient okHttpClient = new OkHttpClient(); Request request = new Request.Builder().url("http://quotes.rest/qod.json").build(); Gson gson = new Gson(); Map<String, Object> responseMap = null; try(Response response = okHttpClient.newCall(request).execute()) { responseMap = gson.fromJson(response.body().string(), Map.class); System.out.println("response map

Conditional emission delays with rxjs

匆匆过客 提交于 2019-12-06 00:43:39
From picture to code? How to get the Out observable from Data and Gates? Data is an observable of any kind e.g. JSON objects to be sent to a remote backend Gates is a boolean observable, where the ticks correspond to true and the crosses to false. For example, Internet connectivity whereby true means the network became accessible and false reflects a disconnection. Out is the resulting observable, which emits the same as Data, sometimes immediately, sometimes with a delay, depending on the gate that preceded. For instance, I could subscribe to the Out in order to post the emitted JSON objects

Marble Diagrams [closed]

為{幸葍}努か 提交于 2019-12-05 22:55:41
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 6 years ago . The Marble diagrams are frequently from the Rx team explaining how certain operators in Rx work. Are there any tool to create the marble diagrams? 回答1: I've thought about creating a tool like this a fair bit having created quite a few of them in Visio (look on my blog for examples). I looked around to see if

Spring WebClient call to Rest-Service: Exception from Jaxb2XmlDecoder

戏子无情 提交于 2019-12-05 21:51:42
I'm currentlty struggling with Springs reactive WebClient calling a Rest-Service. I get an UnsupportedOperationException of the decodeToMono -function from Springs Jaxb2XmlDecoder . public Mono<T> decodeToMono(Publisher<DataBuffer> inputStream, ResolvableType elementType, @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) { throw new UnsupportedOperationException(); } My WebClient call: ResponseEntity<MyAsyncResponse> result = webClient.post() .contentType(MediaType.APPLICATION_XML) .syncBody(payload) .exchange() .flatMap(res -> res.toEntity(MyAsyncResponse.class)) .block(); The

waiting IObservable to get all elements error

谁都会走 提交于 2019-12-05 20:58:38
I have this class: public class TestService { public IObservable<int> GetObservable(int max) { var subject = new Subject<int>(); Task.Factory.StartNew(() => { for (int i = 0; i < max; i++) { subject.OnNext(i); } subject.OnCompleted(); }); return subject; } } I wrote a test method for this as well: [TestMethod] public void TestServiceTest1() { var testService = new TestService(); var i = 0; var observable = testService.GetObservable(3); observable.Subscribe(_ => { i++; }); observable.Wait(); Assert.AreEqual(i, 3); } But sometimes I get the error: Sequence contains no elements in method Wait().

Functional reactive operator for custom filter based on another Observable

淺唱寂寞╮ 提交于 2019-12-05 19:50:27
For fun and to learn I'm trying to implement an undo system in my app using functional reactive programming. I have a stream of state changes, which need to be saved onto the undo stack. When the user clicks undo, I take a value from the stack and update application state accordingly. The problem is that this update itself also generates an event in the state change stream. So what I would like is to derive another stream from state changes, which ommits state change right after undo. A simple diagram: states ----S----S----S---- undos -------U----------- save ----S---------S---- The first line

RxJs avoid external state but still access previous values

怎甘沉沦 提交于 2019-12-05 18:47:17
I'm using RxJs to listen to a amqp queu (not really relevant). I have a function createConnection that returns an Observable that emits the new connection object. Once I have a connection, I want to send messages through it every 1000ms and after 10 messages I want to close the connection. I'm trying to avoid external state, but if I don't store the connection in an external variable, how can I close it? See I begin with the connection, then flatMap and push messages, so after a few chains I no longer have the connection object. This is no my flow but imagine something like this:

do Operators instead of a whole Subscriber

烈酒焚心 提交于 2019-12-05 18:31:56
It's quite appealing to use Action (s) instead of a whole Subscriber when you only need OnNext() merely because it's more readable. But of course, errors happen and if you only use Action1 you'll get an Exception in your app. do operators can be of help here. I'm only concerned these two approaches are fully the same, please confirm or disconfirm. Any pitfalls? The first approach: Observable .just(readFromDatabase()) .doOnError(new Action1<Throwable>() { @Override public void call(Throwable throwable) { // handle error } }).subscribe(new Action1<SomeData>() { @Override public void call

RxJava performing operation on a list and returning an observable

二次信任 提交于 2019-12-05 17:57:58
I'm new to RxJava (specifically, RxJava2) and I'm having some trouble with what seems to be a relatively easy operation. I need to get some data from a db, iterate through the data (it is represented as a list), perform an operation on each item, wrap the data in another object and return. This is what I have so far: mDataManager .getStuffList(id) .flatMapIterable(listOfStuff -> listOfStuff) .flatMap(item -> mDataManager .performCount(id, item.getTitle()) .doOnNext(item::setCounter) .takeLast(1) .map(counter -> item)) .toList() .toObservable() .flatMap(listOfStuff -> Observable.just(new

compose() vs. transform() vs. as() vs. map() in Flux and Mono

折月煮酒 提交于 2019-12-05 17:55:35
问题 Recently, I decided to try spring 5 with projectreactor.io (io.projectreactor:3.1.1). Does anyone know what the best case of using this functions? What cons and pros of using each of them and where they should be used? Good examples will be helpful. 回答1: You have two broadly different categories of operators here: Operators that work on the Flux itself transform and compose are for code mutualization When you compose chains of operators regularly and you have common operator usage patterns in