BatchableJob & Hooks

Estimated reading: 3 minutes 6 views

Introduction

The BatchableJob Interface defines all the methods that a Class must be implement in order to be used by the Batch Manager.

This interface does not only allow you to define your batch logic but also:

  • Specify a function to filter the desired requests to process from the batch.
  • Customize any of the available Hooks. This Hooks will be automatically called in it’s respective moment.

The Interface

public interface BatchableJob {

    Batch_Request__c processRequest(Batch_Request__c req);
    String getRequestsFilter();

    void beforeExecuteJob(Id jobId);
    void afterExecuteJob(Id jobId);

    void beforeValidateJob(Id jobId);
    void afterValidateJob(Id jobId);

    void beforePrepareJob(Id jobId);
    void afterPrepareJob(Id jobId);

    void beforeStartThread();

    void beforeExecuteThread(Id jobId, Id threadId,  List<Batch_Request__c> requests);
    void afterExecuteThread(Id jobId, Id threadId,  List<Batch_Request__c> requests);

    void beforeFinishThread(Id jobId, Id threadId);
    void afterFinishThread(Id jobId, Id threadId);
}

The Implementation

You are free to do whatever you want inside the different Interface Methods, but it’s important to remark that you shouldn’t persist / save any update in the source request and you should just return the original request with the changes that you consider.

Correct processRequest Implementation

  public Batch_Request__c processRequest(Batch_Request__c req){
        req.Status__c = 'Completed';
        return req;
    }

Incorrect processRequest Implementation

  public Batch_Request__c processRequest(Batch_Request__c req){
        req.Status__c = 'Completed';
        update req; // THIS IS WRONG!
        return req;
    }

Interface Example Implementation

This is a very simple implementation but it should be valid as an illustrative example.

public with sharing class BatchableTestImplementation implements BatchableJob {

    public Batch_Request__c processRequest(Batch_Request__c req){
        req.Status__c = 'Completed';
        return req;
    }

    public String getRequestsFilter(){
        return '';
    }

    public void beforeExecuteJob(Id jobId){}
    public void afterExecuteJob(Id jobId){}

    public void beforeValidateJob(Id jobId){}
    public void afterValidateJob(Id jobId){}

    public void beforePrepareJob(Id jobId){}
    public void afterPrepareJob(Id jobId){}

    public void beforeStartThread(){}

    public void beforeExecuteThread(Id jobId, Id threadId,  List<Batch_Request__c> requests){}
    public void afterExecuteThread(Id jobId, Id threadId,  List<Batch_Request__c> requests){}

    public void beforeFinishThread(Id jobId, Id threadId){}
    public void afterFinishThread(Id jobId, Id threadId){}

    private class CalculationJobException extends Exception{}
}

The Hooks

The hooks allow to extend the core Batch Manager logic. It’s possible to add custom logic, validation, data manipulation, etc in different parts of the batch execution.

JobExecutor Hooks

With these Hooks it’s possible to execute custom logic at the Orchestrator level: before starting the process, adding custom validations, preparing data for the bath execution, etc.

flowchart LR
  beforeExecuteJob-->beforeValidateJob-- "Core Validations" -->afterValidateJob-->beforePrepareJob-- "Core Job Preparation" -->afterPrepareJob-->getRequestsFilter
Hook Name Description
beforeExecuteJob TODO
beforeValidateJob TODO
afterValidateJob TODO
beforePrepareJob TODO
afterPrepareJob TODO
getRequestsFilter TODO

BatchJob Hooks

With the BatchJob Hooks you can add custom logic to the Thread Execution. For instance, adding custom requests filters, executing prestart actions, etc.

flowchart LR
beforeStartThread--"Request to process are retrieved"-->beforeExecuteThread--"excute the requests"-->afterExecuteThread-->beforeFinishThread--"Update Thread Stats"-->afterFinishThread
Hook Name Description
beforeStartThread TODO
beforeExecuteThread TODO
afterExecuteThread TODO
beforeFinishThread TODO
afterFinishThread TODO

Leave a Comment

       
Euphoria, forever till the end of times

Euphoria

Share this Doc

BatchableJob & Hooks

Or copy link

CONTENTS