QueryGatewayinterface and the
DefaultQueryGatewayimplementation. 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
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). However, this behavior can be overridden by stating the
queryNameattribute of the
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
@QueryHandlerannotated function's return type is
CompletableFuture, the result will be returned asynchronously regardless of the type of the query bus.
QueryUpdateEmittercomponent provided by Axon.
QueryUpdateEmittercomponent within the event handler function of the
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
initialResultand the other is
updates. In order to achieve 'reactiveness' we use Project Reactor's
Note Once the subscription query is issued, all updates are queued until the subscription to the
updatesis 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.Note
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>)method gives us the possibility to subscribe to the
updatesin one go. If we want more granular control over the results, we can use the
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.
SimpleQueryBusis the only Query Bus implementation in Axon 3.1. It does straightforward processing of queries in the thread that dispatches them. The
SimpleQueryBusallows interceptors to be configured.
@Pattern. You need to include a JSR 303 implementation (such as Hibernate-Validator) on your classpath. Then, configure a
BeanValidationInterceptoron 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.
MessageHandlerInterceptorinterface. This interface declares one method,
handle, that takes three parameters: the query message, the current
InterceptorChainis used to continue the dispatching process, whereas the
UnitOfWorkgives 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).