需求:系统上的标准功能是能够设置间隔一天的执行,或者是写完代码着急测试我们写个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);
  

044_Schedule Job 间隔时间自动执行的更多相关文章

  1. setTimeout() 实现程序每隔一段时间自动执行

    定义和用法 setTimeout() 方法用于在指定的毫秒数后调用函数或计算表达式. 语法 setTimeout(code,millisec) 参数 描述 code 必需.要调用的函数后要执行的 Ja ...

  2. Timer定时方法(间隔时间后执行)

    Timer time = new Timer(); time.schedule(new TimerTask() { @Override public void run() { // TODO Auto ...

  3. 每隔一秒自动执行函数(JavaScript)

    http://www.cnblogs.com/xlx0210/archive/2010/03/19/1689497.html 1. setInterval() ——每隔一秒自动执行方法,setInte ...

  4. Python3.x:简单时间调度Timer(间隔时间执行)

    Python3.x:简单时间调度Timer(间隔时间执行) threading模块中的Timer能够帮助实现定时任务,而且是非阻塞的: 代码: import threading import time ...

  5. 如何让PHP程序自动执行(后台)

    如何让php程序自动执行,这个就需要用到一个函数了: int ignore_user_abort ( [bool setting] )  定义和用法 ignore_user_abort() 函数设置与 ...

  6. 在CMD下启动vmware、Xshell连接虚拟机以及控制Chrome浏览器自动执行js登录校园网

    标题有点长,主要是写个bat出来玩玩, (1)不用每次都手动关闭mysql服务(我不想把它设为手动启动,有强迫症) (2)然后希望每次vmware能自动连上虚拟机 (3)以及每次Xshell都能自动启 ...

  7. 启动tomcat服务器自动执行一个方法

    第一步:配置web.xml文件 添加如下代码 <servlet> <servlet-name>Timer</servlet-name> <servlet-cl ...

  8. Asp.Net(C#)自动执行计划任务的程序实例分析

    在业务复杂的应用程序中,有时候会要求一个或者多个任务在一定的时间或者一定的时间间隔内计划进行,比如定时备份或同步数据库,定时发送电子邮件等,我们称之为计划任务.实现计划任务的方法也有很多,可以采用SQ ...

  9. 小记:Quartz 当 Job 执行时间超过触发间隔时间时所发生的情况

    一个普通的 Job 实现如下: public class Job1 : IJob { public void Execute(IJobExecutionContext context) { Conso ...

  10. 使用php作linux自动执行脚本

    使用php作linux自动执行脚本 [来源] 达内    [编辑] 达内   [时间]2013-03-21 在作社区时, 时常需要统计上线人数等数据. 一般做法是, 把这段代码放在用户 login或者 ...

随机推荐

  1. springcloud 和springboot版本对比

    版本对应关系大版本对应: Spring Cloud Spring Boot Angel版本 兼容Spring Boot 1.2.x Brixton版本 兼容Spring Boot 1.3.x,也兼容S ...

  2. pytorch学习笔记(9)--神经网络模型的保存与读取

    一.网络模型的保存和加载 1.网络模型保存方法1 import torch import torchvision vgg16 = torchvision.models.vgg16(weights=Fa ...

  3. java.lang.UnsatisfiedLinkError:【Linux运行JAVA调用JNA重新,so,SO报错】

    困扰了好半天,我自己新建的项目,包名什么的不一样,太TM坑了,必须要包名一样文件名一样

  4. react 二级路由嵌套

    嵌套路由之后,静态文静路径错误, 更改webpack  打包output 输出根目录,publicPath:'/',二级路由刷新之后白屏,在首页模板文件中路径前加   /,

  5. 【DM论文阅读杂记】复杂社区网络

    Paper Title Community Structure in Time-Dependent, Multiscale, and Multiplex Networks Basic algorith ...

  6. [737] Interlude OpCodez

    [737] Interlude Client 00 SendProtocolVersion 01 MoveBackwardToLocation 02 Say 03 RequestEnterWorld ...

  7. 服务器consul与本地服务健康检查不通问题解决

    (125条消息) 服务器consul与本地服务健康检查不通问题解决_向往鸟的博客-CSDN博客_consul健康检查失败 .MathJax, .MathJax_Message, .MathJax_Pr ...

  8. 如何给img标签指定默认显示的图片?(已解决)

    1. 使用场景 页面上有很多图片,或者图片很大,这都会使加载的时候出现大片空白,影响用户体验. 2. 解决办法 在CSS里给img指定默认显示的图片,以下是代码: { //**** backgroun ...

  9. 虚拟 DOM 的优缺点

    优点: 保证性能下限: 框架的虚拟 DOM 需要适配任何上层 API 可能产生的操作,它的一些 DOM 操作的实现必须是普适的,所以它的性能并不是最优的:但是比起粗暴的 DOM 操作性能要好很多,因此 ...

  10. docker 安装mongodb

    一.安装mongodb 我们首先改一下镜像源,避免拉去速度太慢 创建daemon.json文件:位于/etc/docker目录下: 登录阿里云 即可 2.docker 搜索mongo镜像 然后pull ...