Now, it seems not easy to add metadata to a command.
for example, we can't do this, this code won't compile.
CommandMessage
.asCommandMessage(xxRequest);
command = command.withMetaData(meta);
commandGateway.send(command, useCaseCallback);
Can we just add a third parameter for this case?
Or there's another easy way to do this?
Hi @askfor, I'd like to refer you to the Google User Group or Stack Overflow with the tag axon for questions like this. We'd prefer to keep the issue tab for framework issues and suggestions in that respect. As such I will be closing this 'issue'.
Nonetheless, I would like to resolve the problem you are encountering during development.
To be honest, I wouldn't have expected that the shared snippet wouldn't compile.
Would you be able to share a more specific code snippet showing the problem at hand?
If I, for example, set up the following:
public void publishSomeCommand(CommandBus commandBus) {
CommandGateway commandGateway = DefaultCommandGateway.builder().commandBus(commandBus).build();
Map<String, Object> metaData = new HashMap<>();
metaData.put("some-metadata-key", "some-metadata-value");
commandGateway.send(GenericCommandMessage.asCommandMessage("someCommand").withMetaData(metaData));
}
That will compile without any issues.
Now, to focus on you question about 'a third parameter for metadata'.
Typically, if framework users want to pair dispatching a Command with some MetaData, then that has to happen for _any_ command being dispatched. From that perspective, it is for more maintainable to introduce a MessageDispatchInterceptor, which add the desired MetaData on that level.
That way, you will not 'pollute' the command-dispatching-code with MetaData infrastructure logic, but will have maintained it in one location. Would this resolve the problem you are encountering @askfor?
For now, I will close the issue as a 'Question', and hopefully, I have answered it sufficiently enough :-)
Hi @smcvb , Thanks for your response. the code you showed will compile.
But in my case, I have to pass a callback to the commandGateway.
commandGateway.send expecting the type of the Command should be the same as the GenericType of the CommandCallback. If I wrap the message by GenericCommandMessage.asCommandMessage, then the type of command won't be the same with the GenericType for the callback.
send(C command, CommandCallback super C, R> callback)
Hope I explained it clearly.
Ah, good point their @askfor. If you want to leverage the API using your own CommandCallback + adding MetaData, it might be simpler to use the CommandBus instead.
A little insight here, but we will likely somewhere in the life cycle of Axon 4 deprecated the API on the CommandGateway through which you can provide your own CommandCallback. We view this to be out of scope for a 'convenience approach to dispatching commands' - which is what the CommandGateway is build for.
Hence my suggestion on using the CommandBus.
Hope this helps you out!
Another suggestion could be to set up a Custom CommandGateway by using the CommandGatewayFactory. A description around usage of it can be found here.
Thanks, both solutions fits our needs.
Most helpful comment
Thanks, both solutions fits our needs.