question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Implement Page and PageResponseType for QueryBus

See original GitHub issue

As a user of the QueryBus/QueryGateway the use case where you’d want to retrieve a Page<E> of a certain type makes sense. We should hence introduce a Page implementation which can be used as a ResponseType (introduced in #463) when performing a query.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:11 (7 by maintainers)

github_iconTop GitHub Comments

3reactions
manuelartecommented, Jun 5, 2020

Hi,

I also implemented my own PageResponseType, it is not at all ready for production, but maybe it can give some inspiration to someone in the same situation:

public class PageResponseType<R> extends AbstractResponseType<Page<R>> {


  /**
   * Instantiate a {@link ResponseType} with the given {@code expectedResponseType} as the type to be matched against and
   * to which the query response should be converted to, as is or as the contained type for an array/list/etc.
   *
   * @param expectedResponseType the response type which is expected to be matched against and to be returned, as is or as
   *                             the contained type for an array/list/etc
   */
  @JsonCreator
  @ConstructorProperties({"expectedResponseType"})
  public PageResponseType(@JsonProperty("expectedResponseType") Class<R> expectedResponseType) {
    super(expectedResponseType);
  }

  @Override
  public boolean matches(Type responseType) {
    Type unwrapped = ReflectionUtils.unwrapIfType(responseType, Future.class, Page.class);
    return isGenericAssignableFrom(unwrapped) || isAssignableFrom(unwrapped);
  }

  @Override
  public Page<R> convert(Object response) {
    return (Page<R>) response;
  }

  @Override
  public Class responseMessagePayloadType() {
    return Page.class;
  }

  @Override
  public String toString() {
    return "PageResponseType{" + expectedResponseType + "}";
  }

}
2reactions
rhubarbcommented, Mar 5, 2022

Excellent @manuelarte! I’m curious why you think it’s not production-ready? It works like a charm.

Just to complete the story for others finding this: I used manuelarte’s code above, verbatim, with these imports in my class: (notice that the Page is a spring JPA page)

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.axonframework.common.ReflectionUtils;
import org.axonframework.messaging.responsetypes.AbstractResponseType;
import org.axonframework.messaging.responsetypes.ResponseType;
import org.springframework.data.domain.Page;

import java.beans.ConstructorProperties;
import java.lang.reflect.Type;
import java.util.concurrent.Future;

My query class has a get/setPageable() method on it to hold the pageable that comes in from the Spring REST controller. My query service code looks a bit like this:

        // Create a simple query pojo with a single property to carry the pageable from my Spring REST controller
        final PaginatedSummariesQuery query = new PaginatedSummariesQuery(pageable);

        // use manuelarte's magic response-type to carry a page across the query bus
        final ResponseType<Page<SummaryEntity>> responseType = new PageResponseType<>(SummaryEntity.class);
        final CompletableFuture<Page<BookSummaryEntity>> resultFuture = queryGateway.query(query, responseType);
        try {
            Page<SummaryEntity> entityPage = resultFuture.get();
            return entityPage;
        } catch (InterruptedException | ExecutionException e) {
            throw new ResourceNotFoundException("BookSummary", "query");
        }

and my query handler mostly like this (verbose for clarity)

    @QueryHandler
    public Page<SummaryEntity> handle(PaginatedSummariesQuery query) {
        final Page<SummaryEntity> page = repository.findAll(query.getPageable());
        return page;
    }
Read more comments on GitHub >

github_iconTop Results From Across the Web

No results found

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found