异步apex注意事项

本文深入探讨了Salesforce平台上的FutureMethods与QueueableApex,详细讲解了它们的用途、语法注意事项及限制。FutureMethods适用于外部Web服务调用、资源密集型操作等场景,而QueueableApex则提供了非原始类型参数、作业监控和任务链等额外功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1 Future Methods
以@future作为提示符开始

通常能做什么?

Future methods are typically used for:

  • Callouts to external Web services. If you are making callouts from a
    trigger or after performing a DML operation, you must use a future or
    queueable method. A callout in a trigger would hold the database
    connection open for the lifetime of the callout and that is a “no-no”
    in a multitenant environment.
  • Operations you want to run in their own thread, when time permits
    such as some sort of resource-intensive calculation or processing of
    records.
  • Isolating DML operations on different sObject types to prevent the
    mixed DML error. This is somewhat of an edge-case but you may
    occasionally run across this issue. See sObjects That Cannot Be Used
    Together in DML Operations for more details.

语法注意事项:
Future Method Syntax
必须是静态方法
只能返回VOID类型
传入参数只能是原始数据类型,数组(原始数据类型),集合(原始数据类型)
不能是standard object 和custom object

Future methods must be static methods, and can only return a void type. The specified parameters must be primitive data types, arrays of primitive data types, or collections of primitive data types. Notably, future methods can’t take standard or custom objects as arguments. A common pattern is to pass the method a List of record IDs that you want to process asynchronously.

global class SomeClass {
  @future
  public static void someFutureMethod(List<Id> recordIds) {
    List<Account> accounts = [Select Id, Name from Account Where Id IN :recordIds];
    // process account records to do awesome stuff
  }
}

Future methods只有在系统有可用资源时候才执行。
Remember, future methods are executed when system resources become available.

重要!!!
Future methods不能保证按照调用的顺序执行(原因参照上条),如果想严格按照顺序执行,请考虑Queueable Apex ,两个Future methods还可能同时执行,对结果会有影响。
It’s important to note that future methods are not guaranteed to execute in the same order as they are called. Again, future methods are not guaranteed to execute in the same order as they are called. If you need this type of functionality then Queueable Apex might be a better solution. When using future methods, it’s also possible that two future methods could run concurrently, which could result in record locking and a nasty runtime error if the two methods were updating the same record.

不能用在Visualforce controllers 的getMethodName(), setMethodName(),也不能用在构造器中
不能在一个future method中调用另一个future method,也不能用trigger去调用另一个future method。
future method中不能使用getContent() and getContentAsPDF() methods
每个apex调用future method上限是50个,24小时上限是50个。

  • Future methods can’t be used in Visualforce controllers in
    getMethodName(), setMethodName(), nor in the constructor.
  • You can’t call a future method from a future method. Nor can you
    invoke a trigger that calls a future method while running a future
    method. See the link in the Resources for preventing recursive future
    method calls.
  • The getContent() and getContentAsPDF() methods can’t be used in
    methods with the future annotation.
  • You’re limited to 50 future calls per Apex invocation, and there’s an
    additional limit on the number of calls in a 24-hour period. For more
    information on limits, see the link below.

2 Queueable Apex
Queueable Apex allows you to submit jobs for asynchronous processing similar to future methods with the following additional benefits:
Queueable Apex也可以执行job,跟future methods相比有以下额外的好处
Non-primitive types: Your Queueable class can contain member variables of non-primitive data types, such as sObjects or custom Apex types. Those objects can be accessed when the job executes.
参数可以不是原始数据类型,比如 sObjects or custom Apex types.
Monitoring: When you submit your job by invoking the System.enqueueJob method, the method returns the ID of the AsyncApexJob record. You can use this ID to identify your job and monitor its progress, either through the Salesforce user interface in the Apex Jobs page, or programmatically by querying your record from AsyncApexJob.
可监控。
Chaining jobs: You can chain one job to another job by starting a second job from a running job. Chaining jobs is useful if you need to do some sequential processing.
先后执行多个相关联的job

Things to Remember
注意事项
Queueable Apex is a great new tool but there are a few things to watch out for:
The execution of a queued job counts once against the shared limit for asynchronous Apex method executions.
队列作业的执行一次根据异步Apex方法执行的共享限制计数
You can add up to 50 jobs to the queue with System.enqueueJob in a single transaction.
一个事务中您可以将最多50个作业添加到队列中。
When chaining jobs, you can add only one job from an executing job with System.enqueueJob, which means that only one child job can exist for each parent queueable job. Starting multiple child jobs from the same queueable job is a no-no.
在链接作业时,您只能从使用System.enqueueJob添加一个作业。enqueueJob,这意味着对于每个父队列作业,只能存在一个子作业。从同一队列作业启动多个子作业是不允许的。
No limit is enforced on the depth of chained jobs, which means that you can chain one job to another job and repeat this process with each new child job to link it to a new child job. However, for Developer Edition and Trial orgs, the maximum stack depth for chained jobs is 5, which means that you can chain jobs four times and the maximum number of jobs in the chain is 5, including the initial parent queueable job.
原则上链接多少个job是没限制的,但是Developer Edition and Trial orgs上,最大深度是5,包括初期父亲queueable job+4个自己的job.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值