Reputation: 503
Is there a way to get the running job(s) that I've started before in a Controller? Here I have the controller and the task executor configuration for the launcher:
@Controller
@RequestMapping(value = "/action/file")
public class ArquivoController {
@Autowired
private JobLauncher jobLauncher;
@Autowired
@Qualifier(value = "jobValidateFile")
private Job jobValidateFile;
@RequestMapping(value = "/validate", method = RequestMethod.GET)
public @ResponseBody Map<String, Object> validate(@RequestParam Long fileId, Principal user) {
Map<String, JobParameter> parameters = new HashMap<String, JobParameter>();
parameters.put("fileId", new JobParameter(fileId));
JobExecution execution = this.jobLauncher.run(this.jobValidateFile, new JobParameters(parameters));
return new ResponseBuilder().toSuccessResponse();
}
@RequestMapping(value = "/status", method = RequestMethod.GET)
public @ResponseBody Map<String, Object> seeStatus(@RequestParam Long fileId) {
// Get the running job status here.
return new ResponseBuilder().toSuccessResponse();
}
}
<bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
<property name="jobRepository" ref="jobRepository" />
<property name="taskExecutor">
<bean class="org.springframework.core.task.SimpleAsyncTaskExecutor" />
</property>
</bean>
As you can see, I put the launcher as an asynchronous task. What I'm trying to do, is to get the batch status little by little. For example, I'm going to make a javascript who will call the "seeStatus" method every minute.
Thanks.
Upvotes: 3
Views: 8373
Reputation: 11449
We have following status in Spring Batch
COMPLETED, STARTING, STARTED, STOPPING, STOPPED, FAILED, ABANDONED, UNKNOWN;
You can use :
import org.springframework.batch.core.JobExecution;
@Autowired
JobLauncher jobLauncher;
JobExecution execution = jobLauncher.run(job, new JobParameters());
System.out.println("Status : " + execution.getStatus());
Upvotes: 0
Reputation: 11353
You can take a look at how they do it in Spring Batch Admin too, the JobExecutionController in particular.
Upvotes: 0
Reputation: 21473
You have a couple options:
JobExplorer#findRunningJobExecutions(String jobName)
. This will only work, however, if you have the job names.I'd recommend option 1.
You can read more about the JobExplorer
here: http://docs.spring.io/spring-batch/apidocs/org/springframework/batch/core/explore/JobExplorer.html
Upvotes: 3