+ Start a Discussion
Just Code ItJust Code It 

apex scheduler vs. batch apex

Hi, new to batch Apex and got question on Apex Scheduler vs. Batch Apex. I use the term "apex scheduler" to refer to class that implements "Schedulable" interface and "Batch Apex" for class that implements "Database.Batchable" interface.

What's the difference between these two? For example, I can create class that implements "Schedulable" interface, and I would have batch job (use the "Schedule Apex" page to run the batch job). I don't need to create batch Apex class (class that implements "Database.Batchable" interface), which seems to be complex to use. Anyone could shed some light ;> ? Thanks in advance.
Ashish_SFDCAshish_SFDC
Hi, 


Apex Scheduler is used to Schedule a job at a perticular time. 

Ex, Run a class on all records every week once on saturday midnight. 

Where as Batch Apex is used to process a set of 10000s of records in batches and we get notified when it completes and it is not dynamic. 

Ex, Process 50'000 records and notify once done. 

Reply back if you looking for anything more specific. 


Regards,
Ashish
OyeCodeOyeCode
Apex Schedular is class the implements Schedular interface, not this interface has method to schedule a job to run on a provided time. Clearly for schedule some Apex job you need to implement this Schedular interface and this would  be schedular class thereafter

Batch Apex - Any class that implements DataBase.Batchable interface become batch apex, in here the batchable interface has method to process the bulk record in customizable size of batches, any developer can define the batch size, clearly this is used for handling bulk operation to avoid SOQL exception and cloud clearly knows your are using batch class and on finish you will be informed.

http://www.oyecode.com/2011/10/how-to-use-batch-apex-in-salesforce.html
justin_sfdcjustin_sfdc

Hi,
Apex Schedular basically means to schedule that class at some certain time. It can call either a class or a batch class.
 

Whereas batch apex is used when you are dealing with huge number of data. Example; if you want to update 200K of records then you would have to write a batch class to perform that action in which you can state how many records you want to process at a time.

Thanks,
justin~sfdc

willardwillard
There seems to always be confusion between these two.  First some terms:  I'm just going to say Scheduled Apex is shorthand for a class that implements the Schedulable interface.  Batch apex is shorthand for a class that implements the Batchable interface.

Scheduled apex basically allows you to schedule some type of job.  It doesn't do anything else but determine WHEN something runs, i.e., once a day, once a week, every Tuesday and Thursday of each week, etc.  Batch apex allows you to process long-running jobs, which, if run in a normal context may generate limit exceptions.  Usually Scheduled and Batch apex complement each other.  If you have a long-running batch job, you usually want to schedule that to run, say, once a week.

So at a high level, say you have a batch job (this code may not run - I just wrote it off the cuff):
<pre>
global class AccountWrecker implements Database.Batchable {
global Database.QueryLocator start(Database.BatchableContext bc) {
  return [select Id, Name from Account];
}

global void execute(Database.Batchable bc, SObject[] accounts) {
  for (Account[] batch : (Account[]) accounts) {
   for (Account acc : batch) {
    acc.Name += " just got wrecked.";
   }
   update batch;
  }
}

global void finish(Database.Batchable bc) {
  // send out an email or something to the user
}
}
</pre>

Now - you can run this one time by just doing this:
<pre>
Database.executeBatch(new AccountWrecker());
</pre>

However, say you want to have this run every hour.  You obviously don't want to wait by your computer with an alarm clock to execute the code, so you create some Scheduled apex.
<pre>
global class ZFT_GetExchangeRateScheduler implements Schedulable {
global void execute(SchedulableContext sc) {
  Database.executeBatch(new AccountWrecker());
}
}
</pre>

Now just creating the scheduled apex does not actually schedule the job.  It is just the class that Salesforce's apex scheduler knows to how to talk to to start the job.

Now you can actually schedule the job by either writing some anonymous apex using System.schedule, or by navigating in the UI to Develope > Apex Classes and clicking the "Schedule Apex" button

Hope that helps.

willardwillard
Actually just looking over my answer - it is technically wrong to say that Scheduled apex determines WHEN something runs.  It is just the interface that the Apex scheduler knows how to execute.  

In other words, the scheduler itself determines WHEN something runs.  Scheduled apex is the only thing that the scheduler knows HOW to run.
Suraj Tripathi 47Suraj Tripathi 47
Hi Just Code It
Greeting!

Go through this link if you still want to know about the difference between apex scheduler v/s batch apex:
https://www.javapedia.net/Spring-batch-Interview-questions/887#:~:text=Spring%20Batch%20and%20Quartz%20have,provides%20functionality%20for%20scheduling%20tasks.

If you find your Solution then mark this as the best answer. 

Thank you!
Regards,
Suraj Tripathi