如何在发生任何异常时解析作业状态

时间:2016-02-24 14:45:32

标签: java spring spring-batch

您好我在解决Spring Batch中的作业状态时遇到问题[我是Spring-batch的新手]。在我的ItemReader中,即使发生异常,状态也会显示已完成

这是我的工作配置。

@Bean
public Job customerJob(){
    return jobBuilderFactory.get("custJob")  
            .listener(jobListner)           
            .start(custStep())              
            .build();

private Step custStep() {
    return stepBuilderFactory.get("custStep1")
            .<Customer,Customer>chunk(10)
            .reader(customerItemReader)
            .processor(customerItemProcessor)
            .writer(customerItemWriter)             
            .allowStartIfComplete(true)
            .build();

ItemReader

@Override
public Customer read() throws Exception, UnexpectedInputException, ParseException,
        NonTransientResourceException {

    if(customerList != null && customerList.size() >=1){
        Customer customer;
         customer =  customerList.remove(0);
         System.out.println("@@ ID="+customer.getCustNumber());     


         return customer;
    }
    return null;
}


//Method to fetch list[from ItemStream interface]
@Override
public void open(ExecutionContext executionContext) throws ItemStreamException {
    System.out.println("reading customer list"); 
    try {
        customerList = customerService.getCustomers();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();

    }

来自行getCustomers()的{​​{1}}方法有时会抛出customerList = customerService.getCustomers()异常(如果服务器中存在某些问题)。在这种情况下,我除了我的工作状态应该失败,但它显示“已完成”。

java.net.SocketException
@Override
public void afterJob(JobExecution jobExecution) {
    System.out.println("exit status ="+jobExecution.getExitStatus().getExitCode());
    System.out.println(" status ="+jobExecution.getStatus().name());

}

在这种情况下如何显示状态“FAIL”。

0 个答案:

没有答案