1

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?

asked Sep 12, 2025 at 8:34
4
  • 1
    One 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. Commented 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. Commented Sep 12, 2025 at 19:44
  • I'd think the PartitionedScoped bean would work then since the chunk is local to a given partition. Commented 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. Commented Sep 14, 2025 at 8:22

1 Answer 1

0

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.

answered Oct 4, 2025 at 21:05
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.