018_异步_Schedule
类似于Windows Schedule Job;
有两种方式的用法:
直接上代码:
①:
public class AccountSchedula implements Schedulable {
public void execute(SchedulableContext sc) {
System.debug('ceshi schedula');
}
}
测试:@isTest
public class TestAccountSchedule {
static testMethod void myUnitTest() {
String executeTime = '0 0 16 * * ?';
AccountSchedula goodsSchedule = new AccountSchedula();
System.schedule('batch goods',executeTime,goodsSchedule);
}
}
直接run test 便可以看到Log 上的debug信息
②:
global class dldcApprovalofTimecardScheduled implements Schedulable {
global static void execute(SchedulableContext sc) {
dldc_Timecard_Approval_Reminder.Approval();
}
}
global class dldc_Timecard_Approval_Reminder{
global static void Approval() {
}
}
在Apex class 中找到dldcApprovalofTimecardScheduled 并且点击schedula ,在页面上上就可以设置执行的时间,不过都是半点或者是整点,看个人喜好选择这两种方式
global class GoodSchedule implements
Database.Batchable<sObject>, Database.Stateful {
// instance member to retain state across transactions
global Integer recordsProcessed = 0;
global Database.QueryLocator start(Database.BatchableContext bc) {
return Database.getQueryLocator(
'SELECT ID,GoodsDescribe__c, GoodsBrand__c, GoodsName__c FROM Goods__c WHERE GoodsName__c like \'%小米%\''
);
}
global void execute(Database.BatchableContext bc, List<Goods__c> goods){
for(Goods__c god : goods){
god.GoodsDescribe__c = 'Nice2';
}
Database.upsert(goods,false);
}
global void finish(Database.BatchableContext bc){
System.debug(recordsProcessed + ' records processed. Shazam!');
AsyncApexJob job = [SELECT Id, Status, NumberOfErrors,
JobItemsProcessed,
TotalJobItems, CreatedBy.Email
FROM AsyncApexJob
WHERE Id = :bc.getJobId()];
// call some utility to send email
// EmailUtils.sendMessage(a, recordsProcessed);
}
public void sendEmail(String Msg){
// Send Email to Admin
Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
message.setToAddresses(new String[] { '##.com' });
message.setSubject('Group Upsert');
message.sethtmlBody('Error:'+Msg);
Messaging.sendEmail(new Messaging.Email[] {message});
}
}
手动执行代码:
GoodSchedule sa = new GoodSchedule(); Id batchId = Database.executeBatch(sa);
此刻,静下心来学习

浙公网安备 33010602011771号