Dynamic EJB Instantiation
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Arun_Ranganathan
Posted On:   Monday, August 16, 2004 11:00 PM

Hi, I am trying to instantiate SSBs dynamically bsed on JNDI. Desc: I have 5 beans (now) B1, B2, B3 , B4 & B5. All extending (Session Bean). Using the service Locator, I want to use Beans based on the JNDI names. Say, I get a sequence of B3, B1, B5 then the beans should be created in this sequence . Now the problem is that I get the HomeStub of B3 but am unable to Cast it to BaseHome. So I have to Hard_code it to B3Home. I want to avoid this. Code::::::::: Class clsHome = null; String strHome = null; BaseSessionHome bsHome = null; BaseSe   More>>

Hi,


I am trying to instantiate SSBs dynamically bsed on JNDI.


Desc:
I have 5 beans (now) B1, B2, B3 , B4 & B5. All extending (Session Bean). Using the service Locator, I want to use Beans based on the JNDI names.


Say, I get a sequence of B3, B1, B5 then the beans should be created in this sequence .


Now the problem is that I get the HomeStub of B3 but am unable to Cast it to BaseHome. So I have to Hard_code it to B3Home. I want to avoid this.


Code:::::::::


Class clsHome = null;


String strHome = null;


BaseSessionHome bsHome = null;


BaseSession bs = null;


EJBHome ejbHome = null;


int nBP = arListBPs.size();


for(int i = 0; i < nBP; i++)
{


strHome = (String)arListBPs.get(i);


clsHome = Class.forName(strHome);


ejbHome = srL.getEJBHome(strHome, clsHome);


bs = ((BaseSessionHome)ejbHome).create();


bs.process();


}


where,
- arListBPs has the list of JNDI names in the sequence B3, B1, B5


- srL is an instance of ServiceLOcator that does the usual protable remote etc... and returns EJBHome.


- All Session Beans have Process method.


I want to know:


1. Is this a good practice?


2. Why can I not cast it to BaseHome.


3. Is there another way to do this?



cheers
arun

   <<Less

Re: Dynamic EJB Instantiation

Posted By:   Arun_Ranganathan  
Posted On:   Wednesday, August 18, 2004 10:11 PM

Did it by using reflection:

ejbHome = srL.getEJBHome(strHome, clsHome);
Method mCreate = clsHome.getDeclaredMethod("create", null);
ejbObj = (EJBObject)mCreate.invoke(ejbHome, null);
bs = (BaseSession)ejbObj;
About | Sitemap | Contact