I'm new to EJB world, though currently EJB 3.1 is the latest, I wanted to get my hands dirty with EJB 2.1 and slowly move upwards to 3.1, but I stumbled at my first program and could not greet the world :-)
Below are my steps
1 ) I created an EJB Project called HelloWorld using eclipse associated that with an EAR project. Now I see two projects in workspace HelloWorld and HelloWorldEAR.
2 ) Created EJBHome, EJBObject and SessionBean 3 ) I have ejb-jar.xml and JBOSS.xml added to HelloWorld/ejbModule/META-INF as follows
After all the above steps, I right clicked the HelloWorldEAR and exported the project as ear and it created me an ear file which I deployed to C:\jboss-4.0.0\server\default\deploy\HelloWorldEAR.ear
and started the server. In JMX-Console I also confirm the deployment as I see
As the last step, I wrote a stand alone client as below to run
but getting exception as
Exception in thread "main" javax.naming.NameNotFoundException: comp not bound
What is the mistake I'm doing and how to fix this ?
EJB 3 and EJB 2.1 both version of EJB do things differently so you can directly move to EJB 3.
The main thing is in EJB 3 you can use EJB component as simple POJO as well as EJB but in EJB 2.1 you cannot do such thing.
Also you can use annotations in EJB 3
Try using relative paths instead of absolute paths like "ejb/HelloWorld" while calling lookup method I hope this will help you
Refer this for EJB3
Joined: Jun 15, 2013
Thank you Ninad,
Using Relative path like "HelloWorld" alone worked for me. But I still have this basic question, as per the Sun specifications, should we not actually give the absolute path, how does JNDI know, if HelloWorld is under ejb or env or some other context.
Also, I think, there is something fundamentally wrong I'm doing, but just not able to figure it out.
As an experiment, I tried to define like ENV in JNDI in my ejb-jar.xml like
My intention is to get MAX using JNDI, so I tried to lookup as
I get the exception
javax.naming.NameNotFoundException: comp not bound
and then I tried something like ctx.bind("MAX",new Integer(100));
and then called ctx.lookUp("MAX") which did give me an Integer back.
So is binding not happening properly in the former case, when included in ejb-jar.xml or am I doing anything stupid ?
Joined: Jun 15, 2013
May be too much of information provided in my original post is causing confusion, so I will try to keep my questions short and brief.
Assuming the original setup as posted in my original post, i.e I have only ejb-jar.xml (no vendor specific descriptor files like jboss.xml or for some other vendor) and I have <ejb-name>HelloWorld</ejb-name>
Question 1) - If I had to call the enterprise bean from a standalone client not deployed along with the enterprise beans in a container, do I look up the stub for home using lookup("HelloWorld") or lookup("java:comp/env/ejb/HelloWorld"). Because lookup("HelloWorld") seems to be working for me, when I try the other option, I get exceptions like comp not bound. Could this be because of "In the absence of a jboss.xml specification of the jndi-name for an EJB, the home interface is bound under the ejb-jar.xml ejb-name value" ?
Question 2) I did see examples in many posts flooded in internet, where they used the lookup against java:comp/env/ejb/HelloWorld and it seems to be working fine for them. Is that because, they used a different vendor not like JBoss 4.0 or, they would have looked up like this, from servlet or another component deployed on same jvm as that of enterprise beans as one single ear package.
Question 3) When I looked in JMX console, I noticed HellowWorld under Global JNDI namespace. Is that because, I did not vendor specific descriptor file like jboss.xml. If I had used Jboss.xml, do I need to specify like java:comp/env/ejb/HelloWorld specifically in ejb-jar.xml or jboss.xml. I have tried with different combinations and ended up with different exceptions.
Question 4) When do I use the lookup as
InitialContext iniCtx = new InitialContext();
Context ejbCtx = (Context) iniCtx.lookup("java:comp/env/ejb");
ShoppingCartHome home = (ShoppingCartHome) ejbCtx.lookup("ShoppingCartHome");