Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
289 views
in Technique[技术] by (71.8m points)

spring boot - Axonframework, How can I apply the same state check to every function

So I'm currently developing application using Spring boot with Axonframework, So in Axonframework there is something called aggregate. It can store some states and etc. Everything work fine, but there's a case where I have to check the same state to every incoming command before update the aggregate. Something like this

@CommandHandler
fun handle(command: UpdateProductCommand){
  if (isProductApproved){
    throw IllegalArgumentException("This product has not been approved by qa.")
  }
  ... do something
}

@CommandHandler
fun handle(command: PublishProductCommand){
  if (isProductApproved){
    throw IllegalArgumentException("This product has not been approved by qa.")
  }
  ... do something
}

... Some other commands
... Check the same state again and again to every command

As you can see, I have to check this isProductApproved to most of the commands. Is there anyway to easily apply this checking state to every functions or commands before start doing some logic. I would expect something like this

@Aggregate
@CheckState(value = isProductApproved)
class ProductAggregate {
  ... apply to every command
}

Or any better ways.

question from:https://stackoverflow.com/questions/66059291/axonframework-how-can-i-apply-the-same-state-check-to-every-function

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In Axon Framework applications it is possible to define a handler interceptor for a specific component containing the handlers (in your case an Aggregate). This can be achieved by adding a method handling the message, combined with the @MessageHandlerInterceptor annotation.

You can find more details on official Axon Reference guide (docs): https://docs.axoniq.io/reference-guide/axon-framework/messaging-concepts/message-intercepting#messagehandlerinterceptor

In my opinion this would be the most pragmatic way to do some logic that is general to a particular component, which is in your case an Aggregate (but it can be any other messaging component actually: event/query handlers, for example)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...