Skip to content

Step Execution Context not available on RepeatInterceptor's open method [BATCH-125] #3450

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
spring-projects-issues opened this issue Sep 5, 2007 · 7 comments

Comments

@spring-projects-issues
Copy link
Collaborator

Andres Bernasconi opened BATCH-125 and commented

The StepExecutionContext is not available to RepeatInterceptor's open method. I created a RepeatInterceptor in order to have a step initialization routine, but I need the step name (or other configuration) and it is not available. This is when setting the repeatInterceptor to the stepOperations RepeatTemplate of the SimpleStepExecutor.

Since my Step only performs one cycle in the stepOperations iterator I thought to put the interceptor in the chunkOperations iterator of the Executor. The problem here is that the Context is emptied by the time it reaches the chunk operations.

(Maybe a bug in CompositeCompletionPolicy.java:126?. It seems that the main context passed as parameter it is not being taken into account, but I am not sure). The fact is that I lose all the properties of the RepeatContext in the chunk operations, and the StepExecutionContext is lost because of that.

Let me know if I there's the need for more info on this.

Regards
AB


Affects: 1.0-m2

Attachments:

Referenced from: commits efc6247

@spring-projects-issues
Copy link
Collaborator Author

Andres Bernasconi commented

Debugging the code in more detail it seems that when calling the getAttribute method on the (automatically created) CompositeCompletionPolicy$CompositeBatchContext object, it delegates the call to SynchronizedAttributeAccesor, which in turn call the SynchronizedAttributeAccesor$1(AttributeAccessorSupport) class, which has nothing in its attributes private property. I don't have the sources downloaded from some of the classes so I am not sure why this property is empty. Thought this could help.

Regards
AB

@spring-projects-issues
Copy link
Collaborator Author

Dave Syer commented

Do you have a test case that demonstrates the problem that you can attach? The SimpleStepExecutor attaches the StepExecutionContext and the Step to the RepeatContext of the step. Are you saying it is missing in your case? (N.B. when BATCH-90 is resolved the StepExecutionContext might be merged with the StepExecution.)

@spring-projects-issues
Copy link
Collaborator Author

Andres Bernasconi commented

Dave, here is the example project.

If you debug SimpleStepExecutor you will notice that the RepeatInterceptor's open method is called before the STEP_KEY (stepExecutionContext) is set, so it makes sense why it is not available in the StepOperations RepeatTemplate. But I do not understand why this is the case in the ChunkOperations RepeatInterceptor. Also notice that the tasklet is not being able to get some of the properties set in the interceptors (more precisely in the StepOperations RepeatInterceptor).

Here is the description of the test:
Both the StepOperations and the ChunkOperations have the same RepeatInterceptor, Bug125RepeatInterceptor. This interceptor first prints the name of the property being set (confifurable), then sets a value to such attribute, and after that tries to retrieve the StepExecutionContext from the RepeatContext and prints it.

Then there is the Bug125Tasklet that prints the properties set in the Interceptors, and also prints the StepExecutionContext available.

Here is the output of an execution:

Setting Attribute value [bug125Attribute-StepOpsInterceptor]
The StepContext executing in [StepOperations] is: null
Setting Attribute value [bug125Attribute-ChunkOpsInterceptor]
The StepContext executing in [ChunkOperations] is: null
bug125Attribute-StepOpsInterceptor has value: [null]
bug125Attribute-ChunkOpsInterceptor has value: [valueReceived]
The StepContext in the Tasklet is: null

As always consider the possibility of a bug in my test, or a misconception on my behalf on how the batch should behave.

Best Regards
AB

@spring-projects-issues
Copy link
Collaborator Author

Andres Bernasconi commented

This is the test case for this bug (see comment #4)

@spring-projects-issues
Copy link
Collaborator Author

Dave Syer commented

I see the problem - the stepOperations interceptor doesn't get the StepExceution in its open method because it isn't added until the first iteration. The chunkOperations, however, should have the context you need, but in the parent RepeatContext (the chunk is a child of the step). That means you have a workaround for your case, but I'm leaving this open in case we can address the more general case.

One way might be by adding a standard interceptor to the stepOperations, but I don't like that because then the semantics of whose interceptor gets invoked when becomes messy, and hard to understand.

@spring-projects-issues
Copy link
Collaborator Author

Dave Syer commented

Changes made as part of BATCH-121 will have fixed this - the StepContext will now contain the StepExecution before the iteration starts (so available to the open method in an interceptor at the stepOperations). StepContext is available to a step-scoped bean if it implements StepContextAware, or using the StepSynchronizationManager (prefer StepContextAware if possible). This is actually generically pretty useful for all sorts of purposes, hence the link to an apparently unrelated issue.

@spring-projects-issues
Copy link
Collaborator Author

Dave Syer commented

Assume closed as resolved and released

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant