CompletableFuture with timeout

Two CompletableFutures  can be combined to timeout the CompletableFuture with the business logic like below:  package competablefuture; import java.time.Duration; import java.util.concurrent.CompletableFuture; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; public class CompletableFutureSample { static ScheduledExecutorService pool = Executors.newScheduledThreadPool(4); public static CompletableFuture executeAsyncTimed(Duration duration) { final CompletableFuture asyncTimedFuture = new CompletableFuture<>(); pool.schedule(() -> asyncTimedFuture.completeExceptionally(new TimeoutException()), duration.toMillis(), … More CompletableFuture with timeout

RxJava Mapping functions

RxJava has several mapping functions. These functions transforms the emitted items to another form or value. The mapping functions are: map flatMap concatMap flatMapIterable switchMap   map function maps the items to another value of the same or different type @Test public void testobservableMap() { List<Integer> list = new ArrayList<>(); IntStream.range(0, 1000).forEach(list::add); Observable<Integer> observableFromCollection = … More RxJava Mapping functions

RxJava Operations on Observables

Filtering and other operations on Observables If only a subset of elements in a stream is needed, a filter can be used to eliminate the items not needed and let the observable emit only desired elements. @Test public void testCreateObservableFromCollectionWithFilter() { List<Integer> list = new ArrayList<>(); IntStream.range(0, 1000).forEach(list::add); Observable<Integer> observableFromCollection = Observable.from(list).filter(e -> e%2==0); observableFromCollection.subscribe(new … More RxJava Operations on Observables

Creating Observables and Subscribers in RxJava

RxJava is a very useful library for asynchronous and event-based programming, also called reactive programming. There are two main actors in RxJava: Observables — An object that emits a stream of data or events Observers/Subscribers — An object that acts upon the emitted data.   An observer is an object which know how to act upon 3 sort of … More Creating Observables and Subscribers in RxJava

Reactive processing methods

Reactive programming can be achieved on several ways. Below a short list of technologies and frameworks in Java landscape. Executor service in combination of Futures. For fire and forget implementations, use Runnable threads. For implementations whereby a response is required (Fork-Join), use a Callable implementation. ExecutorCompletionService (Producer-Consumer approach). Asynchronous service calls. This can be achieved … More Reactive processing methods