1

I'm working on a spring project that has n maven jobs. I have a cron expression for any job. I want to check if a job is already running before I create a new cron job. How can I do that?

I take one job to show you an example. In quartz-context.xml I define the beans, set the cron expression property that takes the value from a properties file, the group property, and the job detail that is a class used to set the job parameters. This is the class:

public class DataImportJobDetail extends AbstractJobDetail {

    @Override
    protected JobParameters setupJobParameters(JobParameters jobParameters, 
            JobExecutionContext context, String jobName) {
        JobParametersBuilder builder = new JobParametersBuilder(jobParameters);

        builder.addString(ConfigManager.PARAM_MODE, ConfigManager.MODE_PERSIST);
        return builder.toJobParameters();
    }
} 

I think I have to create a control here by getting the currently executing jobs list and compare it with the jobName?

Hulk
  • 6,399
  • 1
  • 30
  • 52
Michael Cauduro
  • 185
  • 1
  • 3
  • 16
  • this is a link to another similar problem, but here they used singleton pattern but i don't understand where i have to use it. – Michael Cauduro Jan 02 '19 at 14:00
  • https://stackoverflow.com/questions/11843981/quartz-scheduler-execute-job-only-if-it-is-not-already-running/54007426?noredirect=1#comment94851407_54007426 – Michael Cauduro Jan 02 '19 at 14:01

1 Answers1

1

Approach 1: You can store Job Details in database and you can check which jobs are running if you will use spring batch it do the same task(Maintain job status in database).

Approach 2: If you want to just check job is running or not you can create a temp file when job starts and delete when job finish.If file exist means job is running else not running.

vivekdubey
  • 484
  • 2
  • 7
  • yes, i have also a class and a method that control in database if the job is present and is running giving jobName, but i don't know if i can do this controll in the class above? sorry but i'm a junior programmer and is the first time i approach with spring/maven – Michael Cauduro Jan 02 '19 at 11:14