Search code examples
javajakarta-eeapache-tomee

Specify application name for Apache TomEE


I was needing to use JPA in standalone application, so I've found the example http://tomee.apache.org/latest/examples/jpa-hibernate.html as starter.

They create EJB Context via

final Context context = EJBContainer.createEJBContainer(p).getContext();

Then there's a log line:

INFO - Enterprise application "/Users/dblevins/examples/jpa-hibernate" loaded

You need to know that application name to wizard out the search string for lookup:

context.lookup("java:global/jpa-hibernate/Movies");

What makes me worry that I've found out no information on where those 'jpa-hibernate' part comes from. It either comes from artifact id, or, even worse, from the current directory name, which makes the code using it terribly dependend on context, that the developer doesn't control.

I've totally find to google out how to specify that application name so that I could use lookup that will work no matter who invokes my code and where it is copied.

How can I configure this application name?


Solution

  • The Embedded EJB container used in this unit test example allows to run EJBs outside a Java EE container. A good introduction/tutorial can be found here: https://docs.oracle.com/javaee/7/tutorial/ejb-embedded002.htm

    It supports the same configuration files as regular EJB-jars, namely it supports the ejb-jar.xml configuration file (the module deployment descriptor). It is possible to configure the module name there, e.g.

    <ejb-jar xmlns="http://java.sun.com/xml/ns/javaee"  
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
             xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_1.xsd"  
             version="3.1">  
        <module-name>myapp</module-name>  
    </ejb-jar>  
    

    This file needs to go into the jar's META-INF directory.