044_Schedule Job 间隔时间自动执行

需求:系统上的标准功能是能够设置间隔一天的执行,或者是写完代码着急测试我们写个5分钟后执行的;

但是遇到要求没间隔一小时或者十分钟执行,该怎么处理呢?

global class **_RetrieveInquiryBatchtest implements Database.Batchable<sObject>, Database.AllowsCallouts, Database.Stateful, Schedulable {

String query = 'SELECT Id, Inquiry_Type__c, Status__c, Interaction_Number__c, Interaction_Id__c FROM **_Inquirys__c limit 1';
private **_SOA_Settings__c SOASettings = **_SOA_Settings__c.getOrgDefaults();
private List<**_Inquiry.Interaction> interactions;


public **_RetrieveInquiryBatchtest() {

}

public static void runBatch() {
**_SOA_Settings__c SOASettings = **_SOA_Settings__c.getOrgDefaults();
**_RetrieveInquiryBatchtest retrieveBatch = new **_RetrieveInquiryBatchtest();
Id jobId = Database.executeBatch(retrieveBatch, 1);
//Id jobId = Database.executeBatch(retrieveBatch, (Integer)SOASettings.Get_MIR_Statuses_Batch_Limit__c);
}

global void execute(SchedulableContext scMain) {
**_RetrieveInquiryBatchtest retrieveBatch = new **_RetrieveInquiryBatchtest();
Id jobId = Database.executeBatch(retrieveBatch, (Integer)SOASettings.Get_MIR_Statuses_Batch_Limit__c);
}

global Database.QueryLocator start(Database.BatchableContext BC) {
return Database.getQueryLocator(query);
}

global void execute(Database.BatchableContext BC, List<**_Inquirys__c> scope) {

System.debug('----------------------------------');
}

global void finish(Database.BatchableContext BC) {
Datetime now = System.now().addMinutes(1);
String month = string.valueOf(now.month());
String hour = string.valueOf(now.hour());
String day = string.valueOf(now.day());
String minute = string.valueOf(now.minute());
String second = string.valueOf(now.second());
String year = string.valueOf(now.year());
String strSchedule = '0 ' + minute + ' ' + hour + ' ' + day + ' ' + month + ' ?' + ' ' + year;
try {
String strJobName = 'Retrieve GMIP Status ' + System.now().format();
System.schedule(strJobName , strSchedule, new **_RetrieveInquiryBatchtest());
} catch (AsyncException e) {
System.debug(e.getMessage());
// create by ** 2016-06-21 -------- monitor the Batch ----------------------------START
}


List<CronTrigger> cronList = [SELECT Id, State FROM CronTrigger WHERE State = 'DELETED'];
for (CronTrigger cron : cronList){
System.abortJob(cron.Id);
}
// Get the ID of the AsyncApexJob representing this batch job
// from Database.BatchableContext.
// Query the AsyncApexJob object to retrieve the current job's information.

// Send an email to the Apex job's submitter notifying of job completion.
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {'**@**.com'};
mail.setToAddresses(toAddresses);
mail.setSubject('Apex Sharing Recalculation ');
mail.setPlainTextBody('The batch Apex job processed ' );
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}


}
  主要是在finish方法中设置的一段 时间 执行代码。

CronTrigger delete 是执行一个schedul 剩下在Scheduled Jobs 上的记录,所以我们要删除已经处理ok的Schedule。

代码执行:
**_SOA_Settings__c SOASettings = **_SOA_Settings__c.getOrgDefaults();
**_RetrieveInquiryBatchtest retrieveBatch = new **_RetrieveInquiryBatchtest();
Id jobId = Database.executeBatch(retrieveBatch, 5);
  

转载于:https://www.cnblogs.com/bandariFang/p/7921705.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值