Description | A generic class for scheduled batch operations. Instead of requiring several new classes (batchable, schedulable, test...) for each job, this design allows you to add batch jobs as nested classes within an existing Apex class. The effect is to greatly reduce the number of Apex classes and amount of boiler-plate code. BatchJob handles all the plumbing (debug logging, exception logging, etc) and delegates the interesting work (your business logic) to your code. To use this model, create a class that extends BatchJob.Handler and override start, execute, and optionally, finish.
public class Accounts {
public class Cleaner extends aBoost.BatchJob.Handler {
public Iterable<SObject> start(Database.BatchableContext context) {
return new aBoost.Query(Account.SObjectType)
.whereOp(Account.Purge__c, '=', true)
.locator();
}
public void execute(Database.BatchableContext context, SObject[] scope) {
delete scope;
}
}
}
Usage:
// execute an account cleaner job
new Accounts.Cleaner().run();
// execute a batch with a custom batch size
Database.executeBatch(new aBoost.BatchJob(new Accounts.Cleaner()), 5);
// schedule the job with a custom batch size, starting in 10 minutes
System.scheduleBatch(new aBoost.BatchJob(
new Accounts.Cleaner(), 5), 'cleaning...', 10);
// schedule the job with a custom batch size; run every day at 1pm
System.schedule('cleaning...', '0 0 13 * * ?', new aBoost.BatchJob(
new Accounts.Cleaner(), 5));
In addition to Batchable and Schedulable, this class implements Database.AllowsCallouts , Database.RaisesPlatformEvents , and Database.Stateful , which provide extra functionality. Any uncaught exceptions thrown by a BatchJob will be logged by Logger. |
---|