Monday 2 April 2018

Schedule multiple batch classes using single Schedule class

Schedule multiple batch classes using single Schedule class

Hi all,
Today we are going to discuss how we can schedule different batch classes for different time using single Schedule class. Till now I was creating new schedule class for each of my batch class.

GlobalScheduler:

/* Name : GlobalScheduler Date : 30 march 2018 Author : Arpit vijayvergiya Description : We are creating this class as global scheduler. So multiple batchclass can be scheduled with single scheduler. */ public class GlobalScheduler implements Schedulable{ public Database.Batchable<sObject> batchToBeSchedule; public Integer batchSize; public GlobalScheduler(Database.Batchable<sObject> batchToBeSchedule , Integer batchSize){ this.batchToBeSchedule = batchToBeSchedule; this.batchSize = batchSize; } public void execute(SchedulableContext sc){ Database.executeBatch(batchToBeSchedule,batchSize); } }

AccountBatch:

/* Name : AccountBatch Date : 30 march 2018 Author : Arpit vijayvergiya Description : We create this batch class to test GlobalScheduler class. */ global class AccountBatch implements Database.Batchable<sObject>{ global Database.QueryLocator start(Database.BatchableContext dbc){ return Database.getQueryLocator([SELECT Id,Name FROM Account LIMIt 10]); } global void execute(Database.BatchableContext dbc, List<SObject> listOfAccount) { system.debug('Account'+ listOfAccount); } global void finish(Database.BatchableContext dbc){ System.debug('Account batch finish'); } }


ContactBatch:

/* Name : ContactBatch Date : 30 march 2018 Author : Arpit vijayvergiya Description : We create this batch class to test GlobalScheduler class. */ global class ContactBatch implements Database.Batchable<sObject>{ global Database.QueryLocator start(Database.BatchableContext dbc){ return Database.getQueryLocator([SELECT Id,lastName FROM Contact LIMIt 10]); } global void execute(Database.BatchableContext dbc, List<SObject> listOfContact) { system.debug('Contact '+ listOfContact); } global void finish(Database.BatchableContext dbc){ System.debug('contact batch finish'); } }
How to schedule: We can not schedule this class using standard Salesforce UI. This is the only disadvantage I found till now. We need to schedule it using Apex code. We need to make a proper cron expression for that. There are some online tools available for creating cron expression. ie. CronMaker.

If you scheduled a batch class using this scheduler class and then again you need to schedule another batch class at the same time or different time. It will not affect any of our scheduled jobs.

Here is the snippets for scheduling single scheduler multiple time with multiple batches.




AccountBatch accBatch = new AccountBatch(); // Batch Class Name GlobalScheduler scheduler = new GlobalScheduler(accBatch,100); String sch = 00 + ' ' + 57 + ' ' + 15 + ' * * ?'; // this cron schedule batch class to be run at 3 pm 57 minute on daily basis System.schedule('Account Batch Scheduler', sch, scheduler); ContactBatch conBatch = new ContactBatch(); // Batch Class Name GlobalScheduler scheduler1 = new GlobalScheduler(conBatch,200); String sch1 = 00 + ' ' + 56 + ' ' + 15 + ' * * ?'; // this cron schedule batch class to be run at 3 pm 56 minute on daily basis System.schedule('Contact Batch Scheduler', sch1, scheduler1);

Thanks,


No comments:

Post a Comment