+ Start a Discussion
PathikritPathikrit 

Scheduling Troubles with Batch Apex

Hi Guyz,

I am running into a scheduling problem with my apex class. I have a batch apex class named 'MyBatchApex' which I want to schedule to run every 5 minutes. I created a class with implements the Schedulable interface and here goes the code..
 
global class MySchedulerClass{
    global void execute(SchedulableContext sc) {
        ID BatchId = Database.executeBatch(new MyBatchApex(), 50);

    } 
    public static void scheduleThis(){
       String cronString='0 5 * * * ?';
       System.schedule('MyJob', cronString, new MySchedulerClass());
    }
}

To schedule this, I am running the following snipper from Developer Console 
MySchedulerClass.scheduleThis()

The job creates a async job and executes the batch after 5 mins as expected and then stops to run. I want this to execute every 5 minutes like a cron job. Am I doing anything wrong here? How to acheive a frequency of 5 min execution then?
Best Answer chosen by Pathikrit
Vivek_PatelVivek_Patel
Hi Pathikrit,

For your scenario, you will need 12 schedules as follows.

cronString='0 5 * * * ?';
cronString='0 10 * * * ?';
cronString='0 15 * * * ?';
cronString='0 20 * * * ?';
cronString='0 25 * * * ?';
cronString='0 30 * * * ?';
cronString='0 35 * * * ?';
cronString='0 40 * * * ?';
cronString='0 45 * * * ?';
cronString='0 50 * * * ?';
cronString='0 55 * * * ?';
cronString='0 00 * * * ?';

Regards,
Vivek Patel.

Please like or mark as best answer if this resolves your issue to benefit others finding the correct answer and improve the quality of developer forum.
 

All Answers

PathikritPathikrit
Missed this in my first post..
When I try to to run the job a second time from Developer Console, I get the following exception:
system.asyncexception: MyJob is already scheduled for execution
Again, when I query the ApexAsyncJob object, I don't see any jobs scheduled, queued or aborted...
Pablo VenturinoPablo Venturino
I think the problem is your cron expression, "0 5 * * * ?", actually means "Fire at the 5th minute of every hour".
The cron expression you're looking for would be "0 0/5 * * * ?"
(taken from http://docs.oracle.com/cd/E12058_01/doc/doc.1014/e12030/cron_expressions.htm)
PathikritPathikrit
Hi Pablo,

Thanks for that. I thought I was doing something wrong. 
I tried the cron expression you provided and I'm getting this error while running this from Developer Console.

System.StringException: Seconds and minutes must be specified as integers: 0 0/5 * * * ?

Any idea on this?
Vivek_PatelVivek_Patel
Hi Pathikrit,

For your scenario, you will need 12 schedules as follows.

cronString='0 5 * * * ?';
cronString='0 10 * * * ?';
cronString='0 15 * * * ?';
cronString='0 20 * * * ?';
cronString='0 25 * * * ?';
cronString='0 30 * * * ?';
cronString='0 35 * * * ?';
cronString='0 40 * * * ?';
cronString='0 45 * * * ?';
cronString='0 50 * * * ?';
cronString='0 55 * * * ?';
cronString='0 00 * * * ?';

Regards,
Vivek Patel.

Please like or mark as best answer if this resolves your issue to benefit others finding the correct answer and improve the quality of developer forum.
 
This was selected as the best answer
PathikritPathikrit
Thanks Vivek. That works I know. I was hoping to submit only job instead of 12. Do you know if this is the only way to acheive this? Not having a cron feature in schedule job seems to me to be a big big big drawback in apex.
Sergey TrusovSergey Trusov
Following cron string work for me:
"0 */5 * * * ?"
Sergey TrusovSergey Trusov
http://www.thegeekstuff.com/2011/07/cron-every-5-minutes/
Vivek_PatelVivek_Patel
Hi Pathikrit,

I had faced the similar problem and that's the only resolution available I think, other cros expression do not work in salesforce, they are okay for java or other language.

Regards,
Vivek Patel.

Please like or mark as best answer if this resolves your issue to benefit others finding the correct answer and improve the quality of developer forum.
PathikritPathikrit
Hi Sergey,

This expression doesn't seem to work in Apex. I have previously worked on Java and used cron expression like this before. But Apex throughs error (System.StringException: Seconds and minutes must be specified as integers: 0 */5 * * * ?) when you try to use this as your cron expression. 
Can you give a little insight on how were you able to do this?
PathikritPathikrit
Hi Vivek_Patel,

I guess that is the only way for me now. Thanks for the help.