Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pagination in Java Reactive Programming [closed]

I am still new in Java Reactive Programming and my project Requirement wants me to implement Pagination with Reactive Programming in Java.

Like if I hit an API which returns me 10000 records in stream then I needs to return a flux with proper pagination.

Can anyone suggest me a good approach about this?

Like this is the approach that I am following.

Repository

  public interface CouchBaseRepository extends ReactiveCouchBaseRepository<Book,Integer> {
   @Query("#{#n1ql.selectEntity} where name=$1")
   public Flux<Book> getPaginatedFlux(String name ,final Pageable pageable);

This is my Repositorty but when I up my application then it shows this following error.

    java.lang.IllegalStateException: Method has to have one of the following return types! [interface org.springframework.data.domain.Page, interface org.springframework.data.domain.Slice, interface java.util.List]

I cannot use Page inteface here as it is blocking so Is there any way to deal with this problem?

like image 981
Nikhil Avatar asked Sep 22 '18 08:09

Nikhil


People also ask

How pagination is handled in Java?

Pagination in JavaIt allows users to display a part of records only. Loading all records on a single page may take time, so it is always recommended to created pagination. In java, we can develop pagination examples easily. In this pagination example, we are using the MySQL database to fetch records.

How is pagination implemented?

For example, you can implement pagination using links to new pages on your ecommerce site, or using JavaScript to update the current page. Load more and infinite scroll are generally implemented using JavaScript.

How is reactive programming non-blocking?

In plain terms reactive programming is about non-blocking applications that are asynchronous and event-driven and require a small number of threads to scale. A key aspect of that definition is the concept of backpressure which is a mechanism to ensure producers don't overwhelm consumers.


1 Answers

I haven't worked with spring-webflux yet, so I can't comment on specific API calls, but I'll provide a "theoretical" answer that might help as well.

Flux represents a stream of data (possibly infinite). So, pagination is kind of not consistent with reactivity, only because they're talking about different things

Consider implementing pagination with input parameters (like usual limit/offset) in the method that returns Flux of (logically decided) up to 10000 records as per your requirement.

So, one call will be handled in a "reactive manner" but it will return only one page of data, if you want to load another page - do another reactive call.

Of course at the level of streams, after 10000 objects receive, the stream should be closed. This is the approach I suggest.

There is an another option: implement everything via one stream, but in this case the client side (UI or whatever that consumes the paged data) will have to be "smart enough" to load /unload only the required data. In other words, if all-in-all you have, say 1 million objects to show, think whether you should avoid situation where all 1 million is loaded on client side at once.

In addition the page navigation will be kind of tricky (stuff like, get next/previous page). I haven't worked like this before. I think bottom line, the choice will be requirement driven.

like image 113
Mark Bramnik Avatar answered Oct 03 '22 21:10

Mark Bramnik