Improving onSchedule hook implementation
See original GitHub issueHi, We are actively using RxJava in multiple of our projects. In some of #the cases I would like to be able to register multiple ScheduleHandler handlers. For example one for the zipkin/brave and another one for our own project related purposes. I’m proposing some changes to the RxJavaPlugins class that will allow to do that without breaking a current API. I can create a PR but here is a patch for now:
import java.lang.Thread.UncaughtExceptionHandler;
import java.util.concurrent.*;
+import java.util.*;
import org.reactivestreams.Subscriber;
import io.reactivex.*;
+import io.reactivex.Observable;
+import io.reactivex.Observer;
import io.reactivex.annotations.*;
import io.reactivex.exceptions.*;
import io.reactivex.flowables.ConnectableFlowable;
@@ -36,7 +39,7 @@
static volatile Consumer<? super Throwable> errorHandler;
@Nullable
- static volatile Function<? super Runnable, ? extends Runnable> onScheduleHandler;
+ static volatile List<Function<? super Runnable, ? extends Runnable>> onScheduleHandler = new ArrayList<Function<? super Runnable, ? extends Runnable>>();
@Nullable
static volatile Function<? super Callable<Scheduler>, ? extends Scheduler> onInitComputationHandler;
@@ -244,7 +247,7 @@
* @return the hook function, may be null
*/
@Nullable
- public static Function<? super Runnable, ? extends Runnable> getScheduleHandler() {
+ public static List<Function<? super Runnable, ? extends Runnable>> getScheduleHandler() {
return onScheduleHandler;
}
@@ -446,11 +449,35 @@
*/
@NonNull
public static Runnable onSchedule(@NonNull Runnable run) {
- Function<? super Runnable, ? extends Runnable> f = onScheduleHandler;
- if (f == null) {
- return run;
+
+ final List<Runnable> scheduledActions = new ArrayList<Runnable>();
+
+ /*
+ * Passing "empty" actions to all handlers but the last one.
+ * The last one should call an original action
+ */
+ Iterator<Function<? super Runnable, ? extends Runnable>> onScheduleIterator = onScheduleHandler.iterator();
+ while (onScheduleIterator.hasNext()) {
+ try {
+ scheduledActions.add(onScheduleIterator.next().apply(onScheduleIterator.hasNext() ? new Runnable() {
+ @Override
+ public void run() {
+ // Does nothing, just a stub method
+ }
+ } : run));
+ } catch (Exception ex) {
+ // Noting to do
+ }
}
- return apply(f, run);
+
+ return new Runnable() {
+ @Override
+ public void run() {
+ for (Runnable innerAction : scheduledActions) {
+ innerAction.run();
+ }
+ }
+ };
}
/**
@@ -606,7 +633,7 @@
if (lockdown) {
throw new IllegalStateException("Plugins can't be changed anymore");
}
- onScheduleHandler = handler;
+ onScheduleHandler.add(handler);
}
/**
Issue Analytics
- State:
- Created 6 years ago
- Comments:14 (6 by maintainers)
Top Results From Across the Web
Flight of the Flux 3 - Hopping Threads and Schedulers - Spring
In this post, we explore the threading model, how some (most) operators are concurrent agnostic, the Scheduler abstraction and how to hop from ......
Read more >How to use JavaScript scheduling methods with React hooks
Let's initialize them using the useState hook. The hook useState returns a pair. First is the current state and second is an updater...
Read more >Copy Reactor Context to MDC in the case of ... - Stack Overflow
I have been following the code at https://github.com/archie-swif/webflux-mdc/blob/master/src/main/java/com/example/webfluxmdc/MdcContextLifter.
Read more >how to fill the implementation gap for inclusive growth: - case ...
The Implementation Gap in Urban Transport Development in Egypt . ... fill gaps and enhance implementation, thus promoting inclusive growth and development ...
Read more >QUALITY IMPROVEMENT STRATEGY PT 2
Using technology to optimize communication between patients and their care team, including using a patient portal in the EHR. Page 4. 4. IMPLEMENTATION...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
You should write a small library that manages multiple handles for you which you can depend on in your bundles.
The proper management of multiple handlers is non trivial as you have to consider concurrent access, ordering of the handlers as well as the infrastructure impact.
Since the scheduler hook is a core hook, it affects all users and leaves not much choice to avoid the overhead when there is no hook present. Your patch adds a lot of overhead even if there are no handles, plus it changes existing
RxJavaPlugins
API which is binary incompatible and not acceptable.Also I’d reconsider using handlers in the first place as their primary purpose is to support testing and diagnostics, not resource-management or -tracking really.
by the way, we’re trying to top-level some functionality to prevent the endless copy/paste https://github.com/openzipkin/brave/pull/665