Java批处理作业监听器afterJob获取作业的id

时间:2016-07-15 13:47:43

标签: java batch-processing jobs

如何检测正在调用afterJob侦听器的已完成作业的作业ID? 我的工作是从批处理开始,我有听众。

我的工作叫做PayrollJob,监听器是PayrollJobListener

<job id="payroll" xmlns="http://xmlns.jcp.org/xml/ns/javaee" version="1.0">
    <listeners>
        <listener ref="payrollJobListener"/>
    </listeners>
    <step id="process">
        <listeners>
            <listener ref="payrollItemWriteListener"/>
            <listener ref="payrollSkipProcessorListener"/>
            <listener ref="payrollSkipReadListener"/>
            <listener ref="payrollSkipWriteListener"/>
            <listener ref="payrollChunkListener"/>

        </listeners>
        <chunk item-count="5">
            <reader ref="PayrollItemReader"></reader> 
            <processor ref="PayrollItemProcessor"></processor>
            <writer ref="PayrollItemWriter"></writer> 



        </chunk>
    </step>
</job>

和带有监听器的类

@Named()
public class PayrollJobListener extends AbstractJobListener {

    @Override()
    public void beforeJob(){};

    @Override()
    public void afterJob(){
        System.out.println("i need a id of a job calling this method");
    };
}

1 个答案:

答案 0 :(得分:0)

您可以注入JobContext。

@Named()
public class PayrollJobListener extends AbstractJobListener {

    @Inject
    private JobContext jobContext;

    @Override()
    public void beforeJob(){};

    @Override()
    public void afterJob(){
        long id = this.jobContext.getExecutionId();
        String name = this.jobContext.getJobName();
    };
}