layout | title | id |
---|---|---|
documentation |
ReactiveX - Plugins |
plugins |
#Plugins
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
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 class RxJavaDefaultSchedulers
and override these methods:
Scheduler getComputationScheduler( )
Scheduler getIOScheduler( )
Scheduler getNewThreadScheduler( )
Then follow these steps:
- Create an object of the new
RxJavaDefaultSchedulers
subclass you have implemented. - Obtain the global
RxJavaPlugins
instance viaRxJavaPlugins.getInstance( )
. - Pass your default schedulers object to the
registerDefaultSchedulers( )
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.
This plugin allows you to register a function that will handle errors that are raised by RxJava but that cannot be handled by the ordinary RxJava onError
notification process (for instance, if RxJava tries to propagate an error to a subscriber that has not implemented an onError
handler). To do this, extend the class RxJavaErrorHandler
and override this method:
void handleError(Throwable e)
Then follow these steps:
- Create an object of the new
RxJavaErrorHandler
subclass you have implemented. - Obtain the global
RxJavaPlugins
instance viaRxJavaPlugins.getInstance( )
. - Pass your error handler object to the
registerErrorHandler( )
method of that instance.
When you do this, RxJava will begin to use your error handler to field errors that cannot be handled in ordinary ways.
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 class RxJavaObservableExecutionHook
and override any or all of these methods:
method | when invoked |
---|---|
onCreate( ) | during Observable.create( ) |
onSubscribeStart( ) | immediately before Observable.subscribe( ) |
onSubscribeReturn( ) | immediately after Observable.subscribe( ) |
onSubscribeError( ) | upon a failed execution of Observable.subscribe( ) |
onLift( ) | during Observable.lift( ) |
Then follow these steps:
- Create an object of the new
RxJavaObservableExecutionHook
subclass you have implemented. - Obtain the global
RxJavaPlugins
instance viaRxJavaPlugins.getInstance( )
. - Pass your execution hooks object to the
registerObservableExecutionHook( )
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.