Batch Executor

Estimated reading: 2 minutes 4 views

Introduction

The BatchJobExecutor class is designed to handle the execution of batch jobs in Salesforce.

It implements the Schedulable interface, allowing it to be scheduled for execution at specific times. The class defines several constants to represent different statuses of a batch job, such as SUCCESS_STATUS, FAILED_STATUS, DISTRIBUTING_STATUS, and INPROGRESS_STATUS.

It also sets limits for the maximum number of threads and batch sizes.

Constructors

The class has two constructors:

  • A default constructor and one that accepts a job ID.
  • The latter constructor initializes the batchableJob by querying the Batch_Jobc record and instantiating the appropriate class based on the Batchable_Job_Classc field.

If an error occurs during this process, a custom exception BatchJobExecutorException is thrown.

Execute Method

The execute method is the main entry point for executing a batch job.

It first checks if a job ID is provided. If not, it queries for jobs with a status of ‘Scheduled’ and selects the first one. It then attempts to instantiate the batchableJob class. Depending on the execution mode of the job, it either prepares a high-volume job or executes the job directly.

The prepareHighVolumeJob method is responsible for setting up a high-volume job by calculating the number of requests and distributing them across multiple threads.

It updates the job status to ‘Distributing’ and uses the Database.executeBatch method to execute the batch requests.

The executeHighVolumeJob method is called to execute a high-volume job. It updates the job status to ‘In Progress’, prepares the job, and executes the job threads.

After execution, it calls the afterExecuteJob method on the batchableJob.

The executeJob method handles the execution of a job by validating and preparing the job, distributing requests across threads, and executing the job threads. It ensures that the number of threads does not exceed the maximum allowed.

The class also includes several helper methods: getThreadsForJob, executeJobThreads, validateJob, prepareJob, getBatchThreads, and distributeRequestsForJob. These methods assist in managing and executing the batch job threads, validating and preparing the job, and distributing requests.

Finally, the class defines a custom exception BatchJobExecutorException to handle errors specific to the batch job execution process.

Leave a Comment

       
Euphoria, forever till the end of times

Euphoria

Share this Doc

Batch Executor

Or copy link

CONTENTS