JSR 352 - 为什么<skippable-exception-classes>中包含的异常会停止作业

时间:2017-11-02 08:55:25

标签: java exception-handling jsr352

我正在尝试使用JSR 352和Websphere Liberty Profile 17.0.0.2创建一个简单的批处理应用程序。在进入异常处理之前,一切似乎都正常工作。我假设在&#39; skippable-exception-classes&#39;中指定的应用程序引发了异常。 job.xml中的节点将被略微忽略/跳过,并允许作业继续执行下一个项目/步骤。

我遇到的问题是在调用其中一个方法时从注入的bean(通过@Inject)抛出异常时发生的。作业只是在抛出异常的当前项/步骤上停止。

奇怪的是每当我手动/显式地抛出完全相同的异常时,让我们说RecordNotFoundException,这个工作继续进行,这是我的预期。只有从注入的bean触发异常时才会出现此问题。我已将@ApplicationException添加到异常类中,因此异常包装/解包已到位。

我错过了什么吗?这是Websphere Liberty的错误吗?

编辑:添加了简化代码

SampleSharedLibrary - 来自其他项目/ jar的依赖

package sample;

import javax.ejb.ApplicationException;
import javax.ejb.Stateless;

@Stateless
public class SampleSharedLibrary {

    public String someMethod(String object) {
        String result = null;

        if(object != null) {
            result = "OK";
        } else {
            throw new MyException();
        }

        return result;
    }

    @ApplicationException(rollback = true)
    public static class MyException extends RuntimeException {
        public MyException() {}
    }
}

BatchExample应用程序

SampleWriter     import sample.SampleSharedLibrary;

import javax.batch.api.chunk.AbstractItemWriter;
import javax.enterprise.context.Dependent;
import javax.inject.Inject;
import javax.inject.Named;
import java.util.List;

@Dependent
@Named("SampleWriter")
public class SampleWriter extends AbstractItemWriter{

    @Inject
    SampleSharedLibrary sharedLibrary;

    public void writeItems(List<Object> list) throws Exception {
        for(Object item : list) {
            sharedLibrary.someMethod(item.toString());
        }
    }
}

SampleReader

import javax.batch.api.chunk.AbstractItemReader;
import javax.enterprise.context.Dependent;
import javax.inject.Named;
import java.io.Serializable;

@Dependent
@Named("SampleReader")
public class SampleReader extends AbstractItemReader {

    String[] sample;
    int index;
    @Override
    public void open(Serializable checkpoint) throws Exception {
        sample = new String[]{"S1", null, "S3"};
        index = 0;
    }

    public Object readItem() throws Exception {
        Object item = null;

        if(index < sample.length) {
            item = sample[index];
            index++;
        }

        return item;
    }
}

job.xml

<job id="myJob" xmlns="http://xmlns.jcp.org/xml/ns/javaee" version="1.0">
    <step id="myStep" >
        <chunk item-count="1" skip-limit="50">
            <reader ref="SampleReader"/>
            <writer ref="SampleWriter"/>
            <skippable-exception-classes>
                <include class="java.lang.Exception"/>
                <include class="sample.SampleSharedLibrary$MyException"/>
            </skippable-exception-classes>
        </chunk>
    </step>
</job>

至于server.xml,没有添加太多配置,但我启用了以下功能:cdi-1.2,ejbLite-3.2,jaxrs-2.0,jpa-2.0,jsf-2.2,managedBeans-1.0,servlet-3.1 ,localConnector-1.0,batch-1.0和json-1.0

当遇到来自实际应用程序的错误时,我会得到类似下面的日志(必须将某些值更改为星号):

[02-11-17 21:38:12:323 CST] 000000d1 com.ibm.ws.logging.internal.impl.IncidentImpl                I FFDC1015I: An FFDC Incident has been created: "*******.**********.*******.batchflows.service.writer.ErrorWhileUpdating: *******.**********.*******.infrastructure.*****.commands.OurCommand$RecordNotFound: recordId=31AA69FAE017D21F com.ibm.jbatch.container.controller.impl.ChunkStepControllerImpl 485" at ffdc_17.11.02_21.38.12.0.log
[02-11-17 21:38:12:352 CST] 000000d1 com.ibm.ws.logging.internal.impl.IncidentImpl                I FFDC1015I: An FFDC Incident has been created: "com.ibm.jbatch.container.exception.BatchContainerRuntimeException: *******.**********.*******.batchflows.service.writer.ErrorWhileUpdating: *******.**********.*******.infrastructure.*****.commands.OurCommand$RecordNotFound: recordId=31AA69FAE017D21F com.ibm.jbatch.container.controller.impl.ChunkStepControllerImpl 1041" at ffdc_17.11.02_21.38.12.1.log
[02-11-17 21:38:12:363 CST] 000000d1 com.ibm.ws.logging.internal.impl.IncidentImpl                I FFDC1015I: An FFDC Incident has been created: "javax.transaction.RollbackException com.ibm.jbatch.container.transaction.impl.JTAUserTransactionAdapter 107" at ffdc_17.11.02_21.38.12.2.log
[02-11-17 21:38:12:385 CST] 000000d1 com.ibm.ws.logging.internal.impl.IncidentImpl                I FFDC1015I: An FFDC Incident has been created: "com.ibm.jbatch.container.exception.TransactionManagementException: javax.transaction.RollbackException com.ibm.jbatch.container.controller.impl.ChunkStepControllerImpl 680" at ffdc_17.11.02_21.38.12.3.log

1 个答案:

答案 0 :(得分:1)

即使批处理容器跳过给定的异常,EJB容器仍然可以回滚批处理块事务

配置作业XML以跳过给定的异常只会阻止批处理容器将块事务标记为回滚。它不会阻止其他人将事务标记为回滚。

这意味着EJB容器本身可以回滚事务。所以在你的情况下你需要使用:

@ApplicationException(rollback = false)
public static class MyException extends RuntimeException {

或者,在预注释样式中,您需要使用已检查的异常而不是未经检查的异常,以防止EJB容器回滚事务。