I've just created a simple Enterprise Application Project in Eclipse with web module and EJB3 module.
This is my EJB:
@Stateless
public class MyBean implements MyBeanRemote {
public String getGreeting(){return "Hello World!";}
}
This is my Remote Business Interface:
@Remote
public interface MyBeanRemote {
String getGreeting();
}
This is my Servlet:
@WebServlet("/MyServlet")
public class MyServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
MyBeanRemote myBean = null;
try {
myBean=(MyBeanRemote) new InitialContext().lookup("XXX"); //1
} catch (NamingException e) {e.printStackTrace();}
response.getWriter().append(myBean.getGreeting());
}
}
Bean injection works like a charm, but I'm not being able to successfully perform a JDNI lookup: what should be inserted at line //1
instead of XXX
? I've tried any possible combination (well, clearly missing the right one...)
As far as I understand, Geronimo uses OpenEJB to map name to resources, and at this page you can read that
The default JNDI name is in the following format:
{deploymentId}{interfaceType.annotationName}
so I thought it was just MyBeanRemote
, but that's not working... What am I missing? I've spent my last hours with unsuccessful attempts and reading similar answers here on SO.
This is my openejb.jar.xml:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<ejb:openejb-jar xmlns:app="http://geronimo.apache.org/xml/ns/j2ee/application-2.0" xmlns:bp="http://www.osgi.org/xmlns/blueprint/v1.0.0" xmlns:client="http://geronimo.apache.org/xml/ns/j2ee/application-client-2.0" xmlns:conn="http://geronimo.apache.org/xml/ns/j2ee/connector-1.2" xmlns:dep="http://geronimo.apache.org/xml/ns/deployment-1.2" xmlns:ejb="http://openejb.apache.org/xml/ns/openejb-jar-2.2" xmlns:jaspi="http://geronimo.apache.org/xml/ns/geronimo-jaspi" xmlns:log="http://geronimo.apache.org/xml/ns/loginconfig-2.0" xmlns:name="http://geronimo.apache.org/xml/ns/naming-1.2" xmlns:pers="http://java.sun.com/xml/ns/persistence" xmlns:pkgen="http://openejb.apache.org/xml/ns/pkgen-2.1" xmlns:sec="http://geronimo.apache.org/xml/ns/security-2.0" xmlns:web="http://geronimo.apache.org/xml/ns/j2ee/web-2.0.1">
<dep:environment>
<dep:moduleId>
<dep:groupId>default</dep:groupId>
<dep:artifactId>MyProjectEJB</dep:artifactId>
<dep:version>1.0</dep:version>
<dep:type>car</dep:type>
</dep:moduleId>
</dep:environment>
</ejb:openejb-jar>
as created by the wizard.
As user achabahe suggested, and with the help of these question/answers, I printed my whole JNDI tree:
String getJNDITree() {
Context context=null;
try {
context = new InitialContext();
} catch (NamingException e1) {e1.printStackTrace();}
List<String>roots=Arrays.asList("java:comp","java:module","java:app","java:global");
StringBuilder tree=new StringBuilder();
for(String root:roots)
recursiveDeepVisit(context,root,"",tree);
return tree.toString()+"\n";
}
void recursiveDeepVisit(Context context,String path,String prefix,StringBuilder tree){
tree.append("\n"+prefix+path);
try {
NamingEnumeration<NameClassPair> list = context.list(path);
while (list.hasMore()) {
String innerPath = path+"/"+list.next().getName();
recursiveDeepVisit(context, innerPath, " "+prefix,tree);
}
} catch (Exception e) {}
}
getting this output
java:
java:app
java:app/env
java:app/MyProjectEJB
java:app/MyProjectEJB/MyBean!com.dev.bean.MyBeanRemote
java:app/MyProjectEJB/MyBean
java:app/AppName
java:comp
java:comp/UserTransaction
java:comp/HandleDelegate
java:comp/ValidatorFactory
java:comp/TransactionManager
java:comp/Bundle
java:comp/TransactionSynchronizationRegistry
java:comp/env
java:comp/ORB
java:comp/Validator
java:comp/BundleContext
java:TransactionManager
java:TransactionSynchronizationRegistry
java:module
java:module/ModuleName
java:module/env
java:global
java:global/MyProject
java:global/MyProject/MyProjectEJB
java:global/MyProject/MyProjectEJB/MyBean!com.dev.bean.MyBeanRemote
java:global/MyProject/MyProjectEJB/MyBean
java:global/geronimo-mejb-3.0.1
java:global/geronimo-mejb-3.0.1/MEJB
java:global/geronimo-mejb-3.0.1/ejb
java:global/geronimo-mejb-3.0.1/ejb/mgmt
java:global/geronimo-mejb-3.0.1/ejb/mgmt/MEJB
java:global/geronimo-mejb-3.0.1/ejb/mgmt/MEJB!javax.management.j2ee.ManagementHome
java:global/geronimo-mejb-3.0.1/MEJB!javax.management.j2ee.ManagementHome
java:global/env
It turns out that
MyBeanRemote myBean=(MyBeanRemote) new InitialContext().lookup("java:app/MyProjectEJB/MyBean!com.dev.bean.MyBeanRemote");
is the solution I was looking for.