【问题标题】:Naming Convention for Open-EJB embedded container?Open-EJB 嵌入式容器的命名约定?
【发布时间】:2016-10-08 03:42:57
【问题描述】:

我正在使用 open-ejb 嵌入式容器为 javaee 应用程序编写测试。该应用程序仅包含一个 @Stateless bean 和测试

import javax.ejb.embeddable.EJBContainer;

import org.junit.Before;
import org.junit.Test;

public class ProductDaoTest {

    private EJBContainer container;

    @Before
    public void initializeContainerAndDatabase(){
        container = EJBContainer.createEJBContainer();
        System.out.println(container);
    }

    @Test
    public void test(){

    }
}

由于某些原因,嵌入式容器启动的成功或失败取决于源代码所在的文件夹名称。我尝试了不同的名称,在某些情况下有效,在某些情况下却没有,而且模式对我来说似乎是随机的。

hibernate-javaee --> 没用
hibernate-ejb --> 没用
javaee-hibernate --> 没用
abc-xyz --> 工作
hibernate-abc-xyz-def --> 没用
abc-xyz-def-ejb --> 没用
abc-xyz-def-javaee --> 工作
abc-xyz-def-hibernate --> 工作

如果失败,异常跟踪是:

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running org.ares.hibernate.javaee.dao.ProductDaoTest
INFO - ********************************************************************************
INFO - OpenEJB http://tomee.apache.org/
INFO - Startup: Wed Jun 08 08:57:56 IST 2016
INFO - Copyright 1999-2015 (C) Apache OpenEJB/TomEE Project, All Rights Reserved.
INFO - Version: 4.7.4
INFO - Build date: 20160304
INFO - Build time: 09:05
INFO - ********************************************************************************
INFO - openejb.home = C:\fakepath\hibernate-abc-xyz-def
INFO - openejb.base = C:\fakepath\hibernate-abc-xyz-def
INFO - Created new singletonService org.apache.openejb.cdi.ThreadSingletonServiceImpl@26f67b76
INFO - Succeeded in installing singleton service
INFO - Using 'javax.ejb.embeddable.EJBContainer=true'
INFO - Cannot find the configuration file [conf/openejb.xml].  Will attempt to create one for the beans deployed.
INFO - Configuring Service(id=Default Security Service, type=SecurityService, provider-id=Default Security Service)
INFO - Configuring Service(id=Default Transaction Manager, type=TransactionManager, provider-id=Default Transaction Manager)
INFO - Creating TransactionManager(id=Default Transaction Manager)
INFO - Creating SecurityService(id=Default Security Service)
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 2.039 sec <<< FAILURE!
test(org.ares.hibernate.javaee.dao.ProductDaoTest)  Time elapsed: 1.81 sec  <<< ERROR!
org.apache.openejb.OpenEjbContainer$NoModulesFoundException: No modules found to deploy.
1)Maybe descriptors are placed in incorrect location.
Descriptors could go under:
<base-dir>/META-INF or <base-dir>/WEB-INF
but not directly under <base-dir>
Check 'Application Discovery via the Classpath' docs page for more info
2)Maybe no modules are present in the classpath.
Is 'openejb.base' system property pointing to the intended location?
        at org.apache.openejb.util.Exceptions.newNoModulesFoundException(Exceptions.java:99)
        at org.apache.openejb.OpenEjbContainer$Provider.load(OpenEjbContainer.java:540)
        at org.apache.openejb.OpenEjbContainer$Provider.createEJBContainer(OpenEjbContainer.java:322)
        at javax.ejb.embeddable.EJBContainer.createEJBContainer(EJBContainer.java:56)
        at javax.ejb.embeddable.EJBContainer.createEJBContainer(EJBContainer.java:43)
        at org.ares.hibernate.javaee.dao.ProductDaoTest.initializeContainerAndDatabase(ProductDaoTest.java:14)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:497)
        at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
        at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
        at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
        at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
        at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
        at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)
        at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
        at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:497)
        at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
        at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
        at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
        at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
        at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)


Results :

Tests in error:
  test(org.ares.hibernate.javaee.dao.ProductDaoTest): No modules found to deploy.(..)

如果成功,输出为:

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running org.ares.hibernate.javaee.dao.ProductDaoTest
INFO - ********************************************************************************
INFO - OpenEJB http://tomee.apache.org/
INFO - Startup: Wed Jun 08 09:07:02 IST 2016
INFO - Copyright 1999-2015 (C) Apache OpenEJB/TomEE Project, All Rights Reserved.
INFO - Version: 4.7.4
INFO - Build date: 20160304
INFO - Build time: 09:05
INFO - ********************************************************************************
INFO - openejb.home = C:\fakepath\abc-xyz-def-hibernate
INFO - openejb.base = C:\fakepath\abc-xyz-def-hibernate
INFO - Created new singletonService org.apache.openejb.cdi.ThreadSingletonServiceImpl@26f67b76
INFO - Succeeded in installing singleton service
INFO - Using 'javax.ejb.embeddable.EJBContainer=true'
INFO - Cannot find the configuration file [conf/openejb.xml].  Will attempt to create one for the beans deployed.
INFO - Configuring Service(id=Default Security Service, type=SecurityService, provider-id=Default Security Service)
INFO - Configuring Service(id=Default Transaction Manager, type=TransactionManager, provider-id=Default Transaction Manager)
INFO - Creating TransactionManager(id=Default Transaction Manager)
INFO - Creating SecurityService(id=Default Security Service)
INFO - Found EjbModule in classpath: c:\fakepath\abc-xyz-def-hibernate\target\classes
INFO - Beginning load: c:\fakepath\abc-xyz-def-hibernate\target\classes
INFO - Configuring enterprise application: C:\fakepath\abc-xyz-def-hibernate
INFO - Auto-deploying ejb ProductDao: EjbDeployment(deployment-id=ProductDao)
INFO - Configuring Service(id=Default Stateless Container, type=Container, provider-id=Default Stateless Container)
INFO - Auto-creating a container for bean ProductDao: Container(type=STATELESS, id=Default Stateless Container)
INFO - Creating Container(id=Default Stateless Container)
INFO - Configuring Service(id=Default Managed Container, type=Container, provider-id=Default Managed Container)
INFO - Auto-creating a container for bean org.ares.hibernate.javaee.dao.ProductDaoTest: Container(type=MANAGED, id=Default Managed Container)
INFO - Creating Container(id=Default Managed Container)
INFO - Using directory C:\Users\UserName\AppData\Local\Temp for stateful session passivation
INFO - Enterprise application "C:\fakepath\abc-xyz-def-hibernate" loaded.
INFO - Assembling app: C:\fakepath\abc-xyz-def-hibernate
INFO - Jndi(name="java:global/abc-xyz-def-hibernate/ProductDao!org.ares.hibernate.javaee.dao.ProductDao")
INFO - Jndi(name="java:global/abc-xyz-def-hibernate/ProductDao")
INFO - Existing thread singleton service in SystemInstance(): org.apache.openejb.cdi.ThreadSingletonServiceImpl@26f67b76
INFO - OpenWebBeans Container is starting...
INFO - Adding OpenWebBeansPlugin : [CdiPlugin]
INFO - Adding OpenWebBeansPlugin : [OpenWebBeansJsfPlugin]
INFO - All injection points were validated successfully.
INFO - OpenWebBeans Container has started, it took 195 ms.
INFO - Created Ejb(deployment-id=ProductDao, ejb-name=ProductDao, container=Default Stateless Container)
INFO - Started Ejb(deployment-id=ProductDao, ejb-name=ProductDao, container=Default Stateless Container)
INFO - Deployed Application(path=C:\fakepath\abc-xyz-def-hibernate)
org.apache.openejb.OpenEjbContainer@34c01041
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 3.224 sec

为了确保会话 bean 在我添加的类路径中被发现

Enumeration<URL> ejbJars = this.getClass().getClassLoader().getResources("org/ares/hibernate/javaee/dao/ProductDao.class");
        while (ejbJars.hasMoreElements()) {
            URL url = ejbJars.nextElement();
            System.out.println("app = " + url);
        }

this link 中所述。该类在工作和非工作情况下都被发现。

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running org.ares.hibernate.javaee.dao.ProductDaoTest
app = file:/C:/fakepath/hibernate-ejb/target/classes/org/ares/hibernate/javaee/dao/ProductDao.class

是否有我不知道的命名约定或者我偶然发现了一个错误?

应用程序的源代码是here。项目的父目录是abc-def,并且在运行mvn clean test 时项目运行。如果父目录名称更改为hibernate-javaee 并且应用程序停止工作。

【问题讨论】:

    标签: jakarta-ee ejb apache-tomee openejb


    【解决方案1】:

    关于命名约定问题,没有。我很确定您在某个地方(系统属性文件或以编程方式)排除/包括路径。排除/包含允许使用通配符,这就是为什么某些文件夹模式有效而有些无效的原因。要排除任何内容并包含所有内容,请创建具有以下属性的 EJB 容器。

    Properties p = new Properties();
    p.put("openejb.deployments.classpath.exclude", "");
    p.put("openejb.deployments.classpath.include", ".*");
    container = EJBContainer.createEJBContainer(p);
    

    请参阅http://tomee.apache.org/application-discovery-via-the-classpath.html 了解更多说明。

    确保您的EJB 将被部署的其他选项。

    选项 1:应用程序发现

    如果您不想添加额外的配置文件,您必须确保您的 EJB 文件夹是类路径的一部分,并且您的 EJB 包含ejb-jar.xml。有关详细说明,请参阅 http://tomee.apache.org/application-discovery-via-the-classpath.html

    选项2:配置文件

    根据日志你没有配置配置文件。

    INFO - Cannot find the configuration file [conf/openejb.xml].  Will attempt to create one for the beans deployed.
    

    在大多数情况下,这很好,自定义配置文件是例外。尝试使用您的文件夹设置来设置配置文件。有关更多说明,请参阅http://openejb.apache.org/configuration.html

    【讨论】:

    • 对于答案的第二部分,根据open-ejb documentationOpenEJB will function fine with no configuration at all and will happily create things as needed and select defaults for everything. So in a real sense configuration is all about overriding those defaults.,我不会覆盖工作和非工作场景的任何默认值。我会检查您在答案的第一部分中提供的链接。
    • 检查了应用程序发现的链接,没有与通配符相关的内容。它只对目录名称进行了一些更改,让我发疯了。
    • @ares,请发布您的系统属性文件。您可以在 conf/system.properties 中找到该文件。并检查 ${user.home}/.openejb/system.properties 中是否存在其他用户 system.properties 文件。
    • 我正在使用一个具有依赖项&lt;dependency&gt; &lt;groupId&gt;org.apache.openejb&lt;/groupId&gt; &lt;artifactId&gt;tomee-embedded&lt;/artifactId&gt; &lt;version&gt;1.7.4&lt;/version&gt; &lt;/dependency&gt; 的嵌入式容器。因此我没有conf/system.properties。我也没有${user.home}/.openejb/system.properties
    • 好的,您的测试类在类路径中,它包含一个 ejb-jar.xml?
    【解决方案2】:

    一些patterns 会自动从 OpenEJB 扫描中排除。

    【讨论】:

    • 确实javaee-hibernate- 都在排除列表中。
    【解决方案3】:

    请注意,openejb.additional.include=myjarprefix 可以让您将其重新添加到扫描包含中。它将删除冲突的排除,因此如果您使用它,请使其尽可能长。可以在conf/system.properties中设置。

    【讨论】:

      猜你喜欢
      • 2021-01-19
      • 2011-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多