QueryGateway
interface and the DefaultQueryGateway
implementation. The query gateway provides a number of methods that allow you to send a query and wait for a single or multiple results either synchronously, with a timeout or asynchronously. The query gateway needs to be configured with access to the Query Bus and a (possibly empty) list of QueryDispatchInterceptor
s.NoHandlerForQueryException
is thrown. In case multiple handlers are registered, it is up to the implementation of the Query Bus to decide which handler is actually invoked. In the listing below we have a simple query handler:java.lang.String
in our case). However, this behavior can be overridden by stating the queryName
attribute of the @QueryHandler
annotation.List<String>
, we would do something like this:CompletableFuture
, which depending on the type of the query bus may be resolved immediately. However, if a @QueryHandler
annotated function's return type is CompletableFuture
, the result will be returned asynchronously regardless of the type of the query bus.QueryUpdateEmitter
component provided by Axon.CardSummaryProjection
example in the Quick Start section with a query handler for a specific GiftCard:QueryUpdateEmitter
component within the event handler function of the RedeemedEvt
event:CardSummary
) and to be updated once the state of GiftCard with id "gc1" is changed (in our case update means the card is redeemed). The type of the update is an Integer
. Do note that the type of the update must match the type of the emission side.QueryBus
. We receive a query result which contains two components: one is initialResult
and the other is updates
. In order to achieve 'reactiveness' we use Project Reactor's Mono
for initialResult
and Flux
for updates
.Note Once the subscription query is issued, all updates are queued until the subscription to theFlux
ofupdates
is done. This behavior prevents losing of updates.Note The Framework prevents issuing more than one query message with the same id. If it is necessary to be updated in several different places, create a new query message.Notereactor-core
dependency is mandatory for usage of subscription queries. However, it is a compile time dependency and it is not required for other Axon features.
SubscriptionQueryResult#handle(Consumer<? super I>, Consumer<? super U>)
method gives us the possibility to subscribe to the initialResult
and the updates
in one go. If we want more granular control over the results, we can use the initialResult()
and updates()
methods on the query result.RedeemCmd
, our event handler in the projection will eventually be triggered, which will result in the emission of an update. Since we subscribed with the println()
method to updates, the update will be printed out once it is received.SimpleQueryBus
is the only Query Bus implementation in Axon 3.1. It does straightforward processing of queries in the thread that dispatches them. The SimpleQueryBus
allows interceptors to be configured.@NotEmpty
and @Pattern
. You need to include a JSR 303 implementation (such as Hibernate-Validator) on your classpath. Then, configure a BeanValidationInterceptor
on your Query Bus, and it will automatically find and configure your validator implementation. While it uses sensible defaults, you can fine-tune it to your specific needs.TipYou want to spend as few resources on an invalid queries as possible. Therefore, this interceptor is generally placed in the very front of the interceptor chain. In some cases, a Logging or Auditing interceptor might need to be placed in front, with the validating interceptor immediately following it.
MessageHandlerInterceptor
, allowing you to configure it as a Handler Interceptor as well.MessageHandlerInterceptor
interface. This interface declares one method, handle
, that takes three parameters: the query message, the current UnitOfWork
and an InterceptorChain
. The InterceptorChain
is used to continue the dispatching process, whereas the UnitOfWork
gives you (1) the message being handled and (2) provides the possibility to tie in logic prior, during or after (query) message handling (see UnitOfWork for more information about the phases).