zoukankan      html  css  js  c++  java
  • 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);
      

    此刻,静下心来学习
  • 相关阅读:
    mysql5.7初始化密码报错 ERROR 1820 (HY000): You must reset your password using ALTER USER statement
    Python 国内镜像源
    PyCharm
    MySQL 表字段唯一性约束设置方法unique
    ch01系统基础信息模块详解
    Django出错提示TemplateDoesNotExist at /
    windows更改pip源(可用)
    异常处理
    前端基础学习(4) BOM DOM 节点操作 事件
    前端练习题 1 BOM DOM
  • 原文地址:https://www.cnblogs.com/bandariFang/p/7921705.html
Copyright © 2011-2022 走看看