An ItemProcessor, as defined by Java Batch contains only one method: processItem(Object item)
What I am missing are lifecycle events like open() and close() - similar to the methods on ItemReader or ItemWriter. I mimicked open() where processItem(item) will detect it is called for the first time and trigger a call to my open method.
But as close() is still missing I notice more complex ItemProcessors that need to cleanup after use have no chance of doing so. I tried to add the interface ChunkListener to benefit from the afterChunk() method, but at least in JBeret and BatchEE implementations that method is not called on a chunk's processor. It is the same for StepListener. Registering the ItemProcessor as Listener will just create another instance of the same class and cleanup will fail again.
Also I tried to use the finalize() method, but that seems not sufficient/reliable either.
If ItemProcessor is not intended to cleanup after use, how would I have to design the application such that complex decisions (which may require to connect to other systems like database) can be taken?
-
1One thing to consider would be using JBeret's StepScoped jberet.gitbooks.io/jberet-user-guide/content/custom_cdi_scopes and refactoring the more complex stuff (requiring cleanup) into a bean that you could then inject to and invoke from each of the ItemProcessor and ChunkListener events. Technically you can do this kind of thing with StepContext without CDI, but with only the two transient/persistent UserData objects it can get a bit messy if you'd rather work with >1 object.Scott Kurz– Scott Kurz2025年09月12日 14:11:36 +00:00Commented Sep 12, 2025 at 14:11
-
A StepScoped bean. Indeed something to consider. Although I need to be careful - my batch is running multithreaded partitions.queeg– queeg2025年09月12日 19:44:54 +00:00Commented Sep 12, 2025 at 19:44
-
I'd think the PartitionedScoped bean would work then since the chunk is local to a given partition.Scott Kurz– Scott Kurz2025年09月12日 22:22:22 +00:00Commented Sep 12, 2025 at 22:22
-
I investigated on JBeret custom scopes and am checking more on CDI now. Seems in combination with @PreDestroy I am getting a cleanup.queeg– queeg2025年09月14日 08:22:09 +00:00Commented Sep 14, 2025 at 8:22
1 Answer 1
Jakarta Batch treats all batch artifacts as managed beans. CDI allows them to use events like
@PostConstruct
public void postConstruct() {
...
}
@PreDestroy
public void preDestroy() {
...
}
With them the necessary cleanup can be performed after use and before garbage collection kicks in.