TLDR:在将web配置文件示例迁移到一个成熟的EE应用程序之后,CDI并没有像我预期的那样行事。
首先,我要说,我对CDI相对来说是个新手。我认为我理解这一概念,但执行过程中的细微差别仍有待克服。
我正在尝试EJB并随后使用RESTful API批处理(JSR-352)包装。
首先,我将javaee7示例用于工资,并将逻辑和作业规范引入到一个成熟的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名称来引用的(批处理环境清楚地找到了这个名称)。此规范也是工作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的土地上漏掉了什么。
这都是在玻璃鱼4上运行的。
我的web模块的web和EJB模块的META中都有空的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 }下面是工作web配置文件示例的相同日志记录:
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发布于 2016-03-28 20:03:59
最近的一次搜索使我来到这里,因此,让我给出一个部分的答案,尽管这个问题的年龄。
首先,有关背景信息,请快速阅读CDI的"bean发现模式“,例如,请参见这里。
现在,在1.0XSD级别上出现的空beans.xml将被CDI视为ALL模式,而完全没有beans.xml将被视为默认的注释模式。
批处理工件对CDI并不特别。因此,与任何类一样,它们将仅被视为CDI托管bean,并且只能通过bean名称( @Named值)访问,此时:
@Dependent确实是一个定义bean的注释)。我的猜测是,在您的重新打包中,beans.xml不再被CDI检测到,因此您实际上从ALL迁移到了注释的模式,需要@Dependent bean定义的注释通过bean名称加载批处理工件。
(为什么会这样呢?OP没有深入到那么多细节,并且已经很好地向前推进了--我是sure...so --这并不重要,我认为其余的答案仍然有价值)。
希望这还能帮到别人!
发布于 2014-12-06 14:49:38
我和你的情况非常相似,直到我找到了一些对我有用的东西,我希望它也能对你起作用。
注:我也不是CDI方面的专家。
1)您可以在“命名”中添加“依赖”(javax.enterprise.context.Dependent)。例如:
@Dependent
@Named("SimpleItemProcessor")
public class SimpleItemProcessor implements ItemProcessor {
// ...
}只需为所有SimpleItemReader、SimpleItemProcessor和SimpleItemWriter做这件事。这包括你的应用程序中的任何Batchlet。
2)尽量不要让JobContext成为私有的,而是将其改为:
@Inject
protected JobContext jobContext;或
@Inject
JobContext jobContext;到目前为止这些对我有用..。
https://stackoverflow.com/questions/23872306
复制相似问题