Apex BOOST Library
v1.7 from Lucidware Solutions (LWS)

BatchJob

global with sharing class BatchJob implements Database.Batchable, Database.AllowsCallouts, Database.RaisesPlatformEvents, Database.Stateful, Schedulable
DescriptionA 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.

Constructors

  BatchJob

global BatchJob(Handler handler)
DescriptionCreates a batch job with the specified handler. The resulting object can be passed to one of the System batch or schedule methods.
ThrowsInvalidParameterValueException if handler is null

  BatchJob

global BatchJob(Handler handler, Integer batchSize)
DescriptionCreates a batch job with the specified handler and batch size. The resulting object can be passed to one of the System batch or schedule methods.
ThrowsInvalidParameterValueException if handler is null or batchSize is <= 0

BatchJob.Handler

global inherited sharing virtual class Handler
DescriptionExtend this class to provide the logic for a schedulable batch job. See the BatchJob documentation for details and usage.

Methods

  run

global void run()
DescriptionExecutes the batch job immediately, using the default batch size.