How to pause a job in quartz scheduler ?

Do you want to pause or suspend a method of a class which is getting executed via quartz scheduler based on the cron expression ? Let’s say you wanted to pause the method being executed by the cron job based on the business/ technical condition that you have.

Quartz job – DBSyncJob

For example let us consider that you have implemented a database sync job as shown below

package com.sneppets.scheduled;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sneppets.app.DBSync;

public class DBSyncJob implements Job{

	static Logger logger = LoggerFactory.getLogger(CPSyncJob.class);
	
	DBSync dbSync= new DBSync();
	
	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException {		
		try {

		  dbSync.synAllDocuments();
			
		} catch (Exception e) {		
			logger.error("DBSyncJob :{}", e.getMessage());
		}		
	}

}

And let’s say the scheduler executes dbSync.syncAllDocuments() for every 5 mins once as shown in the below code sneppet.

package com.sneppets.scheduled;

import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sneppets.util.MyScheduler;

public class MySchedulerService{
	
	static Logger log = LoggerFactory.getLogger(MySchedulerService.class);
	
	MyScheduler myScheduler = new MySchedulerImpl();
	
	public MySchedulerService(){
		super();
		init();
	}

	void init(){
		myScheduler .register(DBSyncJob.class, "0 */5 * ? * *");
		
		log.info("**************************MySchedulerService");
		
		try {			
			new DBSyncJob().execute(null);
		} catch (JobExecutionException e) {
			log.error("MySchedulerService:{}", e.getMessage());
		}
		
	}
}

To pause the DBSyncJob without any complex logic, what you can do is declare a static boolean variable (default value is “true”) and set that boolean variable false based on the required business/ technical condition (for eg., if database is being updated by some other service, then don’t execute this method till that completes).

In the below updated DBSyncJob code, you can declare a static boolean variable called dbSyncEnabled and set default value as true and create getters and setters for that.

Pause a job in quartz scheduler:

package com.sneppets.scheduled;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sneppets.app.DBSync;

public class DBSyncJob implements Job{

	static Logger logger = LoggerFactory.getLogger(CPSyncJob.class);
	
	DBSync dbSync= new DBSync();
  
    static boolean dbSyncEnabled = true;
	
	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException {		
		try {
			if(isDBSyncEnabled()){
				dbSync.synAllDocuments();
            }
			
		} catch (Exception e) {		
			logger.error("DBSyncJob :{}", e.getMessage());
		}		
	}
 
    public boolean isDBSyncEnabled() {
    	return dbSyncEnabled;
	}

	public static void setDBSyncEnabled(boolean dbSyncEnabled) {
		DBSyncJob.dbSyncEnabled= dbSyncEnabled;
	}

}

By calling setDBSyncEnabled(), you can control dbSyncEnabled static boolean variable and pause or suspend the dbSync.synAllDocuments() method call by quartz job. Hope this helped 🙂

Further Learning:

References:

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments