aspose file tools*
The moose likes EJB and other Java EE Technologies and the fly likes access ejb from outside container Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "access ejb from outside container" Watch "access ejb from outside container" New topic
Author

access ejb from outside container

James Clarke
Ranch Hand

Joined: Oct 04, 2004
Posts: 148
Hi all,

Is it possible to access a stateless session bean or any other bean type from outside the EJB container?

I tried to do a look-up of a deployed ejb in Weblogic from a standalone Java program and had some problems, below is the stack trace of exception:

Exception in thread "main" java.lang.ClassCastException: Cannot narrow remote object weblogic.rmi.internal.BasicRemoteRef - hostID: '-5351748351393873728S:127.0.0.1:[7001,7001,-1,-1,-1,-1,-1]:myDomain10:AdminServer', oid: '286', channel: 'null' to com.titan.travelagent.TravelAgentRemote
at weblogic.corba.server.naming.ReferenceHelperImpl.narrow(ReferenceHelperImpl.java:206)
at weblogic.rmi.extensions.PortableRemoteObject.narrow(PortableRemoteObject.java:88)
at weblogic.iiop.PortableRemoteObjectDelegateImpl.narrow(PortableRemoteObjectDelegateImpl.java:32)
at javax.rmi.PortableRemoteObject.narrow(Unknown Source)
at com.titan.clients.Client.main(Client.java:30)


Below is the code that seems to cause the excepption:

TravelAgentRemote dao = (TravelAgentRemote)
PortableRemoteObject.narrow(ref,TravelAgentRemote.class);

I would like to know if this should be possible and if so is it good practice to use EJB from outside the application server where its deployed?

Thanks,

J.C
Reza Rahman
author
Ranch Hand

Joined: Feb 01, 2005
Posts: 580
    
    5
J.C.,

You can absolutely use EJBs outside the container. Indeed, that's one of the core EJB value propositions. On the exception that you are getting -- are you talking about EJB 2 or EJB 3? There is no need for the additional "narrow" call on EJB 3 remote objects.

Best regards,
Reza


Independent Consultant — Author, EJB 3 in Action — Expert Group Member, Java EE 6 and EJB 3.1
James Clarke
Ranch Hand

Joined: Oct 04, 2004
Posts: 148
Hi Reza,

Iam using EJB 3.0. the more complete version of the code I have given above:

Context jndiContext = getInitialContext( );
Object ref = jndiContext.lookup("EJBSample1titan_jarTravelAgentBean_TravelAgentRemote");//"TravelAgentBeanJ/remote"
print(ref.getClass().toString());
print("remote classloader:"+ref.getClass().getClassLoader().toString());
print("thread classloader:"+Thread.currentThread().getContextClassLoader().toString());
TravelAgentRemote dao = (TravelAgentRemote)ref;

output:
> class com.titan.travelagent.TravelAgentBean_12ko68_TravelAgentRemoteImpl_1001_WLStub
> remote classloader:weblogic.utils.classloaders.GenericClassLoader@af993e finder:
> weblogic.utils.classloaders.CodeGenClassFinder@f6ac0b annotation:
> local classloader:sun.misc.Launcher$AppClassLoader@a39137
> remote classloader:weblogic.utils.classloaders.GenericClassLoader@af993e finder:
> weblogic.utils.classloaders.CodeGenClassFinder@f6ac0b annotation:
> Exception in thread "main" java.lang.ClassCastException: com.titan.travelagent.TravelAgentBean_12ko68_TravelAgentRemoteImpl_1001_WLStub
at com.titan.clients.Client.main(Client.java:29)

Any idea what needs be changed?

Thanks,
James
Reza Rahman
author
Ranch Hand

Joined: Feb 01, 2005
Posts: 580
    
    5
James,

Looks like a class-loader issue. If your code is running in the web container, you need to make sure the interface classes are not duplicated (copied) in the web app classpath. Instead, you'll need to refer to the EJB-JAR as a web application module, so the app server class-loader loads in the interface class only once.

Cheers,
Reza
James Clarke
Ranch Hand

Joined: Oct 04, 2004
Posts: 148
Hi Reza,

The code thats looking up and trying to invoke the session bean is a stand-alone Java program as I mention in my first post.. any ideas how to get around this?

Thanks,
James
Reza Rahman
author
Ranch Hand

Joined: Feb 01, 2005
Posts: 580
    
    5
James,

Unfortunately, not off-hand. It looks like a class-loader issue (the interface implemented by the bean is not the "same" as the interface you are casting to). The other alternative is that you are looking up the wrong EJB. You might want to try explicitly specifying mappedName: http://forums.bea.com/thread.jspa?threadID=300004729. It's difficult to tell for sure, but I would say you might be looking at a WLS specific issue.

Cheers,
Reza
James Clarke
Ranch Hand

Joined: Oct 04, 2004
Posts: 148
Problem solved. The client stub used by weblogic need to be created and included in client classpath and the stub name should be used as the type when declaring+casting the object that results from the JNDI lookup.

Only tihng I dont like about this is the stub name is: TravelAgentBean_12ko68_TravelAgentRemoteImpl_1001_WLStub (very long!)

I have no idea how to inform WL to use a different name.

Thanks,

J.C
 
Consider Paul's rocket mass heater.
 
subject: access ejb from outside container