Asynchronous Apex : Future Methods, Batch Apex

Asynchronous Apex

Future Methods
runs asynchronously and the calling method is not bothered about the return of this method. 
it is used when calling the external web service
it runs in the background
gets executed when the resources are available
it has higher governor limits like SOQL limits and heap size limits
in the global class, we use @Future annotation and the method must be static and returns only VOID(Nothing)
We can not pass the sObject as a parameter to future method because this gets executed later, the sObject may get changed in the meanwhile
add callout=true for calling external services
The future method can not invoke another future method
governor limits:
max 250000 or number of users multiplied by 200 per day whichever is greater
Testing:
future methods are called within startTest() and stopTest(). The result is collected by stopTest() and the method is executed synchronously.
Best practices:
do not add a large number of future methods
optimize the execution time, minimize the callouts 
try using batch apex if there are a huge number of records to be processed 

Queueable Apex
chaining of the jobs
Allows to add asynchronous jobs and monitor them. using System.enQueueJob method
implement Queueable interface
gives id for a job, with which we can monitor
Governor limits:
can add 50 jobs to a queue using system.enQueueJob
maximum of 5 jobs can be chained
only one child can be chained to one job.
Testing:
queueable jobs are called within startTest() and stopTest(). The result is collected by stopTest() and the method is executed synchronously.

Batch Apex
when there are a huge number of rows to be processed we go for Batch apex
the records are divided into small chunks and processed.
We need to implement database.batchable interface
there are three methods
start
returns database.querylocator and takes the database.batchableContext as a parameter
gets executed only once
this method is global 
execute
this returns void but takes database.batchableContext and the list of sObjects as parameters
gets executed multiple times based on the number of records and the batch size
finish
this returns void takes database.batchableContext as parameter
database.executeBatch returns the job id which can be used for tracking
Governor Limits:
5 batch jobs can be queued or active concurrently
100 Holding batch jobs can be held in the Apex flex queue
per 24-hour period is 250,000, or the number of user licenses in your org multiplied by 200—whichever is greater
A maximum of 50 million records can be returned in the Database.QueryLocator
database.executeBatch can have a maximum value of 2,000
the start, execute, and finish methods can implement up to 100 callouts each.

Scheduled Apex

to schedule an apex class at a particular schedule time by implementing the schedulable interface
two ways to schedule one from schedule apex page(front end), two code(system.schedule)
global or public execute method needs to be implemented with void return type
we can track the progress as it returns the job id
Governor limits:
only 100 schedulable jobs at a time
max 250000 or number of users multiplied by 200 per day whichever is greater
Testing:
called within startTest() and stopTest(). Result is collected by stopTest() and the method is executed synchronously.
Best practices:
the job may start or not at the scheduled time based on the resources
be cautious when scheduling from a trigger, it may increase the number
no asynchronous web service call allowed, to achieve this we can call a future method with callout=true

1 comment:

  1. CNC machining processes can be carried out in-house—if the company invests in acquiring and maintaining their own CNC equipment—or out-sourced to devoted CNC machining service suppliers. As its name implies, a CNC water jet cutter uses high-pressure jets of water to cut stylus pen via supplies. Computer numerical control expertise controls the sequence of movement of the water jet to create the desired completed part.

    ReplyDelete

Powered by Blogger.