I have implemented a basic EJB Object in my code. Then I created a jar for this ejb. The jar file contains:
ejb-jar.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN" "http://java.sun.com/dtd/ejb-jar_2_0.dtd">
<ejb-jar>
<display-name>NewTransactionEjb</display-name>
<enterprise-beans>
<session>
<display-name>NewTransactionEjb</display-name>
<ejb-name>NewTransactionEjb</ejb-name>
<home>gr.cosmote.mdb.NewTransactionEjb.NewTransactionEjbHome</home>
<remote>gr.cosmote.mdb.NewTransactionEjb.NewTransactionEjb</remote>
<ejb-class>gr.cosmote.mdb.NewTransactionEjb.NewTransactionEjbBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>
</enterprise-beans>
<assembly-descriptor>
<container-transaction>
<method>
<ejb-name>NewTransactionEjb</ejb-name>
<method-name>*</method-name>
</method>
<trans-attribute>NotSupported</trans-attribute>
</container-transaction>
</assembly-descriptor>
</ejb-jar>
Then I deployed the EJB by placing the jar in webapps folder and from the server logs it seems to have been deployed successfully. But when I try to reference the ejb from my code I get error. Code:
Properties p = new Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.core.LocalInitialContextFactory");
InitialContext context = new InitialContext(p);
ejbHome = (EJBHome) context.lookup("java:global/NewTransactionEjb/NewTransactionEjb");
I have used "java:global/NewTransactionEjb/NewTransactionEjb" because upon ejb deployment I see this line in the server logs:
03-Sep-2021 17:31:23.628 INFO [Catalina-utility-1] org.apache.openejb.assembler.classic.JndiBuilder.bind Jndi(name=global/NewTransactionEjb/NewTransactionEjb!gr.cosmote.mdb.NewTransactionEjb.NewTransactionEjbHome)
--> Ejb(deployment-id=NewTransactionEjb)
In the execution it seems the the above code is working and it retrieves the EJBHome, but when I try to cast it to my own home interface class it fails with the following error:
java.lang.ClassCastException: com.sun.proxy.$Proxy362 cannot be cast to
gr.cosmote.mdb.NewTransactionEjb.NewTransactionEjbHome
What am I doing wrong?
I have finally found the solution to this issue. To be precise the actual problem has nothing to do with the exception which is just confusing. So I will mention my problem anyway, in case someone faces the same issue.
My problem was that 2 of the EJB classes (home interface and remote bean interface) were also in TOMEE /lib directory except for the EJB deployable jar file which was placed in /webapps directory. So I removed them from the jar file (now it contains only the bean implementaion class), deployed it and everything is working fine.