Apex Scheduler Class

First apex scheduler class:

global class Scheduler01 implements Schedulable {
    global void execute(SchedulableContext SC)    
    {
        //Call other class method which will perform operations as it is
        //recommended that all processing take place in a separate class
        doScheduling();
    }
    public void doScheduling(){
       try{
            dateTime dt=System.now().addMinutes(10); //you can specify 10 mins or 15    
                                                                              //mins or any interval
            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);
            Scheduler02 cas = new Scheduler02();
            system.schedule('Scheduler02: Running at '+System.now().format(), SchTimer, cas);
            //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){
        }
   }
}

Second apex Scheduler Class:

global class Scheduler02 implements Schedulable {
    global void execute(SchedulableContext SC){
        //Call other class method which will perform operations as it is
        //recommended that all processing take place in a separate class
        doScheduling();
    }
    public void doScheduling(){
      try{
            dateTime dt=System.now().addMinutes(10);
            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);
            Scheduler01 cas = new Scheduler01();
            system.schedule('Scheduler01: Running at '+System.now().format(), SchTimer, cas);
            //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){
        }
    }

}

No comments:

Post a Comment