Batch 模块用户的一个普遍要求是能够在 Batch 作业的任何阶段获取作业实例 ID。为什么这有用?好吧,可能有许多有用的场景:
- 如果批处理与外部系统通信,有时传递本地作业实例 ID 以供参考和管理目的很有用
- 改进作业的自定义日志记录
- 某些作业可能希望为有意义的事件触发电子邮件或短信通知,在这些消息中包含作业实例 ID 显然很有帮助。
例子
该 id 将被暴露并抛出键为“batchJobInstanceId”的流变量。该流变量从输入阶段一开始就可用。它也将在每个步骤和完成阶段可用。让我们看一个示例作业:
如您所见,这项工作非常简单。在其所有阶段中,它都会调用一个记录 #[flowVars['batchJobInstanceId']] 输出的子流。它产生的日志输出如下:
INFO 2015-05-08 18:18:09,599 [[batch].HTTP_Listener_Configuration.worker.01] com.mulesoft.module.batch.engine.DefaultBatchEngine: Starting input phase
INFO 2015-05-08 18:18:09,603 [[batch].HTTP_Listener_Configuration.worker.01] org.mule.api.processor.LoggerMessageProcessor: Job Instance Id is: ba01e1a0-f5c7-11e4-9414-10ddb1daeb6d
INFO 2015-05-08 18:18:09,603 [[batch].HTTP_Listener_Configuration.worker.01] com.mulesoft.module.batch.engine.DefaultBatchEngine: Input phase completed
INFO 2015-05-08 18:18:09,608 [[batch].HTTP_Listener_Configuration.worker.01] com.mulesoft.module.batch.engine.queue.BatchQueueLoader: Starting loading phase for instance 'ba01e1a0-f5c7-11e4-9414-10ddb1daeb6d' of job 'batchBatch'
INFO 2015-05-08 18:18:09,610 [[batch].HTTP_Listener_Configuration.worker.01] com.mulesoft.module.batch.engine.queue.BatchQueueLoader: Finished loading phase for instance ba01e1a0-f5c7-11e4-9414-10ddb1daeb6d of job batchBatch. 1 records were loaded
INFO 2015-05-08 18:18:09,615 [[batch].HTTP_Listener_Configuration.worker.01] com.mulesoft.module.batch.engine.DefaultBatchEngine: Started execution of instance 'ba01e1a0-f5c7-11e4-9414-10ddb1daeb6d' of job 'batchBatch'
INFO 2015-05-08 18:18:09,624 [batch-job-batchBatch-work-manager.01] org.mule.api.processor.LoggerMessageProcessor: Job Instance Id is: ba01e1a0-f5c7-11e4-9414-10ddb1daeb6d
INFO 2015-05-08 18:18:09,643 [batch-job-batchBatch-work-manager.01] com.mulesoft.module.batch.engine.DefaultBatchEngine: Starting execution of onComplete phase for instance ba01e1a0-f5c7-11e4-9414-10ddb1daeb6d of job batchBatch
INFO 2015-05-08 18:18:09,644 [batch-job-batchBatch-work-manager.01] org.mule.api.processor.LoggerMessageProcessor: Job Instance Id is: ba01e1a0-f5c7-11e4-9414-10ddb1daeb6d
INFO 2015-05-08 18:18:09,644 [batch-job-batchBatch-work-manager.01] com.mulesoft.module.batch.engine.DefaultBatchEngine: Finished execution of onComplete phase for instance ba01e1a0-f5c7-11e4-9414-10ddb1daeb6d of job batchBatch
INFO 2015-05-08 18:18:09,644 [batch-job-batchBatch-work-manager.01] com.mulesoft.module.batch.engine.DefaultBatchEngine: Finished execution for instance 'ba01e1a0-f5c7-11e4-9414-10ddb1daeb6d' of job 'batchBatch'. Total Records processed: 1. Successful records: 1. Failed Records: 0
就是这样了。这是一个小的、易于使用的改进,我希望你会发现它有用。还有其他关于作业实例 ID 如何有用的示例吗?请分享!