CDI注入和Java批处理(JSR-352)

时间:2014-05-26 14:23:53

标签: java-ee cdi java-ee-7

TLDR:将Web配置文件示例迁移到完整的EE应用程序后,CDI的行为与我的预期不同

首先我要说的是我对CDI比较陌生。我喜欢认为我理解这个概念,但实施的细微差别尚未被征服。

我正在尝试EJB-ify并随后使用RESTful API Java Batch(JSR-352)进行包装。

首先,我已将javaee7样本用于Payroll并将逻辑和作业规范拉入一个完整的EE7项目(ejb,web,ear模块)。

当我调用RESTful服务时,我最初遇到这个问题(缩短以保存理智):

WARNING:   Caught exception executing step: java.lang.RuntimeException: com.ibm.jbatch.container.exception.BatchContainerRuntimeException: Tried but failed to load artifact with id: SimpleItemProcessor
Caused by: com.ibm.jbatch.container.exception.BatchContainerRuntimeException: Tried but failed to load artifact with id: SimpleItemProcessor
Caused by: java.lang.ClassNotFoundException: SimpleItemProcessor

请记住,应用程序的这一部分应该看到大致没有变化。 SimpleItemProcessor定义相同,如下所示。

@Named("SimpleItemProcessor")
public class SimpleItemProcessor
implements ItemProcessor {

@Inject
private JobContext jobContext;


public Object processItem(Object obj) throws Exception {
    Properties jobParameters = BatchRuntime.getJobOperator().getParameters(jobContext.getExecutionId());

    PayrollInputRecord inputRecord = (PayrollInputRecord) obj;
    PayrollRecord payrollRecord = new PayrollRecord();
    payrollRecord.setMonthYear((String) jobParameters.get("monthYear"));

    int base = inputRecord.getBaseSalary();
    float tax = base * 27 / 100.0f;
    float bonus = base * 15 / 100.0f;

    payrollRecord.setEmpID(inputRecord.getId());
    payrollRecord.setBase(base);
    payrollRecord.setTax(tax);
    payrollRecord.setBonus(bonus);
    payrollRecord.setNet(base + bonus - tax);

    return payrollRecord;
}

}

这个SimpleItemProcessor由PayrollJob.xml定义中的CDI Name引用(Batch环境正在清楚地找到它)。该规范也是工作JavaEE7示例的副本。

<job id="payroll" xmlns="http://xmlns.jcp.org/xml/ns/javaee" version="1.0">
<step id="process">
    <chunk item-count="3">
        <reader ref="SimpleItemReader"></reader> 
        <processor ref="SimpleItemProcessor"></processor>
        <writer ref="SimpleItemWriter"></writer> 
    </chunk>
</step>

如果我使用SimpleItemProcessor(com.mycompany.SimpleItemProcessor)的完全限定名称,它似乎工作。其他两个批处理组件(Reader和Writer)似乎不需要这个。

我的猜测是我在CDI土地上遗漏了一些东西。

这一切都在Glassfish 4上运行。

我的web模块的WEB-INF和EJB模块的META-INF都有空的beans.xml。

请告诉我您可能对此有任何见解。我已经搅拌了很长时间。

编辑: 增加了批处理组件的日志记录,抛出了异常并得到了一些指向某些东西的有趣内容,可能与CDI相关。

FINER:   ENTRY Loading batch artifact id = SimpleItemReader
FINER:   Delegating to preferred artifact factorycom.ibm.jbatch.container.services.impl.CDIBatchArtifactFactoryImpl@4ac13bc2
FINER:   ENTRY Loading batch artifact id = SimpleItemReader
FINER:   RETURN For batch artifact id = SimpleItemReader, loaded artifact instance: com.mycompany.SimpleItemReader@4862cec7 of type: com.mycompany.SimpleItemReader
FINER:   ENTRY Loading batch artifact id = SimpleItemProcessor
FINER:   Delegating to preferred artifact factorycom.ibm.jbatch.container.services.impl.CDIBatchArtifactFactoryImpl@4ac13bc2
FINER:   ENTRY Loading batch artifact id = SimpleItemProcessor
FINE:   Tried but failed to load artifact with id: SimpleItemProcessor, Exception = java.util.NoSuchElementException
FINER:   RETURN For batch artifact id = SimpleItemProcessor, FAILED to load artifact instance
FINER:   Preferred artifact factory failed to load artifact SimpleItemProcessor. Defaulting to batch.xml.
FINE:   TCCL = EarClassLoader : 

失败的实现的CDI部分(bm.getBeans(id)将返回为空):

51  private Object More ...getArtifactById(String id) {
52
53      Object artifactInstance = null;
54
55      try {
56          InitialContext initialContext = new InitialContext();
57          BeanManager bm = (BeanManager) initialContext.lookup("java:comp/BeanManager");
58          Bean bean = bm.getBeans(id).iterator().next();
59          Class clazz = bean.getBeanClass();
60          artifactInstance = bm.getReference(bean, clazz, bm.createCreationalContext(bean));
61      } catch (Exception e) {
62          // Don't throw an exception but simply return null;
63          logger.fine("Tried but failed to load artifact with id: " + id + ", Exception = " + e);
64      }
65
66      return artifactInstance;
67  }

以下是工作网络配置文件示例的相同日志记录:

FINER:   ENTRY
FINER:   No preferred job xml loader is detected in configuration
FINER:   Preferred job xml loader failed to load PayrollJob. Defaulting to META-INF/batch-jobs/
FINER:   Loaded job xml with PayrollJob from META-INF/batch-jobs/
FINER:   ENTRY Loading batch artifact id = SimpleItemReader
FINER:   Delegating to preferred artifact factorycom.ibm.jbatch.container.services.impl.CDIBatchArtifactFactoryImpl@4ac13bc2
FINER:   ENTRY Loading batch artifact id = SimpleItemReader
FINER:   RETURN For batch artifact id = SimpleItemReader, loaded artifact instance: com.oracle.javaee7.samples.batch.api.SimpleItemReader@753908c of type: com.oracle.javaee7.samples.batch.api.SimpleItemReader
FINER:   ENTRY Loading batch artifact id = SimpleItemProcessor
FINER:   Delegating to preferred artifact factorycom.ibm.jbatch.container.services.impl.CDIBatchArtifactFactoryImpl@4ac13bc2
FINER:   ENTRY Loading batch artifact id = SimpleItemProcessor
FINER:   RETURN For batch artifact id = SimpleItemProcessor, loaded artifact instance: com.oracle.javaee7.samples.batch.api.SimpleItemProcessor@5376b860 of type: com.oracle.javaee7.samples.batch.api.SimpleItemProcessor
FINER:   ENTRY Loading batch artifact id = SimpleItemWriter
FINER:   Delegating to preferred artifact factorycom.ibm.jbatch.container.services.impl.CDIBatchArtifactFactoryImpl@4ac13bc2
FINER:   ENTRY Loading batch artifact id = SimpleItemWriter
FINER:   RETURN For batch artifact id = SimpleItemWriter, loaded artifact instance: com.oracle.javaee7.samples.batch.api.SimpleItemWriter@243e016c of type: com.oracle.javaee7.samples.batch.api.SimpleItemWriter

2 个答案:

答案 0 :(得分:4)

最近的搜索引导我到这里,所以让我尽管问题的年龄给出了部分答案。

首先,对于背景,请快速阅读CDI的“bean发现模式”,例如:见here

现在,1.0XSD级别的空beans.xml的存在将被CDI视为 ALL 模式,而缺少beans.xml将被视为默认注释模式。

批量工件对CDI并不特别特殊。因此,与任何类一样,它们只会被视为CDI托管bean,并且只有在以下任何一个时才能通过bean名称(@Named值)访问:

  • bean discovery mode = ALL OR
  • bean discovery mode = ANNOTATED 另外,工件包含一个bean定义注释(而@Dependent确实是一个bean定义注释)。

我的猜测是,在您的重新打包中,CDI不再检测到beans.xml,因此您有效地从 ALL 转移到 ANNOTATED 模式,需要{ {1}} bean定义注释,用于按bean名称加载批处理工件。

(为什么会这样?OP没有进入那么多细节并且已经很好地转移到现在我确定......所以无所谓,其余的答案我认为仍然很有价值)

希望这对某人有帮助!

答案 1 :(得分:3)

我和你的情况非常相似,直到我发现了一些对我有用的东西,我希望它对你也有用。

注意:我也不是CDI的专家。

1)您可以使用@Named添加@Dependent(javax.enterprise.context.Dependent)。 E.g:

@Dependent
@Named("SimpleItemProcessor")
public class SimpleItemProcessor implements ItemProcessor {
    // ...
}

对所有SimpleItemReader,SimpleItemProcessor和SimpleItemWriter执行此操作。这包括您应用中的所有Batchlet。

2)尽量不要将JobContext设为私有,而是将其设为:

@Inject
protected JobContext jobContext;

@Inject
JobContext jobContext;

到目前为止,这些对我有用......