permaculture playing cards*
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes @EJB annotation from servlet and JBoss Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "@EJB annotation from servlet and JBoss" Watch "@EJB annotation from servlet and JBoss" New topic
Author

@EJB annotation from servlet and JBoss

Sergio Tridente
Ranch Hand

Joined: Mar 22, 2007
Posts: 329

Hi,

I have just begun reading Manning's book: EJB 3 In Action. I am currently on chapter 2 and have downloaded the examples.

I tried the code samples for the first chapter and they are working fine. However, after reading the second chapter, I wanted to try replacing the client code for a servlet.

This is what I have:







And the Web Application's DD (web.xml):


Now whenever I run it, I get a NullPointerException: an that is because "helloUser" refers to null. It looks like DI isn't working by simply using the @EJB annotation.

The client application that came with the samples works fine:


Using this jndi.properties file:


As you can see the client application is using JNDI.

FYI, the whole project is packet in file "example1.ear", which has "META-INF" directory, "chapter1-ejb.jar" (which contains the EJB) and "chapter1-web.war" (which contains the web application).

Just in case this info might be usuful, the "application.xml" inside the ear's META-INF is:


What am I missing? Is it a problem with JBoss' configuration?
Any help will be highly appreciated. Thanks in advance.


SCJP 1.4 (88%) - SCJP 5.0 Upgrade (93%) - SCWCD 1.4 (97%) - SCBCD 5.0 (98%)
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9937
    
159

Sergio,

I guess, you are using JBoss-4.x which does not support EJB injection in servlets or application clients. The later version JBoss-5.x (which is still in Beta) supports injecting the EJB in servlets and application clients.


[My Blog] [JavaRanch Journal]
Sergio Tridente
Ranch Hand

Joined: Mar 22, 2007
Posts: 329

Thank you Jaikiran. I am using version 4.2.2 GA, that explains everything.
Sergio Tridente
Ranch Hand

Joined: Mar 22, 2007
Posts: 329

I have installed JBoss beta 4 and tried again: now it works fine.

BTW, is it me or JBoss 5.x runs slower than 4.2.2 GA?
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9937
    
159

Originally posted by Sergio Tridente:

BTW, is it me or JBoss 5.x runs slower than 4.2.2 GA?


You are right JBoss 5.x is slower compared to JBoss-4.2.2 GA. However, JBoss-5 is still in Beta. The performances changes usually happen after a candidate release, before a GA.

And by the way, since you seem to be using JBoss for certification purpose, i would recommend that you stick with JBoss-4.2.2 GA, instead of JBoss-5.0 Beta which has some known issues even in deployment. You wouldn't want to spend time trying to debug those
Sergio Tridente
Ranch Hand

Joined: Mar 22, 2007
Posts: 329

Originally posted by Jaikiran Pai:

And by the way, since you seem to be using JBoss for certification purpose, i would recommend that you stick with JBoss-4.2.2 GA, instead of JBoss-5.0 Beta which has some known issues even in deployment. You wouldn't want to spend time trying to debug those


I have had the same thought before trying it. The thing is that I went to JBoss' forums and I found some post explaining that JBoss 4.2.2 GA is NOT a JEE 5.0 AS but rather J2EE 1.4 with an EJB 3.0 plugin. And the fact that some annotations are not supported, like for example @EJB, got me thinking. Now I am really confused. What do you think? Should I stick with 4.2.2 GA anyway? What about those options not yet implemented? Won't it be an issue for the exam preparation? Should I go to Glassfish instead?
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9937
    
159

Originally posted by Sergio Tridente:
I have had the same thought before trying it. The thing is that I went to JBoss' forums and I found some post explaining that JBoss 4.2.2 GA is NOT a JEE 5.0 AS but rather J2EE 1.4 with an EJB 3.0 plugin. And the fact that some annotations are not supported, like for example @EJB, got me thinking.

That's right, JBoss-4.2.2 GA is not a JEE 5.0 server. The @EJB annotation is supported only in the EJB layer (and not in web or application client).

Originally posted by Sergio Tridente:
Now I am really confused. What do you think? Should I stick with 4.2.2 GA anyway? What about those options not yet implemented? Won't it be an issue for the exam preparation? Should I go to Glassfish instead?


Sergio,

Between JBoss-4.2.2 GA and JBoss-5 Beta, i would definitely recommend JBoss-4.2.2 GA. I personally have no idea of what's covered as part of the exam, so i can't say how that's going to be affected. You will have a smooth sailing with JBoss-4.2.2 GA but *might* not be able to try out a few things like injection in servlets (if that's covered as part of the exam). If you have an option of using Glassfish (and it doesn't have these issues?) then that definitely is the best option.
Manuel Jimenez
Greenhorn

Joined: Jun 13, 2008
Posts: 3
Originally posted by Jaikiran Pai:
Sergio,

I guess, you are using JBoss-4.x which does not support EJB injection in servlets or application clients. The later version JBoss-5.x (which is still in Beta) supports injecting the EJB in servlets and application clients.


Hi i have a similar problem. Im using JBoss-4.2.2, and i have been tried to make an @EJB injection in a Message Driven Bean, but i get a runtime error in deploy time.

I get this error: "java.lang.RuntimeException: Failed to populate ENC: env/MyMDBean/MySessionBean global jndi name was null"

the code is someting like that:
-------------------------------------------------------------------
@Stateless(name = "MySessionBean")
public class MySessionBean implements IMySessionBeanRemote {

public void doSometing(String a) {
try {
System.out.println("in doSometing!! :"+a);
}cath(Exception e){
e.printStackTrace();
}

}
-------------------------------------------------------------------
@MessageDriven(name="MyMDBean", activationConfig = {
@ActivationConfigProperty(propertyName="destinationType", propertyValue="javax.jms.Queue"),
@ActivationConfigProperty(propertyName="destination", propertyValue="queue/TestQueue")

})
public class MyMDBean implements MessageListener {

@Resource
private MessageDrivenContext context;

@EJB(beanName = "MySessionBean")
private MySessionBean myBean;

public void onMessage(Message message) {
try {
...
} catch (Exception t) {
...
}
}
}

Any ideas?, Jboss 4.2.2 supports @EJB injection?

Thanks for your help.
[ July 04, 2008: Message edited by: Manuel Jimenez ]

SCJP 5, SCBCD 5
Sergio Tridente
Ranch Hand

Joined: Mar 22, 2007
Posts: 329

I have just tried your code on JBoss 4.2.2GA and I am getting the same results. However, after changing the code it works. Here's my modification:



This might be a problem with JBoss. I am going to install Glassfish and try it again. I will post the results back.
[ July 05, 2008: Message edited by: Sergio Tridente ]
RakeshS Chauhan
Greenhorn

Joined: Apr 05, 2008
Posts: 4
In JNDI lookup if you use
context.lookup("example1/HelloUserBean/remote");
then it will work for JBOSS4.XX also.

If ejb is in ear use this earName/ejbImpBean/local((or /remote)).
If ejb is in jar then use this ejbImpBean/local((or /remote)).


I used this with 4.2.2 GA and it worked.
Sergio Tridente
Ranch Hand

Joined: Mar 22, 2007
Posts: 329

Same problem with Glassfish. This is the message I get: Warning : Unable to determine local business vs. remote business designation for EJB 3.0 ref Unresolved Ejb-Ref com.business.MyMDBean/myBean@jndi: @null@com.business.MySessionBean@Session@MySessionBean
Manuel Jimenez
Greenhorn

Joined: Jun 13, 2008
Posts: 3
Tanks for your help!
Sergio Tridente
Ranch Hand

Joined: Mar 22, 2007
Posts: 329

I also tried the following modification on both JBoss and Glassffish:



It worked fine on Glassfish but it did not work on JBoss.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: @EJB annotation from servlet and JBoss
 
Similar Threads
I do not know how to call a JSP inside an ear file
Newbie problem from chapter1 of Head First Servlets & JSP
getting error when deploying web app in weblogic 10.0
Help with EJB DI
Incompatible Java EE specification level?