3

I have an apex batch class that does not implement Scheduleable Interface. I wish to schdule this batch job to run every 5 minutes of an hour all week. Is it possible to do without implementing the schedulable interface? If so how?

What I know:

I saw there is a method to schedule the apex batch class using - System.scheduleBatch , but this schedules the job to run only once and not every 5 minutes of a hour.

Using the System.scheduleBatch Method You can use the System.scheduleBatch method to schedule a batch job to run once at a future time.

The System.scheduleBatch method takes the following parameters. An instance of a class that implements the Database.Batchable interface. The job name. The time interval, in minutes, after which the job starts executing.

3
  • You may write a small class which implements Schedulable interface and call/execute your batch inside that class. You can schedule that class using cronExpression to be executed every 5 minutes of every hour. OR you don't want to implement the Schedulable interface at all in any class. Commented Jun 19, 2017 at 16:45
  • Exactly. That is what I am doing. Just wanted to know what other work around is there. So guessing there no way to schedule a job without implementing Scheduler class.
    – SfdcBat
    Commented Jun 19, 2017 at 16:48
  • I think that's the only way. But one thing is, you schedule the batch once by calling Database.scheduleBatch class once yourself manually, and then use the same expression in Finish method of the batch class to schedule the batch again to be executed after 5 minutes. Does that make sense? Commented Jun 19, 2017 at 16:53

2 Answers 2

3

You can run a batch with five minutes between each execution using system.scheduleBatch by chaining it in your finish method. It won't run every five minutes, because it also takes some time to execute.

You'll want to add some sort of kill switch so your batch is possible to test.

@TestVisible static Boolean chainBatches = true;
public void finish(Database.BatchableContext context)
{
    String jobName = 'MyJob' + Datetime.now();
    if (chainBatches) system.scheduleBatch(this, jobName, 5);
}
2
  • I guess if I delete the scheduled job then that will be a kill switch.
    – SfdcBat
    Commented Jun 19, 2017 at 18:01
  • @SfdcBat Not in a test context. You'd never be able to execute this batch from a test unless you bake in some way to turn it off within the test.
    – Adrian Larson
    Commented Jun 19, 2017 at 18:02
0

Yes I checked the method, and it's possible that you give after how many minutes from now you want batch class to run. So, it makes full sense to re-run the batch after your last execution is finished. You can do so by putting Database.scheduleBatch(batchable, jobName, minutesFromnow) in your finish method.

It is also beneficial in the manner, that next execution will take place only when last execution is done, and Finish method is getting executed.

public void finish(Database.BatchableContext context)
  {
   System.scheduleBatch(yourClassName, YourjobName, TheValuewillbe5);
 }

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .