A way to programmatically retrieve saga associations?

I know you can view the association_value_entry table in the DB but this only allows you to see it for a point in time.

What is the best way we can retrieve a Saga’s associations programmatically from within a @SagaEventHandler?

Reasons for wanting to do this:

  • make debugging easier during Saga development
  • log a DEBUG or TRACE message with these values.

We have SagaLifecycle.associateWith(...), is it worth having a SagaLifecycle.associatedWith() which returns the association values (maybe a List<AssociationValue>)?

Hi vab2048! Thanks for posting your question.

The associated values currently not easily retrievable. We have a Github issue on this, and this might be picked up. in a following release.

For now, the best way to do this is using a Handler Enhancer to access the Saga instance and put the values in the resources of the unit of work. This way you can access them later.

For example, you could do this:

public class AssociationPropertyHandlerEnhancer<T> extends WrappedMessageHandlingMember<T> {

        protected AssociationPropertyHandlerEnhancer(MessageHandlingMember<T> delegate) {
            super(delegate);
        }

        @Override
        public Object handle(Message<?> message, T target) throws Exception {
            if (target instanceof Saga<?>) {
                CurrentUnitOfWork.get().resources().put("associations", ((Saga<?>) target).getAssociationValues());
            }
            return super.handle(message, target);
        }
    }

Now in your handlers you can access the association values:

CurrentUnitOfWork.get().getResource("associations");

I hope this helps!

1 Like

Hi @vab2048 , I just wanted to let you (and others that read this) know that after our conversation here we have decided to make it easier.

Starting with version 4.6.0 you will be able to retrieve the associates values using SagaLifecycle, per the PR created here: [#1125] Introduce `SagaLifecycle.associationValues()` by Morlack · Pull Request #2141 · AxonFramework/AxonFramework · GitHub

1 Like

Wonderful - thank you. A great reason to upgrade when it comes out :slight_smile: