epesh
I'm Joseph Ottinger, editor of TheServerSide.com.

Calendar

««Nov 2009»»
SMTWTFS
1234567
891011121314
15161718192021
22232425262728
2930

My Bookmarks

My Top Tags

                                       

Mailing List

My RSS Feeds








Search Box

 

EJB 3.0 Stateless Bean Issues

posted Monday, 31 July 2006
EJB3 is great; I really like the simplicity. The injection features work like a charm; JPA is a decent and standard entity model. However, not a single one of the references I have seems to make the most common kind of usage for me.

To wit: a nonmanaged class looks up an EJB via JNDI. I can deploy successfully; I can even look up a JNDI reference without an exception, which tells me I have something in JNDI... but the lookup returns null, which tells me the binding couldn't find something. I'm looking up a local reference, and I'm assigning the result of lookup() to an Object, so there shouldn't be a casting issue. The only thing I can think of - having been an EJB user since EJB 1.1 or so - is that it's trying to bind a home interface for the EJB, and since it's EJB3, it doesn't really have one (nor should it.)

Maybe I'm just being clueless, and I should do the easiest thing: instantiate the local EJB as if it were a POJO. But that doesn't seem logical to me. It would work, I think, except it would act like an unmanaged object (which is bad) and plus, it'd be, uh, horribly tight coupling, which is pretty much exactly what I don't want.

So... what am I doing wrong? I'm sure I'm missing something simple; I just can't find what it is.

tags:        




1. John Flinchbaugh left...
Monday, 31 July 2006 11:25 am :: http://www.hjsoft.com/blog/

I'm not positive how JBoss-specific this is, but I've gotten away with looking up "BeanName/remote" on the InitialContext object from the web tier.

final Context context = new InitialContext();
final MyBean remote = (MyBean) context.lookup("MyBean/remote");

I guess I could also setup a local jndi mapping for this as well (in comp/env), but I've not gotten that far yet. I've not really looked at what Java EE 5 has done to the web tier.


2. John Flinchbaugh left...
Monday, 31 July 2006 1:40 pm :: http://www.hjsoft.com/blog/

Oops. To be clear, the lookup returns a stub implementing the remote interface, not an instance of the bean. final MyIfc remote = (MyIfc) context.lookup("MyBean/remote"); would have probably been clearer.


3. Brian Repko left...
Monday, 31 July 2006 5:35 pm

My understanding of the EJB3 spec is that there is no way to do it from non-managed code other than falling back to EJB2. Should your non-managed code be a Java EE application client? If so, then you might get some facilities to get to it but I'm not sure that ever got worked out in the spec writing.

Oh and thanks for promoting my EJB and Dependency Injection article a while back...I had asked this very question way back then.


4. Brian Repko left...
Monday, 31 July 2006 5:37 pm

What is Spring EJB3 support doing for lookup? I'd look there...yes, I'm being lazy.


5. Joseph Ottinger left...
Tuesday, 1 August 2006 7:16 am

Guys, thanks for the replies. However...

John, I know *how* to look up the bean. It's a local, not a remote; it's not in JBoss; I'm specifying the name in the deployment descriptors; I'm successfully looking up a name, but it has a null value.

Brian, injection doesn't work if it's not managed code. However, you're supposed to be able to look up an EJB3 bean via JNDI. Spring does this, for example.


6. Joseph Ottinger left...
Tuesday, 1 August 2006 7:51 pm

BTW, solved it through a search on the glassfish mailing list: see https://glassfish.dev.java.net/servlets/ReadMsg?list=users&msgNo=605


7. arkady left...
Wednesday, 11 February 2009 5:34 pm

Hey guys,

I been banging my head against a wall for 2 hours now, I keep getting the error javax.naming.NameNotFoundException: Name SearchHandlerBean is not bound in this Context

Its a simple bean, I have both the Local and Remote interfaces and I am using Tomcat5.5. I am making the call from a struts action class.

Context context = new InitialContext();

SearchHandlerBeanRemote shb = (SearchHandlerBeanRemote) context.lookup(SearchHandlerBean.RemoteJNDIName);

No matter what I try to look the name up with, I keep getting that error. If anyone could help that would be great!