Starting sagas conditionally

Hi everyone,

I have a Saga component which contains a method which is annotated with @StartSaga and which expects EventA as first parameter.

My “problem” is that the I don’t ant to start a Saga for every occurence of EventA. In fact I want to check some properties in EventA and then decide if I want to start a sage or not.

While it’s possible to end a saga conditionally using SagaLifecycle.end() it seems not to be possible to start a saga conditionally. The only workaround I can think of is calling SagaLifecycle.end() in the @StartSaga annotated method in order to end the saga immediately, but that feels a hack.

Any ideas/suggestions?

Oliver

I stumbled upon @MessageHandlerInterceptor. Can I put that into my saga component to intercept the event handling? By injecting the InterceptorChain I can decide whether I want to proceed by calling InterceptorChain.proceed() or to discard the event.

Hi Oliver,

You are correct, starting a saga is not conditionally possible at the moment. You can use SagaLifeCycle.end() right after @StartSaga. Definitely, something for us to think about for the future. Thank you for giving us some great ideas. :slight_smile:
You can use the @MessageHandlerInterceptor but that would only work for existing sagas. I’m assuming this solution is not going to help you much in this case.
I’m wondering if you really need a saga in this case, or if you can use a MessageHandler instead? Also, can you publish a separate event to start a saga? Is it possible for you to give me a bit more context?

Thanks again,
Sara

Hi Sara,

thanks for answering.

It would be an option to have an EventHandler which publishes a more concrete artificial event. This new event could trigger the start of the saga. I would like to avoid such an event as I just need it for technical reasons. It does not have any domain relevance.

Ending the saga immediately is an option, but I guess it might be a good idea to file a GitHub issue as I think there is some potential for improving the framework.

Oliver

Sounds like a plan. Thanks Oliver for coming up with great ideas that can benefit many in the community. :slight_smile:

Done: Starting a Saga conditionally · Issue #1900 · AxonFramework/AxonFramework · GitHub

1 Like