0

I am sending a command and expecting a result via CommandGateway.sendAndWait(Object command, long timeout, TimeUnit unit) method :

 Greeting greeting = commandGateway.sendAndWait(new GenericCommandMessage<GetGreetingCommand>(cmd), 1000, TimeUnit.MILLISECONDS);

There are 3 possible outcomes of the above call:

1) Return a non null object.

2) Return a null object (via business logic from my @CommandHandler that queries a DB and doesn't find what I was looking for)

3) Return a null object (returned by Axon framework in case timeout is reached)

I need to implement a way to differentiate between points 2) and 3) so that I can return NOT_FOUND or REQUEST_TIMEOUT statuses accordingly. Do you have any recommendations on how to model this ?

One way that I though of was to add a status field in Greeting (my model object) and in case the @CommandHandler receives nothing from DB I would return a dummy Greeting object with status = -1 (meaning NOT_FOUND), but this solution would mean adding flags to model objects only to differentiate between framework flows and I don't think this is recommended.

Sorin J
  • 542
  • 1
  • 4
  • 14

1 Answers1

1

The default CommandGateway doesn't allow for this distinction. However, it is possible to define your own gateway, by providing an interface to the GatewayProxyFactory.createGateway() method. Create an instance by passing the CommandBus that the gateway should send messages through in the constructor.

This mechanism allows you to define the behavior that you want, for each method. If you declare a TimeoutException, that exception will be thrown instead of a null return value being returned. If the timeout is the same for each invocation, you can event replace the timeout value parameters (int/long and timeunit) with an annotation.

Check out the javadoc on the GatewayProxyFactory for more details.

Allard
  • 2,640
  • 13
  • 13