Build a query handler passing the MetaData along with QueryResponse Message

Hi folks,

I’m building a component that is interested in the MetaData of all messages passed around. I started with passing MetaData with commands, adjusted the aggregates to pump it into the resulting events and have some idea how to store them in the projections. On this point I’m stuck at the query side.

I can pass MetaData along with the query, but I can’t get it from my projection to the QueryResponseMessage. The only two values I see there are:

'traceId'->'a9b1350c-799a-4b79-b4d2-9cf564c297ed', 
'correlationId'->'a9b1350c-799a-4b79-b4d2-9cf564c297ed'

I there any way, I can pass it out of the projection without changing the return type?

I found that there is an interceptor chain invoked before the handler, but not after the query handler invocation (see DefaultInterceptorChain).

It is not stated in the docs, but GenericQueryResponseMessage#asNullableResponseMessage seems to be at least prepared to the fact, that the handler invocation is returning a message instead of the payload. In the same time, I could not yet found a way to register a handler returning a QueryResponseMessage<T> to be dispatched by the query for <Q, T>

Is it intended to be used it like this?

Glad about any ideas.

Update:

After debugging a-little I found how the handler is selected and implemented an own ReturnType for let the QueryResultMessage return type to match the query. By doing so, the query handler returning the QueryResultMessagenow gets invokes. On the deserialization of the result message the following error appears:

2020-09-30 21:21:07.545 ERROR 118470 --- [nio-8081-exec-4] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.util.concurrent.CompletionException: org.axonframework.queryhandling.QueryExecutionException: class org.axonframework.serialization.UnknownSerializedType cannot be cast to class org.axonframework.messaging.responsetypes.ResponseType (org.axonframework.serialization.UnknownSerializedType and org.axonframework.messaging.responsetypes.ResponseType are in unnamed module of loader 'app')] with root cause

This message is coming from Axon Server Connector, so it seams that Axon Server fails to work with my provided result type.

Is there any way to solve this on the “framework level” and not introduce own envelope for the result type which contains metadata?

Cheers,

Simon

Hey Simon,

Troublesome to hear you’ve got this problem you’re describing.
Gonna ask a couple of follow ups to better understand your scenario right now:

  1. Which Framework version are you using?
  2. Are you using any Axon Extensions?
  3. Which Server version are you using?
  4. How are you dispatching queries?
  5. Have you defined any MessageDipatchInterceptors on the QueryGateway and/or QueryBus?
  6. Have you defined any MessageHandlerInterceptors on the QueryGateway and/or QueryBus?
  7. Are you doing anything peculiar when it comes to returning your Query Responses?
  8. Have you defined a custom CorrelationDataProvider to populate the MetaData of newly created messages, when another message is being handled?

To be up front, my hunch is it’s something with the CorrelationDataProvider at this stage, as that’s the component which is in charge of populating the MetaData of a newly created message when another is being handled. By the way, the default CorrelationDataProvider instance is the MessageOriginProvider, which sets the correlationId and traceId.

1 Like

Thank you Steven,
for more details see: