Future Vs Queueable

 Future methods and Queueable Apex are used to perform asynchronous processing, but they have usage and Capabilities

Future :- You can call a future method for executing long-running operations, such as callouts to external Web services or any operation you’d like to run in its own thread, on its own time. 

Execution Limits:- 

. Each execution of future method is treated as a separate transaction with its own set of governor limits.

. Multiple Future methods can be executed concurrently

. we cann't monitor the jobs.

. The specified parameters must be primitive data types, arrays of primitive data types, or collections of primitive data types. 

Limitations:-

. The maximum number of future method invocations per a 24-hour period is 250,000 or the number of user licenses in your organization multiplied by 200, whichever is greater. 

. 50 future method invocations per transaction

. The maximum number of future methods that can be queued for execution is 2000

Example:-

Database.Batchable Interface because of which the salesforce compiler will know, this class incorporates batch jobs.

Database.BatchableContext represents the parameter type of a batch job method and contains the batch job ID

User Case:- 

global class BatchCityandEmailUpdate implement Database.Batchable<sObject>, Database.Stateful{


global Database.QueryLocator start(Database.BatchableContext BC){

  String query = 'Select Id,Name,Email__c,City__c Phone_No__c From Account where Name != null';

  return Database.getQueryLocator(query);

}


 global void execute(Database.BatchableContext BC, List<Account> acc){

 

   list<account> acclistupdate = new list<account>();

  for(account acclist : acc){

   acclist.Email__c = 'bingikumarcrm@gmail.com',

   acclist.City__c = 'Kurnool',

   acclistupdate.add(acclist);

  }

     update acclistupdate;

 }


 global void finish(Database.BatchableContext BC) {

  

  AsyncApexJob a = [SELECT Id, Status, NumberOfErrors, JobItemsProcessed, TotalJobItems,

                    CreatedBy.Email FROM asyncApexJob Where Id =: BC.getJobId()];

  Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

  String[] toAddresses = new String[] {a.CreatedBy.Email};

  mail.setToAddresses(toAddresses);

  mail.setSubject('Apex Sharing Recalculation schedule email update'+ a.status);

  mail.setPlainTextBody('The batch Apex job processed Account update for'+ a.TotalJobItems +

                         'batch with' + a.NumberOfErrors + ' failures.');

  Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});

  }

}

Now you can execute the batch in the same way as before:

BatchCityandEmailUpdate  batchupdate = new BatchCityandEmailUpdate();

Database.executeBatch(batchUpdate);


Queueable

Queueable apex is an asynchronous apex method. It's similar to the @future method. By using this queueable interface, we can process an apex that runs for a long time (such as web service call outs and extensive database operations).

. Queueable supports both primitive and non-primitive data types.

. The specified parameters must be primitive data types, arrays of primitive data types, or collections of primitive data types. 

. we can monitor the jobs based on the job Id.

Use Case:-

Comments

Popular posts from this blog

Java Script Basic