How to get variable from subprocess

Please tell me, the correct way to get a variable from the subprocess by Java API … is from the subprocess and not from the main process.
Now I’m in the controller of Spring , I get the first ActivityInstance[], then the execution list and only the variable sub-process.
Perhaps there is a way easier, for example, as it is done through a JavaDelegate.
Tank you.

Can you please give an example process model pointing out the subprocess and the task that contains the delegate?

In subprocess …

    List<DataForm> dataForms = dataFormRepository.findByForm(form);
    dataForms.forEach(dataForm -> {
        List<UserActionLink> userActions = new ArrayList<>();
        subProcessDataCollection.add(new SubProcessData(dataForm.getId(), userActions));
    });

    delegateExecution.setVariable("subProcessDataCollection", subProcessDataCollection);

here I get n subprocesses where n is equal to the number of elements in the collection,
In each subprocess there is a variable of the SubProcessData type, it change at each step by listeners,

Now I extract it as follows ,
ActivityInstance[] activityInstances = runtimeService
.getActivityInstance(processInstance.getId())
.getActivityInstances(SUBPROCESS_NAME);

    for (ActivityInstance activityInstance : activityInstances) {
        List<Execution> executions = runtimeService.createExecutionQuery()
                .executionId(activityInstance.getExecutionIds()[0])
                .list();
        SubProcessData subProcessData = (SubProcessData) runtimeService
                .getVariable(executions.get(0).getId(), SUBPROCESS_DATA_VARIABLE_NAME);

Is it possible to do the same thing more easily, for example, as it is done in delegates

public class Check implements ExucutionListner {
 @Override
 public void notify(DelegateExecution delegateExecution) {
  SubProcessData sub = (SubProcessData ) delegateExecution.getVariable(SUBPROCESS_DATA_VARIABLE_NAME);
}
}

I believe this is the simplest solution. When you use #getVariable in a JavaDelegate, the activity instance context is implicitly given. If you are not in a JavaDelegate, you have to navigate to the correct activity instance and execution as you do in the first code sample.

Cheers,
Thorben

Thank you.