Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
David Karnok edited this pageFeb 5, 2018 ·13 revisions

Plugins allow you to modify the default behavior of RxJava in several respects:

  • by changing the set of default computation, i/o, and new thread Schedulers
  • by registering a handler for extraordinary errors that RxJava may encounter
  • by registering functions that can take note of the occurrence of several regular RxJava activities

As of 1.1.7 the regularRxJavaPlugins and the other hook classes have been deprecated in favor ofRxJavaHooks.

RxJavaHooks

The newRxJavaHooks allows you to hook into the lifecycle of theObservable,Single andCompletable types, theSchedulers returned bySchedulers and offers a catch-all for undeliverable errors.

You can now change these hooks at runtime and there is no need to prepare hooks via system parameters anymore. Since users may still rely on the old hooking system, RxJavaHooks delegates to those old hooks by default.

TheRxJavaHooks has setters and getters of the various hook types:

HookDescription
onError :Action1<Throwable>Sets the catch-all callback
onObservableCreate :Func1<Observable.OnSubscribe, Observable.OnSubscribe>Called when operators and sources are instantiated onObservable
onObservableStart :Func2<Observable, Observable.OnSubscribe, Observable.OnSubscribe>Called before subscribing to anObservable actually happens
onObservableSubscribeError :Func1<Throwable, Throwable>Called when subscribing to anObservable fails
onObservableReturn :Func1<Subscription, Subscription>Called when the subscribing to anObservable succeeds and before returning theSubscription handler for it
onObservableLift :Func1<Observable.Operator, Observable.Operator>Called when the operatorlift is used withObservable
onSingleCreate :Func1<Single.OnSubscribe, Single.OnSubscribe>Called when operators and sources are instantiated onSingle
onSingleStart :Func2<Single, Observable.OnSubscribe, Observable.OnSubscribe>Called before subscribing to aSingle actually happens
onSingleSubscribeError :Func1<Throwable, Throwable>Called when subscribing to aSingle fails
onSingleReturn :Func1<Subscription, Subscription>Called when the subscribing to aSingle succeeds and before returning theSubscription handler for it
onSingleLift :Func1<Observable.Operator, Observable.Operator>Called when the operatorlift is used (note:Observable.Operator is deliberate here)
onCompletableCreate :Func1<Completable.OnSubscribe, Completable.OnSubscribe>Called when operators and sources are instantiated onCompletable
onCompletableStart :Func2<Completable, Completable.OnSubscribe, Completable.OnSubscribe>Called before subscribing to aCompletable actually happens
onCompletableSubscribeError :Func1<Throwable, Throwable>Called when subscribing to aCompletable fails
onCompletableLift :Func1<Completable.Operator, Completable.Operator>Called when the operatorlift is used withCompletable
onComputationScheduler :Func1<Scheduler, Scheduler>Called when usingSchedulers.computation()
onIOScheduler :Func1<Scheduler, Scheduler>Called when usingSchedulers.io()
onNewThreadScheduler :Func1<Scheduler, Scheduler>Called when usingSchedulers.newThread()
onScheduleAction :Func1<Action0, Action0>Called when a task gets scheduled in any of theSchedulers
onGenericScheduledExecutorService :Func0<ScheduledExecutorService>that should return single-threaded executors to support background timed tasks of RxJava itself

Reading and changing these hooks is thread-safe.

You can also clear all hooks viaclear() or reset to the default behavior (of delegating to the old RxJavaPlugins system) viareset().

Example:

RxJavaHooks.setOnObservableCreate(o -> {System.out.println("Creating " +o.getClass());returno; });try {Observable.range(1,10)    .map(v ->v *2)    .filter(v ->v %4 ==0)    .subscribe(System.out::println);}finally {RxJavaHooks.reset();}

In addition, theRxJavaHooks offers the so-called assembly tracking feature. This shims a customObservable,Single andCompletable into their chains which captures the current stacktrace when those operators were instantiated (assembly-time). Whenever an error is signalled via onError, these middle components attach this assembly-time stacktraces as last causes of that exception. This may help locating the problematic sequence in a codebase where there are too many similar flows and the plain exception itself doesn't tell which one failed in your codebase.

Example:

RxJavaHooks.enableAssemblyTracking();try {Observable.empty().single()    .subscribe(System.out::println,Throwable::printStackTrace);}finally {RxJavaHooks.resetAssemblyTracking();}

This will print something like this:

java.lang.NoSuchElementExceptionat rx.internal.operators.OnSubscribeSingle(OnSubscribeSingle.java:57)...Assembly trace:at com.example.TrackingExample(TrackingExample:10)

The stacktrace string is also available in a field to support debugging and discovering the status of various operators in a running chain.

The stacktrace is filtered by removing irrelevant entries such as Thread entry points, unit test runners and the entries of the tracking system itself to reduce noise.

RxJavaSchedulersHook

Deprecated

This plugin allows you to override the default computation, i/o, and new thread Schedulers with Schedulers of your choosing. To do this, extend the classRxJavaSchedulersHook and override these methods:

  • Scheduler getComputationScheduler( )
  • Scheduler getIOScheduler( )
  • Scheduler getNewThreadScheduler( )
  • Action0 onSchedule(action)

Then follow these steps:

  1. Create an object of the newRxJavaDefaultSchedulers subclass you have implemented.
  2. Obtain the globalRxJavaPlugins instance viaRxJavaPlugins.getInstance( ).
  3. Pass your default schedulers object to theregisterSchedulersHook( ) method of that instance.

When you do this, RxJava will begin to use the Schedulers returned by your methods rather than its built-in defaults.

RxJavaErrorHandler

Deprecated

This plugin allows you to register a function that will handle errors that are passed toSafeSubscriber.onError(Throwable). (SafeSubscriber is used for wrapping the incomingSubscriber when one callssubscribe()). To do this, extend the classRxJavaErrorHandler and override this method:

  • void handleError(Throwable e)

Then follow these steps:

  1. Create an object of the newRxJavaErrorHandler subclass you have implemented.
  2. Obtain the globalRxJavaPlugins instance viaRxJavaPlugins.getInstance( ).
  3. Pass your error handler object to theregisterErrorHandler( ) method of that instance.

When you do this, RxJava will begin to use your error handler to field errors that are passed toSafeSubscriber.onError(Throwable).

For example, this will call the hook:

RxJavaPlugins.getInstance().reset();RxJavaPlugins.getInstance().registerErrorHandler(newRxJavaErrorHandler() {@OverridepublicvoidhandleError(Throwablee) {e.printStackTrace();    }});Observable.error(newIOException()).subscribe(System.out::println,e -> { });

however, this call and chained operators in general won't trigger it in each stage:

Observable.error(newIOException()).map(v ->"" +v).unsafeSubscribe(System.out::println,e -> { });

RxJavaObservableExecutionHook

Deprecated

This plugin allows you to register functions that RxJava will call upon certain regular RxJava activities, for instance for logging or metrics-collection purposes. To do this, extend the classRxJavaObservableExecutionHook and override any or all of these methods:

methodwhen invoked
onCreate( )duringObservable.create( )
onSubscribeStart( )immediately beforeObservable.subscribe( )
onSubscribeReturn( )immediately afterObservable.subscribe( )
onSubscribeError( )upon a failed execution ofObservable.subscribe( )
onLift( )duringObservable.lift( )

Then follow these steps:

  1. Create an object of the newRxJavaObservableExecutionHook subclass you have implemented.
  2. Obtain the globalRxJavaPlugins instance viaRxJavaPlugins.getInstance( ).
  3. Pass your execution hooks object to theregisterObservableExecutionHook( ) method of that instance.

When you do this, RxJava will begin to call your functions when it encounters the specific conditions they were designed to take note of.

Copyright (c) 2016-present, RxJava Contributors.
Twitter @RxJava |Gitter @RxJava

Clone this wiki locally


[8]ページ先頭

©2009-2025 Movatter.jp