QueryBusis the mechanism that dispatches queries to query handlers. Queries are registered using the combination of the query request name and query response type. It is possible to register multiple handlers for the same request-response combination, which can be used to implement the scatter-gather pattern. When dispatching queries, the client must indicate whether it wants a response from a single handler or from all handlers.
QueryGatewayis a convenient interface towards the query dispatching mechanism. While you are not required to use a gateway to dispatch queries, it is generally the easiest option to do so. It abstracts certain aspects for you, like the necessity to wrap a Query payload in a Query Message.
QueryGateway, both provide several types of queries. Axon Framework makes a distinction between three types, being:
NoHandlerForQueryExceptionis 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.Stringin our case).
queryNameattribute of the
List<String>, we would do something like this:
@QueryHandlerannotated function's return type is
QueryUpdateEmittercomponent provided by Axon.
QueryUpdateEmittercomponent within the event handler function of the
FetchCardSummariesQueryin our case)
gc1id and initial value of
gc1is changed (in our case an update means the card is redeemed).
initialResultand the other is
NoteOnce the subscription query is issued, all updates are queued until the subscription to the
updatesis done. This behavior prevents the losing of updates.NoteThe 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.NoteThe
reactor-coredependency 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>)
updatesin one go.
updates()methods on the query result.
queryUpdateEmitterwill continue to emit updates even when there are no subscribers, we need to notify the emitting side once we are no longer interested in receiving updates.
doFinallyhook, there is the
Flux#usingAPI. This is synonymous
RedeemCmd, our event handler in the projection will eventually be triggered,
println()method, the update will be printed out once it is received.