Configuring Query Handlers

This page describes the process when it comes to configuring a Query Handlers. Note, that a Query Handler is a (singleton) object containing @QueryHandler annotated functions.

Registering a Query Handler

When you register a Query Handler, that in essence means you register a class containing annotated query handlers. Upon receiving such a class during configuration, Axon will scan it's contents for all the @QueryHandler annotated methods. In the registration process the following information defines a given query handling function:

  1. The first parameter of the method is the query payload.

  2. The methods response type is the query's response type.

  3. The value of the queryName field in the annotation as the query's name (this is optional and in its absence will default to the query payload).

Note that it is possible to register multiple query handlers for the same query payload, response type and name. Further more, when dispatching a query the client can indicate whether he/she wants the result from a single handler or the result from all handlers corresponding to the query payload, name and response type combination.

The following snippets point out how a Query Handler can be registered:

Axon Configuration API
Spring Boot AutoConfiguration
Axon Configuration API

Taken the existence of the following Query Handler:

public class CardSummaryProjection {
@QueryHandler
public CardSummary handle(FetchCardSummaryQuery query) {
CardSummary cardSummary;
// Retrieve CardSummary instance, for example from a repository.
return cardSummary;
}
}

The following is needed to register a CardSummaryProjection as being a Query Handler:

Configurer axonConfigurer = DefaultConfigurer.defaultConfiguration()
.registerQueryHandler(conf -> new CardSummaryProjection());
Spring Boot AutoConfiguration

When using Spring Boot, simply specifying the Query Handler as a bean is sufficient:

@Component
public class CardSummaryProjection {
@QueryHandler
public CardSummary handle(FetchCardSummaryQuery query) {
CardSummary cardSummary;
// Retrieve CardSummary instance, for example from a repository.
return cardSummary;
}
}

Identical Query Handling methods in a single Query Handler

A Query Handler can currently contain several identical query handling methods in one Query Handler. The outcome of which method will actually be called is however unspecified.

Note that this should be regarded as a very uncommon scenario, as typically identical query handling methods would be spread over several Query Handlers