Batch 调用 定时执行

本文介绍了如何设置定时任务来执行批量操作,特别是在批处理过程结束后,如何触发另一个batch任务的执行,实现流程自动化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

定时执行 batch 

global class LogistiInfoSchedu implements Schedulable, Database.Batchable<sObject>, Database.Stateful, Database.AllowsCallouts {
    public Set<Id> updateOiIdSet;

    global void execute(SchedulableContext sc) {
        LogistiInfoSchedu lo = new LogistiInfoSchedu();
        database.executeBatch(lo,90);
    }

    //Start 方法里面查询的是系统中发货状态不为到达的发货申请的记录
    global Database.QueryLocator start(Database.BatchableContext bc) {
        updateOiIdSet = new Set<Id>();
        List<Order_Inquiry__c> qiList = new List<Order_Inquiry__c>();
        qiList = [select id,OrderStatus__c,OTNumber__c,SAP_order_number__c from Order_Inquiry__c where OrderStatus__c != 'delivered' and OrderStatus__c != 'epod' and OTNumber__c != NULL AND OTNumber__c != '' AND (ApprovalStatus__c = 'Approved' OR ApprovalStatus__c = 'Auto Approved')];
        System.debug('LogistiInfoSchedu 查到的数量:'+qiList.size());

        return Database.getQueryLocator([select id,OrderStatus__c,OTNumber__c,SAP_order_number__c from Order_Inquiry__c where OrderStatus__c != 'delivered' and OrderStatus__c != 'epod' and OTNumber__c != NULL AND OTNumber__c != '' AND (ApprovalStatus__c = 'Approved' OR ApprovalStatus__c = 'Auto Approved')]);
    }

    global void execute(Database.BatchableContext BC,list<Order_Inquiry__c> scope) {
        //调用接口解析xml 数据
        LogisticsInfoInterface lo = new LogisticsInfoInterface();
        Set<String> otmNymberSet = new Set<String>();

        for(Order_Inquiry__c ord : scope){
            otmNymberSet.add(ord.OTNumber__c);
        }

        system.debug('Batch批次中要处理:'+otmNymberSet);
        system.debug('Batch批次中要处理的数量:'+otmNymberSet.size());

        // httpResponse xmlRes = lo.getLogisticsInfo(otmNymberSet);
        // String responsStr = xmlRes.getBody().Replace('</time>','</theTime>');
        // responsStr = responsStr.Replace('<time>','<theTime>');

        SaveDeliveryInfo s = new SaveDeliveryInfo();
        s.getResponseXML(otmNymberSet);

        //updateOiIdSet.addAll(s.locationUpdateIdSet);
    }

    global void finish(Database.BatchableContext BC) {
        if(updateOiIdSet.size() > 0){
            UpdateLocationBatch thisBatch = new UpdateLocationBatch(updateOiIdSet);
            thisBatch.startUpdateLocation();
        }
    }
}

Finish 中调用其他batch

global class UpdateLocationBatch implements Database.Batchable<sObject>, Database.Stateful, Database.AllowsCallouts {
    public Set<Id> locationUpdateIdSet;

    global UpdateLocationBatch() {
        locationUpdateIdSet = new Set<Id>();
    }

    global UpdateLocationBatch(Set<Id> orderIdSet) {
        locationUpdateIdSet = new Set<Id>();
        if(orderIdSet != NULL){
            locationUpdateIdSet = orderIdSet;
        }
    }

    /**
     * @Function [该Batch的启动方法]
     */
    public void startUpdateLocation(){
        UpdateLocationBatch thisBatch = new UpdateLocationBatch(locationUpdateIdSet);
        Database.executeBatch(thisBatch, 1);
    }

    global Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator([SELECT Id, Latitude__c, Longitude__c, Location__c
                                        FROM Order_Inquiry__c
                                        WHERE Id IN: locationUpdateIdSet
                                            AND Latitude__c != NULL
                                            AND Longitude__c != NULL]);
    }

    global void execute(Database.BatchableContext BC, list<Order_Inquiry__c> scope) {
        Order_Inquiry__c thisOrder = scope.get(0);

        GetCurrentLocationService loc = new GetCurrentLocationService();
        thisOrder.Location__c = loc.getCurrentLocation(thisOrder.Latitude__c, thisOrder.Longitude__c);
        System.debug('thisOrder.Location__c='+thisOrder.Location__c);
        update thisOrder;
    }

    global void finish(Database.BatchableContext BC) {

    }
}

### 如何配置Pentaho Kettle定时任务执行 #### 使用 Spoon 创建带参数的定时任务并将其部署到服务器后台运行 为了使Kettle的任务能够在服务器上作为后台进程定期自动运行,可以采用如下方法: 对于希望在特定时间触发作业的情况,在本地利用Spoon完成转换和作业的设计之后,需进一步考虑如何让这些流程按照预定的时间表启动。一种常见的方式是借助操作系统的调度功能——例如Linux下的`cron`服务或Windows的任务计划程序。 当准备就绪后,可编写批处理(.bat)文件来调用Kitchen.bat(用于执行Job)或者Pan.bat(用于执行Transformation),并通过命令行传递必要的参数,如作业路径、日志级别等信息[^4]。 ```batch @echo off if "%1" == "h" goto begin mshta vbscript:createobject("wscript.shell").run("%~nx0 h",0)(window.close)&&exit :begin :: 设置工作目录至Kitchen.bat所在的磁盘分区及具体位置 cd D:\software\Kettle7 :: 调用kitchen执行指定job并将输出追加记录到log文件中 kitchen /file:D:\KettleProject\job2.kjb /level:Basic >>D:\KettleProject\test.log ``` 此脚本展示了怎样静默模式下启动一个名为`job2.kjb`的工作流实例,并将执行过程中的基本信息保存于`test.log`文档内。 #### 利用操作系统自带工具实现自动化调度 - **Linux**: 用户可以通过`crontab -e`指令进入个人用户的定时任务管理界面,添加新条目以安排上述`.bat`脚本或其他形式的Shell Script按周期性规律被执行。需要注意的是,如果命令字符串里包含了百分号(`%`)字符,则应该替换为两个连续的百分号(`%%`)以便正确解析[^3]。 `* * * * * sh /path/to/script.sh` - **Windows**: 对应地,在Windows平台上则应当运用“任务计划程序(Task Scheduler)”图形化界面或是PowerShell cmdlet (`Register-ScheduledTask`, `New-ScheduledTaskTrigger`, etc.) 来达到相同目的。 另外,考虑到跨平台兼容性和集中管控的需求,部分企业级方案可能会引入专门的日程管理系统(Cron Service), 或者依赖CI/CD流水线工具(Jenkins, GitLab CI)集成此类特性。 #### 参数化与环境变量支持 为了让同一个物理上的作业能够适应不同场景的应用需求,建议合理规划输入参数列表及其默认值设定;同时充分利用系统级别的环境变量机制,使得像数据库连接串这样的敏感数据不必硬编码进源码之中而是动态获取自外部配置项。这不仅有助于提高灵活性也增强了安全性[^5]。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值