To Schedule the batch class for every 20 seconds need to write 2 class (1 Batch, 1 Schedule).
Once batch is Executed those will have next run this will make conflicts, so immediately it need to be deleted/abort, for those we need to write abort job in the same schedule class.
Batch Class:
global class MyBatchClass implements Database.Batchable<sObject> {
global (Database.QueryLocator | Iterable<sObject>) start(Database.BatchableContext bc) {
// collect the batches of records or objects to be passed to execute
}
global void execute(Database.BatchableContext bc, List<P> records){
// process each batch of records
}
global void finish(Database.BatchableContext bc){
// execute any post-processing operations
}
}
Schedule Class:
global class MyBatchClass implements Database.Batchable<sObject>,schedulable {
global void execute(SchedulableContext SC)
{
doScheduling();
}
public void doScheduling(){
try{
dateTime dt=System.now().addSeconds(20);
String Csec,Cmin,Chr,Cday,Cmonth,CYear;
Csec=String.valueof(dt.second());
Cmin=String.valueof(dt.minute());
Chr=String.valueof(dt.hour());
Cday=String.valueof(dt.day());
Cmonth=String.valueof(dt.month());
CYear=String.valueof(dt.Year());
String SchTimer=Csec+' '+Cmin+' '+Chr+' '+Cday+' '+Cmonth+' ? '+CYear;
system.debug('*************SchTimer:'+SchTimer);
MyBatchClass batchclass = new MyBatchClass();
database.executebatch(batchclass,100);
//we will delete completed apex scheduled jobs for which state is DELETED
for( CronTrigger c:[Select State,Id,EndTime,CronExpression From CronTrigger where
NextFireTime= null AND State='DELETED' Limit 100]){
System.abortJob(c.id);
}
}
catch(exception e){
....your code....
}
}
}
Once batch is Executed those will have next run this will make conflicts, so immediately it need to be deleted/abort, for those we need to write abort job in the same schedule class.
Batch Class:
global class MyBatchClass implements Database.Batchable<sObject> {
global (Database.QueryLocator | Iterable<sObject>) start(Database.BatchableContext bc) {
// collect the batches of records or objects to be passed to execute
}
global void execute(Database.BatchableContext bc, List<P> records){
// process each batch of records
}
global void finish(Database.BatchableContext bc){
// execute any post-processing operations
}
}
Schedule Class:
global class MyBatchClass implements Database.Batchable<sObject>,schedulable {
global void execute(SchedulableContext SC)
{
doScheduling();
}
public void doScheduling(){
try{
dateTime dt=System.now().addSeconds(20);
String Csec,Cmin,Chr,Cday,Cmonth,CYear;
Csec=String.valueof(dt.second());
Cmin=String.valueof(dt.minute());
Chr=String.valueof(dt.hour());
Cday=String.valueof(dt.day());
Cmonth=String.valueof(dt.month());
CYear=String.valueof(dt.Year());
String SchTimer=Csec+' '+Cmin+' '+Chr+' '+Cday+' '+Cmonth+' ? '+CYear;
system.debug('*************SchTimer:'+SchTimer);
MyBatchClass batchclass = new MyBatchClass();
database.executebatch(batchclass,100);
//we will delete completed apex scheduled jobs for which state is DELETED
for( CronTrigger c:[Select State,Id,EndTime,CronExpression From CronTrigger where
NextFireTime= null AND State='DELETED' Limit 100]){
System.abortJob(c.id);
}
}
catch(exception e){
....your code....
}
}
}
No comments:
Post a Comment