How to schedule a batch class after every X mins?
I had recently a issue arised where I had to schedule the apex batch job but in the experience cloud the batch job was not getting due to technical limitation.
I had tried all the mechanism to execute the batch like, provide the permission set to the user, permission at the profile level which is providing the batch class access to the profile. BUT BATCH DIDN’T GOT EXECUTED THROUGH EXPERIENCE CLOUD.
So came to an alternative where I scheduled the batch using a scheduling class.
The solultion requires that, it checks first if any other existing job is executed and lined up for the next execution, ABORT it and then schedule the another job in the finish method of the batch class.
global without sharing class AsynchronousProcessBatch implements Database.Batchable<sObject>,Database.AllowsCallouts,Database.Stateful{
global Database.QueryLocator start(Database.BatchableContext BC) {
String query = 'select query that fulfils the number of records here';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC,List<SObject> scope){
system.debug('scope :' + scope);
// Your BUSINESS LOGIC CLASS CALLED HERE
MYBusinessClass bgClass = new MYBusinessClass();
bgClass.myBusinessMethodCalled();
}
global void finish(Database.BatchableContext BC){
//Abort the existing job
Set<Id> crnJobDtSet = new Set<Id>();
List<CronJobDetail> cjDetailList = [SELECT Id, Name, JobType FROM CronJobDetail WHERE Name LIKE 'DocRqStarted at%'];
for(CronJobDetail cjd : cjDetailList){
crnJobDtSet.add(cjd.Id);
}
Set<Id> cronTriggerId = new Set<Id>();
List<CronTrigger> cronTriggerList = [SELECT Id, CronJobDetailId,CronJobDetail.Name, PreviousFireTime, State, StartTime,
EndTime, CronExpression, CreatedDate, TimesTriggered FROM CronTrigger WHERE CronJobDetailId IN :crnJobDtSet AND TimesTriggered > 0];
Set<Id> cronJobIds = (new Map<Id, CronTrigger>(cronTriggerList)).keyset();
for(Id cronId : cronJobIds){
System.abortJob(cronId);
}
String hour = String.valueOf(Datetime.now().hour());
//You can add any no of Minute you want to add to schedule it for next Here it is 2 mins
String min = String.valueOf(Datetime.now().addMinutes(2).minute());
String ss = String.valueOf(Datetime.now().second());
//parse to a cron expression
String nextFireTime = ss + ' ' + min + ' ' + hour + ' * * ?';
//Create an Instance of your Schedule Class
ScheduleMyJob s = new ScheduleMyJob();
System.schedule('DocRqStarted at' + String.valueOf(Datetime.now()), nextFireTime, s);
}
}
Also we need to create a schedulable class which calls this batch. So this way the batch is continuously running after every X minutes. Also, cleaning up the jobs that are already executed.
global without sharing class SchedulerJob implements Schedulable{
global void execute(SchedulableContext sc)
{
AsynchronousProcessBatch b = new AsynchronousProcessBatch();
database.executeBatch(b, 1); // if you need to increase the chunk size then you can put any other number
}
}
Peace ✌️
You can schedule a batch class to run on a regular basis by using the scheduling tools supplied by your programming language. After defining the batch class and its job logic, configure the scheduling system to launch the batch class every X minutes. This can be done via cron jobs, task schedulers, or built-in scheduling tools. For example, Java’s Timer and ScheduledExecutorService classes can be used. Ensure that errors are dealt with properly and that the batch class is completed on time. For advice on controlling temperature and maximising fuel efficiency in respect to insulating an offset smoker, go to https://trackacourier.com/leman-tracking/.
Use the scheduling features provided by your programming language to plan a batch class to run periodically. Set up the scheduling mechanism to trigger the batch class every X minutes after you have defined it and its task logic. Cron jobs, task schedulers, or in-built scheduling tools can all be used to accomplish this. You can use the Timer or ScheduledExecutorService classes, for instance, in Java. Make sure errors are handled correctly and the batch class is finished on time. Visit this https://schoenhalde.de/preise/tierbestattungen-im-landkreis-reutlingen-und-tuebingen-abholung/ for tips on temperature management and fuel efficiency in relation to insulating an offset smoker.
To schedule a batch class to run after every X minutes, you can leverage the scheduling functionality available in your programming language or development platform. The exact implementation may vary depending on the technology you are using. Generally, you would need to define the batch class and its logic to execute a specific task or process. Then, set up a scheduling mechanism to trigger the batch class at the desired interval, in this case, X minutes. This scheduling can be accomplished through cron jobs, task schedulers, or built-in scheduling features provided by your development environment. For example, in a Java application, you could use the Timer or ScheduledExecutorService classes to schedule the batch class to run periodically. Remember to handle any necessary error handling and ensure that the batch class completes its execution within the desired timeframe. As an unrelated example, if you’re looking for information on how to insulate an offset smoker, you can visit this helpful link: https://www.juicyburn.com/how-to-insulate-an-offset-smoker/. It provides detailed instructions on insulating your offset smoker to maintain consistent temperatures, improve fuel efficiency, and optimize your smoking experience.