This is supposed to be some kind of a FAQ. Please add specific problems you think others may encounter too during EJB3 development. Don't forget to add your solution/pointer/workaround as well!
Q: I'am getting a ClassCastException when I try to cast a Session Bean to it's interface after I successfully obtained the Session Bean over InitialContext lookup. Whats going on?
A: You are probably dealing with scoped Beans, remote interfaces and trying to run everything in the same VM while package multiple copies of your interface classes into the different parts of your application(s).
Solve this by either:
eliminate multiple copies of interfaces, package everything into the same ear
run your application in different VMs
if required to let different EARs interact with one another in the same VM:
enable CallByValue for JNDI in server/xxx/conf/jboss-service.xml or server/xxx/deploy/naming-service.xml
upgrade to EJB 3.0 RC4-PFD -- CallByValue is broken in RC3
A: Maybe you forgot that as of RC4 - the default jndi naming convention is changed (its in the migration notes) - if
a quick getClass().getName() on your ic.lookup() says org.jnp.interfaces.NamingContext
and not $Proxy0 - you need to think: APP/BeanName/remote or APP/BeanName/local
have a look in the jndi tree with http://localhost:8080 at your global jndi namespace.
Q. I'm also getting a ClassCastException while casting a Session Bean to its interface after a JNDI lookup. I'm using JBOSS 4.0.4GA. The same code used to work fine in JBOSS 4.0.3SP1. I have tried lot of different things - like using Local interfaces, using @RemoteBinding annotation to give the EJB a different name - to no avail. I still see the ClassCastException with "Proxy $" string. I asked this question to Bill after his EJB 3.0 talk at JBOSSWorld in Vegas. His answer was that EJB spec has changed and follow the instructions on the wiki page - but that didn't help. Also, how do I check the global JNDI tree?
A: See DisplayTheJDNITreeWithTheJMXConsole on how to view the JNDI tree.
Follow-up: Thanks for the information. I checked the JNDI tree on the JBOSS instance. As I suspected, the Session Bean I am trying to lookup is present. It's called (VCCSecurity/UsersBean/local). If the object was not present, I would get some other Java Exception - not the ClassCastException. The session bean, based on the information in the JNDI view, also implements the following interfaces - (proxy: $Proxy88 implements interface com.eds.autoeng.Users,interface org.jboss.ejb3.JBossProxy,interface javax.ejb.EJBLocalObject). When I try to cast the object to the Users interface, it throws up the ClassCastException. Any clues?
A: I had multiple copies of the interface (one in each jar) in the ear file. Kept the one in ejb jar and got rid of the rest of them. Problem Solved.