我们有一个工作正常的JBoss v8“ Wildfly”部署,如下所示:

App-ear.ear
|_App-model.jar
|_App-ejb.jar
|_App-web.war


此部署在persistence.xml内带有App-model.jar。现在,我想用另一个WAR模块部署第二个EAR,我可以独立于原始应用程序重新部署它。为此,我创建了第二个EAR,如下所示:

Second-ear.ear
|_Second-web.war


第二个应用程序在Eclipse中进行开发(设置为Maven项目)时具有对App-model.jar的类路径依赖关系,并且构建良好。为了在运行时使该依赖关系可用,我对Second-ear.ear/META-INF/jboss-deployment-structure.xml进行了如下编辑:

<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure xmlns="urn:jboss:deployment-structure:1.2">
    <deployment>

        <dependencies>
            <module name="deployment.App-ear.ear.App-model.jar" />
        </dependencies>

    </deployment>
</jboss-deployment-structure>


但是,JBoss AS无法加载持久性,并显示以下错误:

2015-08-19 09:13:36,903 ERROR [] [ @ ] [] [MSC service thread 1-5] [fail] MSC000001: Failed to start service jboss.deployment.unit."Second-ear.ear".WeldStartService: org.jboss.msc.service.StartException in service jboss.deployment.unit."Second-ear.ear".WeldStartService: Failed to start service
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1904) [jboss-msc-1.2.2.Final.jar:1.2.2.Final]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [rt.jar:1.8.0_25]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [rt.jar:1.8.0_25]
    at java.lang.Thread.run(Thread.java:745) [rt.jar:1.8.0_25]
Caused by: java.lang.IllegalArgumentException: JBAS016069: Error injecting persistence unit into CDI managed bean. Can't find a persistence unit named MyPersistenceUnitName in deployment Second-ear.ear
    at org.jboss.as.weld.services.bootstrap.WeldJpaInjectionServices.getScopedPUName(WeldJpaInjectionServices.java:110)
    at org.jboss.as.weld.services.bootstrap.WeldJpaInjectionServices.registerPersistenceUnitInjectionPoint(WeldJpaInjectionServices.java:90)
    at org.jboss.weld.injection.ResourceInjectionFactory$PersistenceUnitResourceInjectionProcessor.getResourceReferenceFactory(ResourceInjectionFactory.java:279)
    at org.jboss.weld.injection.ResourceInjectionFactory$PersistenceUnitResourceInjectionProcessor.getResourceReferenceFactory(ResourceInjectionFactory.java:267)
    at org.jboss.weld.injection.ResourceInjectionFactory$ResourceInjectionProcessor.createFieldResourceInjection(ResourceInjectionFactory.java:207)
    at org.jboss.weld.injection.ResourceInjectionFactory$ResourceInjectionProcessor.createResourceInjections(ResourceInjectionFactory.java:182)
    at org.jboss.weld.injection.ResourceInjectionFactory.discoverType(ResourceInjectionFactory.java:405)
    at org.jboss.weld.injection.ResourceInjectionFactory.getResourceInjections(ResourceInjectionFactory.java:92)
    at org.jboss.weld.injection.producer.ResourceInjector.<init>(ResourceInjector.java:59)
    at org.jboss.weld.injection.producer.ResourceInjector.of(ResourceInjector.java:49)
    at org.jboss.weld.injection.producer.BeanInjectionTarget.<init>(BeanInjectionTarget.java:62)
    at org.jboss.weld.injection.producer.BeanInjectionTarget.createDefault(BeanInjectionTarget.java:46)
    at org.jboss.weld.manager.InjectionTargetFactoryImpl.chooseInjectionTarget(InjectionTargetFactoryImpl.java:128)
    at org.jboss.weld.manager.InjectionTargetFactoryImpl.createInjectionTarget(InjectionTargetFactoryImpl.java:87)
    at org.jboss.weld.bean.ManagedBean.<init>(ManagedBean.java:91)
    at org.jboss.weld.bean.ManagedBean.of(ManagedBean.java:71)
    at org.jboss.weld.bootstrap.AbstractBeanDeployer.createManagedBean(AbstractBeanDeployer.java:264)
    at org.jboss.weld.bootstrap.BeanDeployer.createClassBean(BeanDeployer.java:228)
    at org.jboss.weld.bootstrap.ConcurrentBeanDeployer$2.doWork(ConcurrentBeanDeployer.java:78)
    at org.jboss.weld.bootstrap.ConcurrentBeanDeployer$2.doWork(ConcurrentBeanDeployer.java:75)
    at org.jboss.weld.executor.IterativeWorkerTaskFactory$1.call(IterativeWorkerTaskFactory.java:60)
    at org.jboss.weld.executor.IterativeWorkerTaskFactory$1.call(IterativeWorkerTaskFactory.java:53)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266) [rt.jar:1.8.0_25]
    ... 3 more


尝试使用该持久性单元的CDI Bean注入由原始EntityManager产生的App-model.jar

@Named("test")
public class Test {

    @Inject
    @MyModelDB // Qualifier
    private EntityManager em = null;

    // A simple test of the datamodel...
    public Long numberOfProjects() {
        if (this.em != null) {
            return ((BigDecimal) this.em.createNativeQuery("select count(*) from e_bo_projects").getSingleResult()).longValue();
        }
        return -1L;
    }
}


我怎么解决这个问题?

最佳答案

我现在已经成功部署了两个EAR。感谢您对我的原始问题的评论。如果persistence.xml不包含JNDI绑定,则我最初的方法是使用2 EAR(包括相同的数据模型JAR)。谢谢@弗兰克!提到的jboss-deployment-structure.xml不再引用我的另一个EAR。

所以现在我的部署看起来像这样:

App-ear.ear
|_App-model.jar
|_App-ejb.jar
|_App-web.war

Second-ear.ear
|_App-model.jar
|_Second-web.war


这对两个应用程序都适用,并且在两种情况下我都已成功访问数据库。

关于java - 如何在JBoss“Wildfly”中的2 EAR之间共享JPA数据模型,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32089161/

10-11 10:57