Batch Module: Obtaining the Job Instance ID in Mule 3.7
Learn how to grab the job instance ID of any of a Batch job's phases in Mule Batch Module.
Join the DZone community and get the full member experience.
Join For FreeA popular request among users of the Batch module is the ability to grab the job instance id in any of a Batch job’s phases. Why is that useful? Well, there could be a number of useful scenarios:
- If the batch communicates with an external system, it’s sometimes useful to pass the local job instance id for reference and management purposes
- To improve the job’s custom logging
- Some jobs might want to fire email or SMS notifications for meaningful events, having the job instance id in those messages is obviously helpful.
Example
The id will be exposed throw a flow variable of key ‘batchJobInstanceId’. That flow variable will be available since the very beginning of the input phase. It will also be available on every step and in the on-complete phase. Let’s see an example job:
As you see this job is pretty simple. In all its phases, it invokes a sub-flow which logs the output of #[flowVars[‘batchJobInstanceId’]]. The log output that it produces is the following:
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
So that’s it. This is a small, easy to use improvement which I hope you’ll find useful. Have any other examples of how the job instance id can be useful? Please share!
Published at DZone with permission of Mariano Gonzalez, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments