cancel
Showing results for 
Search instead for 
Did you mean: 

Dynamic Calling of EJB

former_member343107
Participant
0 Kudos

Hi,

I want to call an EJB in NetWeaver. I don't know the class of its Home Interface. I only know its JNDI name. So I use the following method.

1. Context context = new InitialContext();

2. Object home = context.lookup( name );

3. EJBHome ejbHome =(EJBHome) PortableRemoteObject.narrow (home, EJBHome.class);

But I got the exception "java.lang.ClassCastException: javax.ejb.EJBHome" when the program runs into 3.

I check the "home" variable, its type is "class com.sap.engine.interfaces.cross.ObjectReferenceImpl".

Could somebody tell me why this doesn't work? Is there any way to call an EJB when we don't know the Home Interface type and only know its JNDI name and the business method?

Accepted Solutions (0)

Answers (1)

Answers (1)

Former Member
0 Kudos

Hi Jerome

For each enterprise bean, the EJB-jar file must include the following class files:

1)Bean class

2)Home and Remote Interfaces

3)Primary key class, if the bean is an entity bean

Context initialContext = new InitialContext();

AccountHome accountHome = (AccountHome)

javax.rmi.PortableRemoteObject.narrow(

initialContext.lookup(“java:comp/env/ejb/accounts”),

AccountHome.class);

Sai

former_member343107
Participant
0 Kudos

Hi Sai,

Thanks for your information. In our project, we cannot know the home interface, remote interface or bean class. Our EJB-calling program will be deployed to NetWeaver first. Others will develop the called EJB, deploy that EJB to NetWeaver, and put the metadata of that EJB into an registry. Our calling progrm will get the JNDI name from the registry and dynamically call that EJB. So we cannot hardcode the class name of the called EJB.

0 Kudos

Hi Jerome,

You're getting the ClassCastException because you haven't added the bean's home interface to the classpath of the client. Therefore the looked up object cannot be loaded and then type-casted to javax.ejb.EJBHome. Of course you cannot add it during testing because you don't know it. However, I guess that at runtime you will have all the necessary classes in the classpath, so this scenario should work then.

Hope this helps.

Best regards,

Vesselin.

former_member343107
Participant
0 Kudos

Hi Vesselin,

Thanks a lot for the information. The caller is an EJB. As far as I know, one EJB cannot access classes of other EJB if the <ejb-ref> element of the caller's ejb-jar.xml is not set to be the called EJB. However, in our scenario, the called EJB is developed after the caller EJB is deployed to NetWeaver. So the caller cannot put the reference to the called EJB in its ejb-jar.xml file. So how can we make the classes of later-deployed EJB visible to the earlier-deployed EJB?

Best Regards

Jerome