I have once raised similar question before but seems that discussion move to other topic 
I have some job that may be invoked concurrently (i.e. same Job is invoked multiple times at the same time, with different parameter of course)
In my tasklet, reader or writer, we may have to keep stateful information in it during execution. As all these beans are singleton beans, we cannot have the same Job launched multiple times concurrent as the stateful information inside will be messed up.
So, is the following the best practice suggested by Spring Batch?
For each job submission, create a new App Context (probably use current App Context as parent), and get the job launcher from it and run the Job. i.e., one app context per job execution.
That is correct. The samples contain two examples of this pattern - the quartz sample and the jmx one (TaskExecutorLancher). Both use a custom JobFactory also in the samples currently.
adrianshum,
Can you please share your information on how you are invoking that same job concurrently? Small code snippet would be helpful.
Thanks!
Originally Posted by hailspringadrianshum,
Can you please share your information on how you are invoking that same job concurrently? Small code snippet would be helpful.
Thanks!
Nothing special in fact...
Just having a Message Driven Pojo, which get the Job Launcher and invoke the job:Code:
public class JobRequestWorker implements MessageListener { // member variables
public void onMessage(Message msg) { if (msg instanceof ObjectMessage) { SubmitNewJobRequest req = (SubmitNewJobRequest) msg; Job job = this.jobLocator.getJob(req.getJobName()); this.jobLauncher.run(job, req.getJobParameters()); } }
}
something like this in high level.
Originally Posted by Dave SyerThat is correct. The samples contain two examples of this pattern - the quartz sample and the jmx one (TaskExecutorLancher). Both use a custom JobFactory also in the samples currently.
Taken that approach already (using ClassPathXmlApplicationContextJobFactory). Any plan of putting ClassPathXmlApplicationContextJobFactory to distribution as it seems to be quite a standard way to do. |