SlideShare a Scribd company logo
RxJava2
Should I use it?
RxJava2
1. What’s new?
2. Performance
3. Libraries compatibility
4. RxJava1 compatibility
5. Summary
What’s new?
● Reactive Streams compatibility
● Observable types
● Null handling
● Backpressure handling
● Testing
Reactive Streams
“Reactive Streams is an initiative to provide a standard for asynchronous
stream processing with non-blocking back pressure. This encompasses efforts
aimed at runtime environments (JVM and JavaScript) as well as network
protocols.”
Reactive Streams
public interface Publisher<T> {
public void subscribe(Subscriber<? super T> s);
}
public interface Subscriber< T> {
public void onSubscribe (Subscription s) ;
public void onNext(T t);
public void onError(Throwable t) ;
public void onComplete();
}
Reactive Streams
public interface Subscription {
public void request(long n);
public void cancel();
}
public interface Processor<T, R> extends Subscriber<T>, Publisher<R> {
}
Observable types
Type Description
Flowable<T> Emits 0 or n items and terminates with complete or an
error. Supports backpressure, which allows to control
how fast a source emits items.
Observable<T> Emits 0 or n items and terminates with complete or an
error.
Single<T> Emits either a single item or an error. The reactive
version of a method call. You subscribe to a Single and
you get either a return value or an error.
Maybe<T> Succeeds with an item, or no item, or errors. The
reactive version of an Optional.
Completable Either completes or returns an error. It never return
items. The reactive version of a Runnable.
Null handling
Observable.just(null);
Single.just(null);
Observable.fromCallable(() -> null)
.subscribe(System. out::println, Throwable::printStackTrace) ;
Observable.just(1).map(v -> null)
.subscribe(System. out::println, Throwable::printStackTrace) ;
Null handling
Solution:
enum Irrelevant { INSTANCE }
Single.just(1).map(v -> Irrelevant. INSTANCE);
Backpressure handling
private Flowable<Integer> createFlowable (int items,
BackpressureStrategy backpressureStrategy) {
return Flowable.create(subscriber -> {
for (int i = 0; i < items; i++) {
if (subscriber.isCancelled()) {
return;
}
subscriber.onNext(i) ;
}
subscriber.onComplete() ;
}, backpressureStrategy) ;
}
Backpressure handling
● BackpressureStrategy.BUFFER
● BackpressureStrategy.DROP
● BackpressureStrategy.LATEST
● BackpressureStrategy.ERROR
● BackpressureStrategy.MISSING
Testing
@Test
public void testUsingTestObserver () {
//given
TestObserver<String> observer = new TestObserver<>() ;
Observable<String> observable = Observable. fromIterable(WORDS)
.zipWith(Observable. range(1, Integer.MAX_VALUE),
(string, index) -> String. format("%2d. %s", index, string));
//when
observable.subscribe(observer) ;
//then
observer.assertComplete() ;
observer.assertNoErrors() ;
observer.assertValueCount( 9);
assertThat(observer.values() , hasItem(" 4. fox"));
}
Testing
//given
TestObserver<String> observer = new TestObserver<>() ;
Observable<String> observable = Observable. fromIterable(WORDS)
.zipWith(Observable. range(1, Integer.MAX_VALUE),
(string, index) -> String. format("%2d. %s", index, string))
.subscribeOn(Schedulers. computation());
//when
observable.subscribe(observer) ;
//then
observer.assertComplete() ;
observer.assertNoErrors() ;
observer.assertValueCount( 4);
assertThat(observer.values() , hasItem(" 4. fox"));
Testing
public class ImmediateSchedulerRule implements TestRule {
@Override
public Statement apply(final Statement base , Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
RxJavaPlugins. setIoSchedulerHandler(build(Schedulers. trampoline()));
RxJavaPlugins. setComputationSchedulerHandler(build(Schedulers. trampoline()));
RxJavaPlugins. setNewThreadSchedulerHandler(build(Schedulers. trampoline()));
try {
base.evaluate() ;
} finally {
RxJavaPlugins. reset();
}
}
};
Testing
@Rule
public final ImmediateSchedulerRule testSchedulerRule
= new ImmediateSchedulerRule() ;
Testing
@Test
public void testUsingTestObserver () {
//given
TestObserver<String> observer ;
Observable<String> observable = Observable. fromIterable(WORDS)
.zipWith(Observable. range(1, Integer.MAX_VALUE),
(string, index) -> String. format("%2d. %s", index, string))
.subscribeOn(Schedulers. computation());
//when
observer = observable.test() ;
//then
observer.assertComplete() ;
observer.assertNoErrors() ;
observer.assertValueCount( 4);
assertThat(observer.values() , hasItem(" 4. fox"));
}
Performance
Libraries compatibility
● Retrofit READY
● Realm NOT
● Requery READY
● SugarOrm NOT
● Ormlite NOT
● GreenDao NOT
● Firebase NOT (wrapper libraries)
● SqlBrite NOT
RxJava 1 compatibility
github.com/akarnokd/RxJava2Interop
RxJava 1 compatibility
github.com/akarnokd/RxJava2Interop
Should I switch to RxJava2?

More Related Content

What's hot

Process Synchronization
Process SynchronizationProcess Synchronization
Process Synchronization
vinay arora
 
서버 개발자가 바라 본 Functional Reactive Programming with RxJava - SpringCamp2015
서버 개발자가 바라 본 Functional Reactive Programming with RxJava - SpringCamp2015서버 개발자가 바라 본 Functional Reactive Programming with RxJava - SpringCamp2015
서버 개발자가 바라 본 Functional Reactive Programming with RxJava - SpringCamp2015
NAVER / MusicPlatform
 
Java Concurrency Gotchas
Java Concurrency GotchasJava Concurrency Gotchas
Java Concurrency Gotchas
Alex Miller
 
6 Synchronisation
6 Synchronisation6 Synchronisation
6 Synchronisation
Dr. Loganathan R
 
Qt Framework Events Signals Threads
Qt Framework Events Signals ThreadsQt Framework Events Signals Threads
Qt Framework Events Signals Threads
Neera Mital
 
Java Concurrency Idioms
Java Concurrency IdiomsJava Concurrency Idioms
Java Concurrency Idioms
Alex Miller
 
2009 03-26 grails-testing
2009 03-26 grails-testing2009 03-26 grails-testing
2009 03-26 grails-testing
geoffnettaglich
 
TDC 2015 SP - O ciclo de vida de aplicações UWP
TDC 2015 SP - O ciclo de vida de aplicações UWP TDC 2015 SP - O ciclo de vida de aplicações UWP
TDC 2015 SP - O ciclo de vida de aplicações UWP
Vitor Meriat
 
Angular2 rxjs
Angular2 rxjsAngular2 rxjs
Angular2 rxjs
Christoffer Noring
 
Transactions
TransactionsTransactions
Transactions
ashish61_scs
 
Compose Async with RxJS
Compose Async with RxJSCompose Async with RxJS
Compose Async with RxJS
Kyung Yeol Kim
 
J2ee Transaction Overview
J2ee Transaction OverviewJ2ee Transaction Overview
J2ee Transaction Overview
Terry Cho
 
React tips
React tipsReact tips
React tips
Vedran Maršić
 
Modeling the Behavior of Threads in the PREEMPT_RT Linux Kernel Using Automata
Modeling the Behavior of Threads in the PREEMPT_RT Linux Kernel Using AutomataModeling the Behavior of Threads in the PREEMPT_RT Linux Kernel Using Automata
Modeling the Behavior of Threads in the PREEMPT_RT Linux Kernel Using Automata
Daniel Bristot de Oliveira
 
Tdd guide
Tdd guideTdd guide
Tdd guide
Hanokh Aloni
 
Infinum iOS Talks #1 - Swift under the hood: Method Dispatching by Vlaho Poluta
Infinum iOS Talks #1 - Swift under the hood: Method Dispatching by Vlaho PolutaInfinum iOS Talks #1 - Swift under the hood: Method Dispatching by Vlaho Poluta
Infinum iOS Talks #1 - Swift under the hood: Method Dispatching by Vlaho Poluta
Infinum
 
Sistemas de control a tiempo discreto por Guillermo Guzmán
Sistemas de control a tiempo discreto por Guillermo GuzmánSistemas de control a tiempo discreto por Guillermo Guzmán
Sistemas de control a tiempo discreto por Guillermo Guzmán
GuillermoGuzmn16
 
Os3
Os3Os3
Effective java - concurrency
Effective java - concurrencyEffective java - concurrency
Effective java - concurrency
feng lee
 
Clojure concurrency overview
Clojure concurrency overviewClojure concurrency overview
Clojure concurrency overview
Sergey Stupin
 

What's hot (20)

Process Synchronization
Process SynchronizationProcess Synchronization
Process Synchronization
 
서버 개발자가 바라 본 Functional Reactive Programming with RxJava - SpringCamp2015
서버 개발자가 바라 본 Functional Reactive Programming with RxJava - SpringCamp2015서버 개발자가 바라 본 Functional Reactive Programming with RxJava - SpringCamp2015
서버 개발자가 바라 본 Functional Reactive Programming with RxJava - SpringCamp2015
 
Java Concurrency Gotchas
Java Concurrency GotchasJava Concurrency Gotchas
Java Concurrency Gotchas
 
6 Synchronisation
6 Synchronisation6 Synchronisation
6 Synchronisation
 
Qt Framework Events Signals Threads
Qt Framework Events Signals ThreadsQt Framework Events Signals Threads
Qt Framework Events Signals Threads
 
Java Concurrency Idioms
Java Concurrency IdiomsJava Concurrency Idioms
Java Concurrency Idioms
 
2009 03-26 grails-testing
2009 03-26 grails-testing2009 03-26 grails-testing
2009 03-26 grails-testing
 
TDC 2015 SP - O ciclo de vida de aplicações UWP
TDC 2015 SP - O ciclo de vida de aplicações UWP TDC 2015 SP - O ciclo de vida de aplicações UWP
TDC 2015 SP - O ciclo de vida de aplicações UWP
 
Angular2 rxjs
Angular2 rxjsAngular2 rxjs
Angular2 rxjs
 
Transactions
TransactionsTransactions
Transactions
 
Compose Async with RxJS
Compose Async with RxJSCompose Async with RxJS
Compose Async with RxJS
 
J2ee Transaction Overview
J2ee Transaction OverviewJ2ee Transaction Overview
J2ee Transaction Overview
 
React tips
React tipsReact tips
React tips
 
Modeling the Behavior of Threads in the PREEMPT_RT Linux Kernel Using Automata
Modeling the Behavior of Threads in the PREEMPT_RT Linux Kernel Using AutomataModeling the Behavior of Threads in the PREEMPT_RT Linux Kernel Using Automata
Modeling the Behavior of Threads in the PREEMPT_RT Linux Kernel Using Automata
 
Tdd guide
Tdd guideTdd guide
Tdd guide
 
Infinum iOS Talks #1 - Swift under the hood: Method Dispatching by Vlaho Poluta
Infinum iOS Talks #1 - Swift under the hood: Method Dispatching by Vlaho PolutaInfinum iOS Talks #1 - Swift under the hood: Method Dispatching by Vlaho Poluta
Infinum iOS Talks #1 - Swift under the hood: Method Dispatching by Vlaho Poluta
 
Sistemas de control a tiempo discreto por Guillermo Guzmán
Sistemas de control a tiempo discreto por Guillermo GuzmánSistemas de control a tiempo discreto por Guillermo Guzmán
Sistemas de control a tiempo discreto por Guillermo Guzmán
 
Os3
Os3Os3
Os3
 
Effective java - concurrency
Effective java - concurrencyEffective java - concurrency
Effective java - concurrency
 
Clojure concurrency overview
Clojure concurrency overviewClojure concurrency overview
Clojure concurrency overview
 

Similar to Rx java2 - Should I use it?

Iniciación rx java
Iniciación rx javaIniciación rx java
Iniciación rx java
Elisa De Gregorio Medrano
 
Rx workshop
Rx workshopRx workshop
Rx workshop
Ryan Riley
 
How to Think in RxJava Before Reacting
How to Think in RxJava Before ReactingHow to Think in RxJava Before Reacting
How to Think in RxJava Before Reacting
IndicThreads
 
rxJava 2 tips and tricks
rxJava 2 tips and tricks rxJava 2 tips and tricks
rxJava 2 tips and tricks
Stepan Goncharov
 
Reactive mesh
Reactive meshReactive mesh
Reactive mesh
Kalin Maldzhanski
 
Saving lives with rx java
Saving lives with rx javaSaving lives with rx java
Saving lives with rx java
Shahar Barsheshet
 
RxJava applied [JavaDay Kyiv 2016]
RxJava applied [JavaDay Kyiv 2016]RxJava applied [JavaDay Kyiv 2016]
RxJava applied [JavaDay Kyiv 2016]
Igor Lozynskyi
 
Chapter 6 - Process Synchronization
Chapter 6 - Process SynchronizationChapter 6 - Process Synchronization
Chapter 6 - Process Synchronization
Wayne Jones Jnr
 
RxJava 2 Reactive extensions for the JVM
RxJava 2  Reactive extensions for the JVMRxJava 2  Reactive extensions for the JVM
RxJava 2 Reactive extensions for the JVM
Netesh Kumar
 
RxJava и Android. Плюсы, минусы, подводные камни
RxJava и Android. Плюсы, минусы, подводные камниRxJava и Android. Плюсы, минусы, подводные камни
RxJava и Android. Плюсы, минусы, подводные камни
Stfalcon Meetups
 
Finagle and Java Service Framework at Pinterest
Finagle and Java Service Framework at PinterestFinagle and Java Service Framework at Pinterest
Finagle and Java Service Framework at Pinterest
Pavan Chitumalla
 
Reactive Streams and RxJava2
Reactive Streams and RxJava2Reactive Streams and RxJava2
Reactive Streams and RxJava2
Yakov Fain
 
Functional Programming in Java - Code for Maintainability
Functional Programming in Java - Code for MaintainabilityFunctional Programming in Java - Code for Maintainability
Functional Programming in Java - Code for Maintainability
Marcin Stepien
 
RxJava2 Slides
RxJava2 SlidesRxJava2 Slides
RxJava2 Slides
YarikS
 
RxJava on Android
RxJava on AndroidRxJava on Android
RxJava on Android
Dustin Graham
 
Reactive Java (33rd Degree)
Reactive Java (33rd Degree)Reactive Java (33rd Degree)
Reactive Java (33rd Degree)
Tomasz Kowalczewski
 
An Introduction to RxJava
An Introduction to RxJavaAn Introduction to RxJava
An Introduction to RxJava
Sanjay Acharya
 
Akka.NET streams and reactive streams
Akka.NET streams and reactive streamsAkka.NET streams and reactive streams
Akka.NET streams and reactive streams
Bartosz Sypytkowski
 
Building Scalable Stateless Applications with RxJava
Building Scalable Stateless Applications with RxJavaBuilding Scalable Stateless Applications with RxJava
Building Scalable Stateless Applications with RxJava
Rick Warren
 
Get Reactive: Microservices, Programming, and Systems
Get Reactive: Microservices, Programming, and SystemsGet Reactive: Microservices, Programming, and Systems
Get Reactive: Microservices, Programming, and Systems
Jeremy Davis
 

Similar to Rx java2 - Should I use it? (20)

Iniciación rx java
Iniciación rx javaIniciación rx java
Iniciación rx java
 
Rx workshop
Rx workshopRx workshop
Rx workshop
 
How to Think in RxJava Before Reacting
How to Think in RxJava Before ReactingHow to Think in RxJava Before Reacting
How to Think in RxJava Before Reacting
 
rxJava 2 tips and tricks
rxJava 2 tips and tricks rxJava 2 tips and tricks
rxJava 2 tips and tricks
 
Reactive mesh
Reactive meshReactive mesh
Reactive mesh
 
Saving lives with rx java
Saving lives with rx javaSaving lives with rx java
Saving lives with rx java
 
RxJava applied [JavaDay Kyiv 2016]
RxJava applied [JavaDay Kyiv 2016]RxJava applied [JavaDay Kyiv 2016]
RxJava applied [JavaDay Kyiv 2016]
 
Chapter 6 - Process Synchronization
Chapter 6 - Process SynchronizationChapter 6 - Process Synchronization
Chapter 6 - Process Synchronization
 
RxJava 2 Reactive extensions for the JVM
RxJava 2  Reactive extensions for the JVMRxJava 2  Reactive extensions for the JVM
RxJava 2 Reactive extensions for the JVM
 
RxJava и Android. Плюсы, минусы, подводные камни
RxJava и Android. Плюсы, минусы, подводные камниRxJava и Android. Плюсы, минусы, подводные камни
RxJava и Android. Плюсы, минусы, подводные камни
 
Finagle and Java Service Framework at Pinterest
Finagle and Java Service Framework at PinterestFinagle and Java Service Framework at Pinterest
Finagle and Java Service Framework at Pinterest
 
Reactive Streams and RxJava2
Reactive Streams and RxJava2Reactive Streams and RxJava2
Reactive Streams and RxJava2
 
Functional Programming in Java - Code for Maintainability
Functional Programming in Java - Code for MaintainabilityFunctional Programming in Java - Code for Maintainability
Functional Programming in Java - Code for Maintainability
 
RxJava2 Slides
RxJava2 SlidesRxJava2 Slides
RxJava2 Slides
 
RxJava on Android
RxJava on AndroidRxJava on Android
RxJava on Android
 
Reactive Java (33rd Degree)
Reactive Java (33rd Degree)Reactive Java (33rd Degree)
Reactive Java (33rd Degree)
 
An Introduction to RxJava
An Introduction to RxJavaAn Introduction to RxJava
An Introduction to RxJava
 
Akka.NET streams and reactive streams
Akka.NET streams and reactive streamsAkka.NET streams and reactive streams
Akka.NET streams and reactive streams
 
Building Scalable Stateless Applications with RxJava
Building Scalable Stateless Applications with RxJavaBuilding Scalable Stateless Applications with RxJava
Building Scalable Stateless Applications with RxJava
 
Get Reactive: Microservices, Programming, and Systems
Get Reactive: Microservices, Programming, and SystemsGet Reactive: Microservices, Programming, and Systems
Get Reactive: Microservices, Programming, and Systems
 

Recently uploaded

Graspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code AnalysisGraspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code Analysis
Aftab Hussain
 
Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)
Julian Hyde
 
Artificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension FunctionsArtificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension Functions
Octavian Nadolu
 
E-commerce Application Development Company.pdf
E-commerce Application Development Company.pdfE-commerce Application Development Company.pdf
E-commerce Application Development Company.pdf
Hornet Dynamics
 
Microservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we workMicroservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we work
Sven Peters
 
SMS API Integration in Saudi Arabia| Best SMS API Service
SMS API Integration in Saudi Arabia| Best SMS API ServiceSMS API Integration in Saudi Arabia| Best SMS API Service
SMS API Integration in Saudi Arabia| Best SMS API Service
Yara Milbes
 
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
kalichargn70th171
 
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOMLORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
lorraineandreiamcidl
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian CompaniesE-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
Quickdice ERP
 
Using Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query PerformanceUsing Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query Performance
Grant Fritchey
 
Energy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina JonuziEnergy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina Jonuzi
Green Software Development
 
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Łukasz Chruściel
 
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Crescat
 
socradar-q1-2024-aviation-industry-report.pdf
socradar-q1-2024-aviation-industry-report.pdfsocradar-q1-2024-aviation-industry-report.pdf
socradar-q1-2024-aviation-industry-report.pdf
SOCRadar
 
Transform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR SolutionsTransform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR Solutions
TheSMSPoint
 
Using Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional SafetyUsing Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional Safety
Ayan Halder
 
What is Augmented Reality Image Tracking
What is Augmented Reality Image TrackingWhat is Augmented Reality Image Tracking
What is Augmented Reality Image Tracking
pavan998932
 
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, FactsALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
Green Software Development
 
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdfVitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke
 

Recently uploaded (20)

Graspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code AnalysisGraspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code Analysis
 
Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)
 
Artificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension FunctionsArtificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension Functions
 
E-commerce Application Development Company.pdf
E-commerce Application Development Company.pdfE-commerce Application Development Company.pdf
E-commerce Application Development Company.pdf
 
Microservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we workMicroservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we work
 
SMS API Integration in Saudi Arabia| Best SMS API Service
SMS API Integration in Saudi Arabia| Best SMS API ServiceSMS API Integration in Saudi Arabia| Best SMS API Service
SMS API Integration in Saudi Arabia| Best SMS API Service
 
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
 
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOMLORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian CompaniesE-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
 
Using Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query PerformanceUsing Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query Performance
 
Energy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina JonuziEnergy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina Jonuzi
 
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
 
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
 
socradar-q1-2024-aviation-industry-report.pdf
socradar-q1-2024-aviation-industry-report.pdfsocradar-q1-2024-aviation-industry-report.pdf
socradar-q1-2024-aviation-industry-report.pdf
 
Transform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR SolutionsTransform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR Solutions
 
Using Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional SafetyUsing Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional Safety
 
What is Augmented Reality Image Tracking
What is Augmented Reality Image TrackingWhat is Augmented Reality Image Tracking
What is Augmented Reality Image Tracking
 
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, FactsALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
 
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdfVitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdf
 

Rx java2 - Should I use it?

  • 2. RxJava2 1. What’s new? 2. Performance 3. Libraries compatibility 4. RxJava1 compatibility 5. Summary
  • 3. What’s new? ● Reactive Streams compatibility ● Observable types ● Null handling ● Backpressure handling ● Testing
  • 4. Reactive Streams “Reactive Streams is an initiative to provide a standard for asynchronous stream processing with non-blocking back pressure. This encompasses efforts aimed at runtime environments (JVM and JavaScript) as well as network protocols.”
  • 5. Reactive Streams public interface Publisher<T> { public void subscribe(Subscriber<? super T> s); } public interface Subscriber< T> { public void onSubscribe (Subscription s) ; public void onNext(T t); public void onError(Throwable t) ; public void onComplete(); }
  • 6. Reactive Streams public interface Subscription { public void request(long n); public void cancel(); } public interface Processor<T, R> extends Subscriber<T>, Publisher<R> { }
  • 7. Observable types Type Description Flowable<T> Emits 0 or n items and terminates with complete or an error. Supports backpressure, which allows to control how fast a source emits items. Observable<T> Emits 0 or n items and terminates with complete or an error. Single<T> Emits either a single item or an error. The reactive version of a method call. You subscribe to a Single and you get either a return value or an error. Maybe<T> Succeeds with an item, or no item, or errors. The reactive version of an Optional. Completable Either completes or returns an error. It never return items. The reactive version of a Runnable.
  • 8. Null handling Observable.just(null); Single.just(null); Observable.fromCallable(() -> null) .subscribe(System. out::println, Throwable::printStackTrace) ; Observable.just(1).map(v -> null) .subscribe(System. out::println, Throwable::printStackTrace) ;
  • 9. Null handling Solution: enum Irrelevant { INSTANCE } Single.just(1).map(v -> Irrelevant. INSTANCE);
  • 10. Backpressure handling private Flowable<Integer> createFlowable (int items, BackpressureStrategy backpressureStrategy) { return Flowable.create(subscriber -> { for (int i = 0; i < items; i++) { if (subscriber.isCancelled()) { return; } subscriber.onNext(i) ; } subscriber.onComplete() ; }, backpressureStrategy) ; }
  • 11. Backpressure handling ● BackpressureStrategy.BUFFER ● BackpressureStrategy.DROP ● BackpressureStrategy.LATEST ● BackpressureStrategy.ERROR ● BackpressureStrategy.MISSING
  • 12. Testing @Test public void testUsingTestObserver () { //given TestObserver<String> observer = new TestObserver<>() ; Observable<String> observable = Observable. fromIterable(WORDS) .zipWith(Observable. range(1, Integer.MAX_VALUE), (string, index) -> String. format("%2d. %s", index, string)); //when observable.subscribe(observer) ; //then observer.assertComplete() ; observer.assertNoErrors() ; observer.assertValueCount( 9); assertThat(observer.values() , hasItem(" 4. fox")); }
  • 13. Testing //given TestObserver<String> observer = new TestObserver<>() ; Observable<String> observable = Observable. fromIterable(WORDS) .zipWith(Observable. range(1, Integer.MAX_VALUE), (string, index) -> String. format("%2d. %s", index, string)) .subscribeOn(Schedulers. computation()); //when observable.subscribe(observer) ; //then observer.assertComplete() ; observer.assertNoErrors() ; observer.assertValueCount( 4); assertThat(observer.values() , hasItem(" 4. fox"));
  • 14. Testing public class ImmediateSchedulerRule implements TestRule { @Override public Statement apply(final Statement base , Description description) { return new Statement() { @Override public void evaluate() throws Throwable { RxJavaPlugins. setIoSchedulerHandler(build(Schedulers. trampoline())); RxJavaPlugins. setComputationSchedulerHandler(build(Schedulers. trampoline())); RxJavaPlugins. setNewThreadSchedulerHandler(build(Schedulers. trampoline())); try { base.evaluate() ; } finally { RxJavaPlugins. reset(); } } };
  • 15. Testing @Rule public final ImmediateSchedulerRule testSchedulerRule = new ImmediateSchedulerRule() ;
  • 16. Testing @Test public void testUsingTestObserver () { //given TestObserver<String> observer ; Observable<String> observable = Observable. fromIterable(WORDS) .zipWith(Observable. range(1, Integer.MAX_VALUE), (string, index) -> String. format("%2d. %s", index, string)) .subscribeOn(Schedulers. computation()); //when observer = observable.test() ; //then observer.assertComplete() ; observer.assertNoErrors() ; observer.assertValueCount( 4); assertThat(observer.values() , hasItem(" 4. fox")); }
  • 18. Libraries compatibility ● Retrofit READY ● Realm NOT ● Requery READY ● SugarOrm NOT ● Ormlite NOT ● GreenDao NOT ● Firebase NOT (wrapper libraries) ● SqlBrite NOT
  • 21. Should I switch to RxJava2?