Friday 17 April 2015

Writing Apex Scheduled Batch class - Framework!

Basics:
1. Apex schedulers are implemented to do data/logical processing at a given time.

2. Apex batch classes are implemented to process LDV (Large Data Volume) for given objects.

3. For implementing a schedule class you need to implement "Schedulable" interface

4. For implementing a batch class you need to implement "Database.Batchable" interface

5. You can make a single class both Schedulable and Batchable so you don't need to have 2 separate class for batch and schedule. Kindly see below code for this.

6. Standard batch size is 200 and it can be modified/changed when you invoke Database.executeBatch method to call batch class. (second parameter of this method is batchsize where first param is instance of class). Maximum value of size can be 2000 and minimum should be greater than ZERO.

7. Apex batch processing is "Asynchronous" mechanism so no guarantee on time of processing

8. When you schedule something at 7am for example, it is not guranteed that it will execute at same time. Your job actually pushed to Salesforce queue for same at 7am and Salesforce will pick it up based on resources available on Salesforce Cloud (Servers). So in nutshell, actual execution might be delayed based on service availability.

9. You can only have 100 scheduled Apex jobs at one time. You can evaluate your current count by viewing the Scheduled Jobs page in Salesforce and creating a custom view with a type filter equal to “Scheduled Apex”. You can also programmatically query the CronTrigger and CronJobDetail objects to get the count of Apex scheduled jobs.

10. When implementing test class for your scheduled batch class, you should have all assertions after Test.stopTest() as this is an asynchronous processing.

11. Governor limits for batch and scheduled apex applied separately. So, all scheduled apex limits apply for batch jobs scheduled using System.scheduleBatch. After the batch job is queued (with a status of "Holding" or "Queued"), all batch job limits apply and the job no longer counts toward scheduled apex limits.

Let's get on code! If you want to process Account records by a scheduled batch..

How your code framework should be for AccountBatch?


Some important considerations:
1. Your batch size ideally should fall under 50-100 but there is no standard rule for this. Batch size is really defined based on how many DML statements on different objects you have or how many callouts your are doing from your method?
2. The start(), execute(), and finish() methods can implement up to 10 webservice callouts each. Time for each callout is 120 seconds. (Documentation Link: https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_batch_interface.htm)

But this limit is "100" now. You can make 100 callouts from a batch.

Serial Batch processing:
This is very common scenario where you need to execute batch classes on different object right one after the other. How can you achieve that? It is very simple; you can call next batch in finish() method of your current batch as finish method is actually for doing something after work done is committed by your batch.

What is Database.Stateful?
This is an interface used to maintainsstate across transactions. When using Database.Stateful, only instance member variables retain their values between transactions. Static member variables don’t and are reset between transactions. If you don’t specify Database.Stateful, all static and instance member variables are set back to their original values.

What is Apex Flex Queue?
It is used to submit up to 100 batch jobs without getting an error. The batch job is placed in the Apex flex queue, and its status is set to "Holding". This feature is released in Spring'15 and it is great improvement over Apex batch queue. 

No comments:

Post a Comment