The below is my requirement.
-
There is a screen in which an user enters a file name and clicks on submit.
-
On click of submit , a spring batch job must be triggered. The batch job reads the file and populates a database.
How do we call a spring batch job from a java code(specifically from a struts action class code ) ?
Also,I need to pass the file name(that the user entered on the screen) to the batch program. How do we achieve this ?
JobLauncher
Spring Batch jobs are launched (e.g. run) via a JobLauncher. One of the implementations of the launcher is provided by the framework that you can use outside of the box: SimpleJobLauncher. Take a look at the Configuring a JobLauncher section of the docs
Running Jobs from within a Web Container
While most of the time batch jobs are launched from a command line ( scheduled or not ), there are several ways to do it from the web. Take a look at Running Jobs from within a Web Container section of Spring Batch docs.
The idea is simple. You just call
jobLauncher.runfrom within a controller:This is Spring MVC (not Struts), but you can see it is very simple, and would work for any controller / action class:
jobLauncherandjobare injected => setJobLauncher(…) / setJob(…)jobLauncherruns the job from ajobLauncher.htmlpage (note: the call torundoes not block)Spring Batch Admin
Another way to launch Spring Batch jobs without worrying about Spring MVC and Struts at all is to use a Spring Batch Admin which is there to solve this exact problem and more (monitoring / stopping / etc..)