2016-07-15 76 views
1

如何檢測作業ID是否在作業完成後調用作業偵聽器? 我有批處理開始的工作,我有聽衆。Java批處理作業偵聽器在作業獲得作業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"); 
    }; 
} 

回答

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(); 
    }; 
}