3

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

每当调用其方法之一时从注入的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() {}
    }
}

批处理示例应用程序

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

样本阅读器

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

工作.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、批处理 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
4

1 回答 1

1

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

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

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

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

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

于 2017-11-02T15:47:21.107 回答