Source:
public <Q, R> CompletableFuture<QueryResponseMessage<R>> query(QueryMessage<Q, R> queryMessage) { validateShutdown("queries"); QueryMessage<Q, R> interceptedQuery = dispatchInterceptors.intercept(queryMessage); CompletableFuture<QueryResponseMessage<R>> queryTransaction = new CompletableFuture<>(); try { String context = targetContextResolver.resolveContext(interceptedQuery); QueryRequest queryRequest = serializer.serializeRequest( interceptedQuery, DIRECT_QUERY_NUMBER_OF_RESULTS, DIRECT_QUERY_TIMEOUT_MS, priorityCalculator.determinePriority(interceptedQuery) ); queryService(context) .query(queryRequest, new StreamObserver<QueryResponse>() { @Override public void onNext(QueryResponse queryResponse) { logger.debug("Received query response [{}]", queryResponse); queryTransaction.complete( serializer.deserializeResponse(queryResponse, queryMessage.getResponseType()) ); } @Override public void onError(Throwable throwable) { logger.debug("Received error while waiting for first response", throwable); queryTransaction.completeExceptionally( ErrorCode.QUERY_DISPATCH_ERROR.convert( configuration.getClientId(), throwable ) ); shutdownLatch.decrement(); } @Override public void onCompleted() { if (!queryTransaction.isDone()) { queryTransaction.completeExceptionally( ErrorCode.QUERY_DISPATCH_ERROR.convert( ErrorMessage.newBuilder() .setMessage("No result from query executor") .build() ) ); } shutdownLatch.decrement(); } }); shutdownLatch.increment(); } catch (Exception e) { logger.debug("There was a problem issuing a query {}.", interceptedQuery, e); queryTransaction.completeExceptionally( ErrorCode.QUERY_DISPATCH_ERROR.convert(configuration.getClientId(), e) ); } return queryTransaction; }

Target:
public <Q, R> CompletableFuture<QueryResponseMessage<R>> query(QueryMessage<Q, R> queryMessage) { shutdownLatch.ifShuttingDown(String.format("Cannot dispatch new %s as this bus is being shut down", "queries")); QueryMessage<Q, R> interceptedQuery = dispatchInterceptors.intercept(queryMessage); ShutdownLatch.ActivityHandle queryInTransit = shutdownLatch.registerActivity(); CompletableFuture<QueryResponseMessage<R>> queryTransaction = new CompletableFuture<>(); try { String context = targetContextResolver.resolveContext(interceptedQuery); QueryRequest queryRequest = serializer.serializeRequest(interceptedQuery, DIRECT_QUERY_NUMBER_OF_RESULTS, DIRECT_QUERY_TIMEOUT_MS, priorityCalculator.determinePriority(interceptedQuery)); QueryServiceGrpc.QueryServiceStub queryService = queryService(context); queryService.query(queryRequest, new StreamObserver<QueryResponse>() { @Override public void onNext(QueryResponse queryResponse) { logger.debug("Received query response [{}]", queryResponse); QueryResponseMessage<R> responseMessage = serializer.deserializeResponse(queryResponse, queryMessage.getResponseType()); queryTransaction.complete(responseMessage); } @Override public void onError(Throwable throwable) { logger.debug("Received error while waiting for first response", throwable); AxonException exception = ErrorCode.QUERY_DISPATCH_ERROR.convert(configuration.getClientId(), throwable); queryTransaction.completeExceptionally(exception); queryInTransit.end(); } @Override public void onCompleted() { if (!queryTransaction.isDone()) { ErrorMessage errorMessage = ErrorMessage.newBuilder() .setMessage("No result from query executor") .build(); AxonException exception = ErrorCode.QUERY_DISPATCH_ERROR.convert(errorMessage); queryTransaction.completeExceptionally(exception); } queryInTransit.end(); } }); } catch (Exception e) { logger.debug("There was a problem issuing a query {}.", interceptedQuery, e); AxonException exception = ErrorCode.QUERY_DISPATCH_ERROR.convert(configuration.getClientId(), e); queryTransaction.completeExceptionally(exception); queryInTransit.end(); } return queryTransaction; }