Spawning multipe jobs from within a Job
Hi,
I have the need to start several jobs asynchronously after processing some input.
All this is to be done from the ItemProcessor of a Job step.
If I attempt to do the above, I get an error to the tune of: Transaction already started... set the @Transactional annotation on the client..
Looking at the namespace config, it seems that it is not possible to start a Job from within another Job. It is possible to start multiple steps, tho'
However, I want to start an indeterminate number of jobs because I would like to monitor them using JobExplorer and manage their starting/stopping using JobOperator. These niceties are not available to me if I use Steps.
Can someone suggest another way to spawn "jobs" programitacally that can then be monitored using the JobOperator interface?
Thanks!
Spawjning multipe jobs from within a Job
Dave,
Thanks for the reply and the suggestion to use a JobStep.
However, the way I am determining the granularity of my jobs and steps is that restartable units are deemed as 'jobs' because those are what are exposed by the JobOperator class, not the JobSteps.
So given that I have to be able to restart/stop these units, the only option I have is to spawn jobs, not steps.
As you pointed out there is a setTransactionalAttribute() method on TaskletStep. But that will not help as I was looking for a similar API on the Job interface instead.
Here is the code that I am trying and failing with the "Transaction already started... set the @Transactional annotation on the client.." issue.
Code:
public class SplitFilesTasklet implements Tasklet {
//jobRepository and jobLauncher are injected
//Tried setting TransactionalAttribute as below but that doesn't help
//@Transactional(propagation = Propagation.SUPPORTS)
public RepeatStatus execute(StepContribution contribution,
ChunkContext chunkContext) throws Exception {
Map<String, JobParameter> parameters = new HashMap<String, JobParameter>();
parameters.put("timestamp", new JobParameter(new Date().getTime()));
SimpleJob job = null;
for (int i = 0; i < 5; i++) {
job = new SimpleJob("test-" + i);
TaskletStep taskletStep = new TaskletStep();
taskletStep.setName("step");
taskletStep.setJobRepository(jobRepository);
taskletStep.setTransactionManager(transactionManager);
Tasklet tasklet = new SingleFileTasklet();
taskletStep.setTasklet(tasklet);
job.addStep(taskletStep);
job.setJobRepository(jobRepository);
JobExecution jobExecution = jobLauncher.run(job,parameters);
}
}
}
The above code is in a class called SplitFilesTasklet that is itself configured to be launched using the SimpleAsynchExector and a QuartzScheduler.
When the jobLauncher attempts to launch the job above, it issues the exception.
Thanks!