Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I kickoff a batch job when input file arrives?

We have Spring4 and Spring Batch 3 and our app consumes CSV files as input file. Currently we kick off the jobs manually from the command line, using CommandLineJobRunner with parms, including the name of the file to process.

I want to kick off a job to process asynchronously just as soon as the input file arrives in a monitored directory. How can we do that?

like image 900
JamesD Avatar asked Oct 16 '25 14:10

JamesD


2 Answers

You may use java.nio.file.WatchService to monitor directory for a file. Once file appears you may start (or kick off a job to process asynchronously) actual processing.

You may also use FileReadingMessageSource.WatchServiceDirectoryScanner from Spring Integration (https://docs.spring.io/spring-integration/reference/html/files.html#watch-service-directory-scanner)

Comparing release notes Spring Batch https://github.com/spring-projects/spring-batch/releases to Spring Integration https://github.com/spring-projects/spring-integration/releases it looks that Spring Integration is released more often. It also has more features and Integration points.

In this case it looks like a overkill to bring Spring Integration if you just need to watch a directory for a file.

like image 176
Bartosz Bilicki Avatar answered Oct 18 '25 07:10

Bartosz Bilicki


I would recommend using the powerful combination of Spring Batch with Spring Integration. For example, you can use a FileInboundChannelAdapter from Spring Integration to monitor a directory and start a Spring Batch Job as soon as the input file arrives.

There is a code example for this typical use case in the reference documentation of Spring Batch here: https://docs.spring.io/spring-batch/4.0.x/reference/html/spring-batch-integration.html#launching-batch-jobs-through-messages

I hope this helps.

like image 25
Mahmoud Ben Hassine Avatar answered Oct 18 '25 07:10

Mahmoud Ben Hassine



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!