• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Cannot narrow stub??

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I previously had the following fragment of code inside a servlet (which worked fine):

Context ctx = new InitialContext();
BeanEJB beanEJB = null;
BeanEJBHome beanHome = null;

Object obj = ctx.lookup("ejb/BeanEJB");
beanHome = (BeanEJBHome ) PortableRemoteObject.narrow(obj, BeanEJBHome.class);

However, I moved the above code to the run() method of a new thread I created and now the PortableRemoteObject.narrow() call throws a ClassCastException.

Can anyone help?

Thank you!
 
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As the EJB spec says: Don't create threads in an EJB container, you might run into problems.
 
ravinderSingh singh
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for clarifying the problem (the code is in a web WAR archive but I'm assuming the same applies).

Is there any work around since I need a certain EJB method to be invoked at a certain point in time (I'm using the Timer and TimerTask classes)?

Thank you!
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, I misread your post. I suspect a classloader problem. Try doing this after your lookup and report the results.
 
ravinderSingh singh
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I executed the above 4 statements are here's what resulted:

1) com.beans.BeanEJBBean_l6o3nb_HomeImpl_WLStub
2) false
3) weblogic.utils.classloaders.GenericClassLoader@3d1402
finder: weblogic.utils.classloaders.MultiClassFinder@646128
4) weblogic.utils.classloaders.ChangeAwareClassLoader@523632
finder: weblogic.utils.classloaders.MultiClassFinder@268a10

Here's the full exception that is thrown:
java.lang.ClassCastException: Cannot narrow remote object to com.beans.BeanEJBHome.
 
Ranch Hand
Posts: 775
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dumb question, but here goes. Is "com.beans.BeanEJBHome" literally the name of the home interface class that you specified in your ejb-jar deployment descriptor. Not a superinterface, or something like that, but literally the one for this particular bean?
 
ravinderSingh singh
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, it is the exact name of the home interface.

The three classes/interfaces all reside in the package com.beans and have the following names:

BeanEJB --> Remote Interface
BeanEJBHome --> Home Interface
BeanEJBBean --> Bean implementation
 
Reid M. Pinchback
Ranch Hand
Posts: 775
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You mentioned moving code into a run() method. Did you move *all* of the code that you show above into the body of the run method, or is part of it inside the run method (executed by the thread), and part of it outside the run method? I'm wondering if you are running into some problem with a class depending on thread-specific storage (e.g. a classloader).
 
ravinderSingh singh
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Yeah, I copied the above code exactly from the run method of my thread. A method invoked latter (on the remote interface) does use a member variable of the thread but that isn't until after the ClassCastException is thrown.
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Almost certainly the problem is that the thread's context class loader is not set correctly because the server does not know about your thread. If you must create a thread, grab the contextClassLoader and then set it in your thread.

However, it's best not to create threads in the server. A non-thread solution would be to send a JMS message which causes a message-driven bean to invoke your EJB method.
 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

You are looking up a remote EJB home object (although you're in a servlet-that makes only sense if your servlet engine is on another host than the ejb container).

The WAR archive must then include the stub for the EJB Object and the EJB Home object. If you try to narrow a remote object, the stub MUST BE PRESENT on gthe CLASSPATH! If not, a ClassCastException is thrown (until 1.4, using Java 5 a NullPointerException is thrown!)

so just run:
(and for the EJB Object)
and include the stubs in the war archive
and the problem should dissapear. Write a message if not. Bye.

[ January 13, 2006: Message edited by: Marco Barenkamp ]
[ January 13, 2006: Message edited by: Marco Barenkamp ]
 
I'm THIS CLOSE to ruling the world! Right after reading this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic