I am writing a batch class which should be scheduled to run at a certain frequency. I am putting both interfaces on same class -
global with sharing class testbatch implements Database.Batchable<sObject>,Database.Stateful,Schedulable{
Integer batchChunkSize;
global testBatch(){
//get chunksize value from a custom setting and assign it to variable batchChunkSize.
batchChunkSize = (MyCustom_Setting__c.getOrgDefaults().chunkSize__c).intValue();
}
global Database.QueryLocator start(Database.BatchableContext BC){
//return query;
}
global void execute(Database.BatchableContext BC, List<Application__c> scope){
//do stuff
}
global void finish(Database.BatchableContext BC){}
global void execute(SchedulebleContext SC){
testBatch currentInstanceOfBatch = new testBatch();
Database.executeBatch(currentInstanceOfBatch,currentInstanceOfBatch.batchChunkSize);
}
}
In the very last execute statement, I am confused as to if I should use the second parameter as just batchChunkSize or currentInstaceOfBatch.batchChunkSize.
Both of them are working. I don’t know how, but I want to understand the basics.
Let’s say I schedule this class to run daily. When it's time to run, the last execute statement is run (as far as I understand). At this stage, has an instance of class testBatch already been created? If so, then has the constructor already run (hence, batchChunkSize variable now has the value I want it to be)?
If an instance of class testBatch is already created by the time the last execute statement is run, then within that execute statement, do I need to do
testBatch currentInstanceOfBatch = new testBatch();
or can I do
testBatch currentInstanceOfBatch = this;
This is the first time I am doing both interfaces together, so I am a little confused as to what gets instantiated when.
------ Updating the questions in my head after reading the excellent response from David Reed ----
Please go through things I am describing and point out where I am wrong.
So let's say, this job is scheduled to run everyday at 8:00 AM. When people say "Scheduler instance", what does it mean? When Monday 8:00 AM comes in, will an instance of testBatch be created? Is that what is meant by Scheduler Instance? If yes, let's call it instance1. So instance1 runs at Monday 8:00 AM. execute (SchedulableContext) of Instance1 is called. Now within the execute statement, I create a new instance of this class (call it Instance2), pass it as a parameter to Databse.executeBatch which will kick start the batch job for the day.
When Instance1.execute method is run, in the statement Database.executeBatch, the second parameter can be Instance1 member variable batchChunkSize or Instance2 member variable batchChunkSize. Both should be the same, right?
Now the next time a job is scheduled to run is Tuesday 8:00 AM. This whole process will start again. A new instance of class will be generated on Tuesday 8:00 AM and its execute (SchedulableContext) method will get called. Am I correct? If so, any change in custom setting value of batch size between Monday and Tuesday should reflect on the Tuesday instance.
I know I am all sorts of wrong in assuming when instances are being created. I would appreciate a patient explanation of the concepts. In database.executeBatch, if I pass a second parameter.
----- Final Update ------
The answers provided by David and sfdcfox are excellent. Anyone stumbling on this post, I recommend reading both the answers.