首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >Salesforce 异步处理 Queueable Apex

Salesforce 异步处理 Queueable Apex

原创
作者头像
repick
发布2021-11-27 18:31:48
发布2021-11-27 18:31:48
1.6K0
举报
文章被收录于专栏:SalesforceSalesforce

前边我们分别讲了Batch Apex,Future方法,他们都是异步进程,都可以在自己的线程运行,除了上述两个方法,还有一种异步进程处理方式,就是QueueableApex,它是通过使用可排队接口控制异步 Apex进程。使用此接口,可以将作业添加到队列并对其进行监视。与使用Future方法相比,使用该接口是运行异步Apex代码的增强方式。长时间运行的顶点进程(如大量数据库操作或外部 Web 服务标注)可以通过实现可排队接口并将作业添加到Apex作业队列来异步运行,异步Apex作业在其自己的线程中在后台运行,并且不会延迟主Apex逻辑的执行,每个排队的作业在系统资源变为可用时运行,如果 Apex 事务回滚,则不会处理排队等待事务执行的任何可排队作业。

Queueable Apex例:

代码语言:javascript
复制
public with sharing class AsyncExecutionExample implements Queueable{
    public void execute(QueueableContext context) {
        Account accItem = new Account(Name='Acme',Phone='(415) 555-1212');
        insert accItem;
        system.debug('>>>>>>>>>>accItem>>'+accItem);
    }
}

调用Queueable Apex,并查看Job信息。

代码语言:javascript
复制
ID jobID = System.enqueueJob(new AsyncExecutionExample());
AsyncApexJob jobInfo
    = [SELECT Status,NumberOfErrors FROM AsyncApexJob WHERE Id=:jobID];
system.debug('>>>>>>>>AsyncApexJob>>>>'+jobInfo);

测试类:

代码语言:javascript
复制
@isTest
public with sharing class AsyncExecutionExampleTest {
    static testmethod void test1() {
        // startTest/stopTest block to force async processes
        //   to run in the test.
        Test.startTest();
        System.enqueueJob(new AsyncExecutionExample());
        Test.stopTest();

        // Validate that the job has run
        // by verifying that the record was created.
        // This query returns only the account created in test context by the
        // Queueable class method.
        Account acct = [SELECT Name,Phone FROM Account WHERE Name='Acme' LIMIT 1];
        System.assertNotEquals(null, acct);
        System.assertEquals('(415) 555-1212', acct.Phone);
    }
}

Chaining Jobs

若要在某个其他处理首先由另一个作业完成某些其他处理后运行作业,可以链接可排队作业。要将一个作业链接到另一个作业,请从可排队类的execute()方法提交第二个作业。只能从正在执行的作业中添加一个作业,这意味着每个父作业只能存在一个子作业。例如,如果有第二个名为 AsyncExecutionSecondJob的类来实现可排队接口,则可以在 execute() 方法中将此类添加到队列中,如下所示:

代码语言:javascript
复制
public with sharing class AsyncExecutionSecondJob implements Queueable{
    public void execute(QueueableContext context) {
        Account firstAccount = [SELECT Name,Phone FROM Account WHERE Name='Acme' LIMIT 1];
        system.debug('>>>>>>>>>>firstAccount>>'+firstAccount);

        Account secondAccount = new Account(Name='Acme2',Phone='(415) 666-1212');
        insert secondAccount;
        system.debug('>>>>>>>>>>secondAccount>>'+secondAccount);
    }
}
代码语言:javascript
复制
public with sharing class AsyncExecutionExample implements Queueable{
    public void execute(QueueableContext context) {
        Account accItem = new Account(Name='Acme',Phone='(415) 555-1212');
        insert accItem;
        system.debug('>>>>>>>>>>accItem>>'+accItem);

        // Chain this job to next job by submitting the next job
        System.enqueueJob(new AsyncExecutionSecondJob());
    }
}

调用Queueable Apex,并查看Job信息。

代码语言:javascript
复制
ID jobID = System.enqueueJob(new AsyncExecutionExample());
AsyncApexJob jobInfo
    = [SELECT Status,NumberOfErrors FROM AsyncApexJob WHERE Id=:jobID];
system.debug('>>>>>>>>AsyncApexJob>>>>'+jobInfo);

Queueable Apex Limits

1.在单个事务中,最多可以使用 System.enqueueJob 向队列中添加 50 个作业。在异步事务中(例如,从批处理 Apex 作业),只能使用 System.enqueueJob 将一个作业添加到队列中。要检查在一个事务中添加了多少个可排队作业,请调用Limits.getQueueableJobs().

2.由于对链接作业的深度没有强制限制,因此可以将一个作业链接到另一个作业。可以对每个新的子作业重复此过程,以将其链接到新的子作业。对于开发人员版和试用版组织,链接作业的最大堆栈深度为 5,这意味着可以链接作业四次,链中的最大作业数为 5,包括初始父可排队作业。

3.使用 System.enqueueJob 链接作业时,只能从正在执行的作业中添加一个作业。每个父可排队作业只能存在一个子作业。不支持从同一可排队作业启动多个子作业。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Queueable Apex例:
  • Chaining Jobs
  • Queueable Apex Limits
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档