批处理类Apex中的执行混乱 [英] Execution Confusion in Batch class Apex

查看:167
本文介绍了批处理类Apex中的执行混乱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在调用一个批处理类实例,并且在完成该批处理之后,我正在调用另外两个批处理类实例.第一批批次的finish()方法是

I am calling a batch class instance and after completing the batch, I am calling two other batch class instances. The finish() method for first batch class is

public void finish(Database.BatchableContext BC)
{
    List<Event__c> events = [SELECT Id FROM Event__c];
    delete events;

    System.debug('Executing finish');
    for (CalendarSettings__c c: [SELECT Id, Name, CalendarId__c,
                                       CalendarQuery__c, FieldToDisplay__c
                                FROM CalendarSettings__c])
    {
        System.debug('Calendar Id is' + c.CalendarId__c);
        BatchPublicCampaignsToGoogle bjob = new BatchPublicCampaignsToGoogle(
                c.CalendarQuery__c, c.CalendarId__c, c.FieldToDisplay__c);
        Database.executeBatch(bjob,9);
    }

}

我面临的问题是我希望Batch类BatchPublicCampaignsToGoogle将被调用两次,但只能被调用一次.该循环运行了两次(我分析了调试日志).为什么只叫一次?

The problem I am facing is I am expecting that Batch class BatchPublicCampaignsToGoogle will be called two times but it is called only one time. The loop is running for two times (I analysed debug log). Why it is called only one time?

推荐答案

链式批处理作业是Winter 13的一项功能.发布说明:

Chaining Batch Jobs was introduced as a feature in Winter 13. From the release notes:

从另一个批处理作业开始批处理作业

您现在可以开始批处理 通过从 批处理类的finish方法.这使您可以链接批次 创造就业机会链.请注意,调速器限制为 批处理作业仍然适用.此更改适用于使用以下命令保存的批处理Apex Salesforce.com API版本26.0及更高版本.以前,保存了Apex 使用Salesforce.com API 25.0及更低版本,您无法调用 任何批处理Apex方法中的Database.executeBatch.版本号 使用的是正在运行的批处理类的版本,该版本开始另一个 批处理作业.如果正在运行的批处理类中的finish方法调用a 帮助程序类中的方法来启动批处理作业,Salesforce.com 辅助程序类的API版本无关紧要. 来源

Starting a Batch Job from Another Batch Job

You can now start a batch job from another batch job by calling Database.executeBatch from the finish method of the batch class. This allows you to link your batch jobs and create a chain of jobs. Note that the governor limits of batch jobs still apply. This change applies to batch Apex saved using Salesforce.com API version 26.0 and later. Previously, with Apex saved using Salesforce.com API version 25.0 and earlier, you couldn’t call Database.executeBatch from within any batch Apex method. The version used is the version of the running batch class that starts another batch job. If the finish method in the running batch class calls a method in a helper class to start the batch job, the Salesforce.com API version of the helper class doesn't matter. Source

因此,对于使用API​​ 26.0及更高版本的Apex类,您可以将一个批处理作业链接到刚刚完成的一个作业的末尾.完成一项后,您将无法分支并启动多个批处理作业.

So for Apex classes using API version 26.0 and later you can chain one batch job onto the end of one that just finished. You can't branch out and start multiple batch jobs when one finishes.

这篇关于批处理类Apex中的执行混乱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆