Search code examples
javajakarta-eejsr352

Getting jobId from inside of Batchlet


I need get the jobId from inside of executing batchlet. Already, I'm able to get the jobId from the class that I am lanching the Batchlet, but not from inside of Batchlet.

long jobId = job.start("BatchletName", parm);

This sentence launch the Batchlet "BatchletName". Now, I need to get the jobId value from inside de class BatchletName.java:

@Dependent
@Named("BatchletName")
@TransactionManagement(TransactionManagementType.BEAN)
public class BatchletName extends AbstractBatchlet {
    public String process() throws Exception {
        // I need jobId here
        return "";
    }    
}

Thank you in advance.


Solution

  • You can inject javax.batch.runtime.context.JobContext into your batchlet class, then call JobContext.getJobName() to get the job id.

    import javax.batch.runtime.context.JobContext;
    import javax.inject.Inject;
    import javax.inject.Named;
    
    @Dependent
    @Named("BatchletName")
    public class BatchletName extends AbstractBatchlet {
        @Inject
        JobContext jobContext;
    
        public String process() throws Exception {
            // I need jobId here
    
            String jobId = jobContext.getJobName();
            return "";
        }  
    }