Source:
private <C, R> void doDispatch(CommandMessage<C> commandMessage, CommandCallback<? super C, ? super R> commandCallback) { shutdownLatch.isShuttingDown( () -> new IllegalStateException("Cannot dispatch new commands as this bus is being shutdown") ); AtomicBoolean serverResponded = new AtomicBoolean(false); try { String context = targetContextResolver.resolveContext(commandMessage); Command command = serializer.serialize(commandMessage, routingStrategy.getRoutingKey(commandMessage), priorityCalculator.determinePriority(commandMessage)); CommandServiceGrpc .newStub(axonServerConnectionManager.getChannel(context)) .dispatch(command, new StreamObserver<CommandResponse>() { @Override public void onNext(CommandResponse commandResponse) { serverResponded.set(true); logger.debug("Received command response [{}]", commandResponse); try { CommandResultMessage<R> resultMessage = serializer.deserialize(commandResponse); commandCallback.onResult(commandMessage, resultMessage); } catch (Exception ex) { commandCallback.onResult(commandMessage, asCommandResultMessage(ex)); logger.info("Failed to deserialize payload - Cause: {}", commandResponse.getPayload().getData(), ex.getCause().getMessage()); } } @Override public void onError(Throwable throwable) { serverResponded.set(true); commandCallback.onResult(commandMessage, asCommandResultMessage( ErrorCode.COMMAND_DISPATCH_ERROR.convert( configuration.getClientId(), throwable ) )); shutdownLatch.decrement(); } @Override public void onCompleted() { if (!serverResponded.get()) { ErrorMessage errorMessage = ErrorMessage.newBuilder() .setMessage("No result from command executor") .build(); commandCallback.onResult(commandMessage, asCommandResultMessage( ErrorCode.COMMAND_DISPATCH_ERROR.convert(errorMessage)) ); } shutdownLatch.decrement(); } } ); shutdownLatch.increment(); } catch (Exception e) { logger.debug("There was a problem dispatching command [{}].", commandMessage, e); commandCallback.onResult( commandMessage, asCommandResultMessage(ErrorCode.COMMAND_DISPATCH_ERROR.convert(configuration.getClientId(), e)) ); } }

Target:
private <C, R> void doDispatch(CommandMessage<C> commandMessage, CommandCallback<? super C, ? super R> commandCallback) { shutdownLatch.ifShuttingDown("Cannot dispatch new commands as this bus is being shutdown"); ShutdownLatch.ActivityHandle commandInTransit = shutdownLatch.registerActivity(); AtomicBoolean serverResponded = new AtomicBoolean(false); try { String context = targetContextResolver.resolveContext(commandMessage); Command command = serializer.serialize(commandMessage, routingStrategy.getRoutingKey(commandMessage), priorityCalculator.determinePriority(commandMessage)); CommandServiceGrpc.CommandServiceStub commandService = CommandServiceGrpc.newStub(axonServerConnectionManager.getChannel(context)); commandService.dispatch(command, new StreamObserver<CommandResponse>() { @Override public void onNext(CommandResponse commandResponse) { serverResponded.set(true); logger.debug("Received command response [{}]", commandResponse); try { CommandResultMessage<R> resultMessage = serializer.deserialize(commandResponse); commandCallback.onResult(commandMessage, resultMessage); } catch (Exception ex) { commandCallback.onResult(commandMessage, asCommandResultMessage(ex)); logger.info("Failed to deserialize payload - Cause: {}", commandResponse.getPayload().getData(), ex.getCause().getMessage()); } } @Override public void onError(Throwable throwable) { serverResponded.set(true); commandCallback.onResult(commandMessage, asCommandResultMessage( ErrorCode.COMMAND_DISPATCH_ERROR.convert(configuration.getClientId(), throwable) )); commandInTransit.end(); } @Override public void onCompleted() { if (!serverResponded.get()) { ErrorMessage errorMessage = ErrorMessage.newBuilder() .setMessage("No result from command executor") .build(); commandCallback.onResult( commandMessage, asCommandResultMessage(ErrorCode.COMMAND_DISPATCH_ERROR.convert(errorMessage)) ); } commandInTransit.end(); } }); } catch (Exception e) { logger.debug("There was a problem dispatching command [{}].", commandMessage, e); commandCallback.onResult( commandMessage, asCommandResultMessage(ErrorCode.COMMAND_DISPATCH_ERROR.convert(configuration.getClientId(), e)) ); commandInTransit.end(); } }