samedi 7 mars 2015

How to find any of previously running job/task is not running in spring batch? I hv tried some code but not sure

I have written a spring-batch job, which I execute using scheduler at every 10 mins. Now this job is mainly has task of transferring data from one database to another database.


The problem I am facing is sometimes there could be huge amount of data which may require the job execution more than 10 mins. In such cases I want to detect that is there any job already running or not? If not then only I will do the rerun of a new job instance. If that job is already running then I would skip the "new job instance invokation". I have some code written which is as follow.



Job job = (Job) context.getBean("job");

JobExplorer explorer = (JobExplorer) context.getBean("jobExplorer");

// Fetching all job instances.
List<JobInstance> jobInstances = explorer.findJobInstancesByJobName(
job.getName(), 0, explorer.getJobInstanceCount(job.getName()));

// Fetching the last job instance.
JobInstance lastJobInstance = jobInstances.get(0);

//Obtain all executions for last job instance.
List<JobExecution> executions = explorer.getJobExecutions(lastJobInstance);

// fetching last job execution
JobExecution lastJobExecution = executions.get(0);

System.out.println("Is my last Job running? \nAns:"+lastJobExecution.isRunning());


A few things, this code is to check previously all running jobs. what I am assuming is that If I start the server then no other job would be running before the server start and If I run a job after checking previous job running or not I assume no other job will be invoked in my scheduler code.


I want to know that is this a correct way to do so? I mean to make sure you run only 1 job instance for the same job at same time and that too after completion of any previous runs for the same job.


P.S The job instance and jObExplorer and jobLauncher are injected inside the scheduler. This is just a similar code and hence getting beans using context.


Thank you for reading.


Aucun commentaire:

Enregistrer un commentaire