Java_MVVM_with_Swing_and_RxJava_Examples
Java_MVVM_with_Swing_and_RxJava_Examples copied to clipboard
Unsubscription due to missing exception handling (e.g. binders) -> application gets unresponsive
When a subscriber throws an exception during onNext, the subscriber will be unsubscribed and will not receive any more values (RxJava 1.x).
Example code:
private void wireInternally() {
v2vm_submitButtonEvents
.map(actionEvent -> new NameFirstname(v2vm_name.getValue(), v2vm_firstname.getValue()))
.subscribe(vm2m_nameFirstname);
}
Let's change this code to "throw up sometimes":
private final AtomicInteger counter= new AtomicInteger(0);
private void wireInternally() {
v2vm_submitButtonEvents
.map(actionEvent -> {
if (counter.incrementAndGet() == 3) {
throw new RuntimeException("boom");
}
return new NameFirstname(v2vm_name.getValue(), v2vm_firstname.getValue());
})
.subscribe(vm2m_nameFirstname);
}
As soon as we reach counter value 3
, the "flow" will automatically stop. The map
function is automatically unsubscribed from v2vm_submitButtonEvents
.
The application doesn't respond anymore and does not recover.
This is kind of horrible, because the chance to see those issues is high and you will probably discover them very late (in production).
Possible workarounds:
- add the
retry
operator "somewhere" - add try/catch blocks
Needs more investigation for a clean solution.
@mikebaum is this the kind of issue you talked about? It concerns not only the subjects, it concerns everything in RxJava, especially all operators.
@Petikoch Sorry for the delay, I must admit I missed your email. Well the issues I was experiencing was that whenever one BehaviourSubject emits an error the subscription to a linked BehaviourSubject would be broken. I suppose this is what you're describing here. For me the solution was to never subscribe to onError when binding subjects. This is why I feel that BehaviourSubjects and specifically the onError message makes no sense when using Rx in the UI.