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

    列动态导出的Excel,大致整了一下,没有过多深入. 1.首先定义列样式 package a.b.common.util.excel.stream; import java.util.Objects; ...

  2. angular 路由守卫Observable异步请求串联

    假设路由守卫有这种场景 需要使用observable同时发送多个Http 请求,判断request2返回的数据中是否存在request1返回的数据 使用async await export class ...

  3. vue 中使用 XLSX 和 xlsx-style 实现前端下载Excel表格

    import XLSX from "xlsx-style"  //ps  需要修改源码:在\node_modules\xlsx-style\dist\cpexcel.js 807行 ...

  4. JDK-11.0.17 + Neo4j-4.4.12

    JDK安装 下载地址:https://www.oracle.com/java/technologies/javase-downloads.html 注册Oracle账户,并下载,选择路径安装,将bin ...

  5. Winform窗体中打开PDF文件的三种方式

    来源:https://www.jb51.net/article/251451.htm

  6. BUU刷题记录

    [GWCTF 2019]mypassword xss+csp 打开页面可以注册登录 登进去提示不是sql注入 然后提示源码 看一下 然后有段后端代码写道了注释里 <!-- if(is_array ...

  7. 4.git的指令应用

    1.stash 应用:   应用场景:在当前分支开发代码,开发到一半,代码没有提交,你想在当前分支的基础上切换到别的分支:或者创建新的分支,所以需要把当前开发的内容进行藏匿起来. 1.1藏匿指令:   ...

  8. Java基础__02.数据类型

    Java中的数据类型 Java是一种强类型的语言,所有的变量都必须要先定义才能使用. Java中的数据类型分为 基本数据类型和引用数据类型. 1.基本数据类型:(8种) 数值类型 整数类型 byte: ...

  9. Twenty-eight

    组件之间的父子关系 使用组件的三个步骤 步骤1:使用import语法导入需要的组件 步骤2:使用conponents节点注册组件 步骤3:以标签形式使用刚才注册的组件   通过components注册 ...

  10. centos7.2下配置dhcp v4或v6服务

    1.centos7.2下配置dhcp v4或v6服务 安装dhcp-server centos7及以前版本的操作系统使用命令: yum install dhcp centos8使用命令: yum in ...