aspose file tools*
The moose likes EJB and other Java EE Technologies and the fly likes EJB 3.0 Lookup (or inject) stateful SessionBean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "EJB 3.0 Lookup (or inject) stateful SessionBean" Watch "EJB 3.0 Lookup (or inject) stateful SessionBean" New topic
Author

EJB 3.0 Lookup (or inject) stateful SessionBean

Morten Franorge
Ranch Hand

Joined: Jul 29, 2005
Posts: 137
If you have a stateful session bean that looks like this:

@Stateful
public class ShoppingCart{

private String custNo;

@Init
public void initCart(Strig custNo){
this.custNo=custNo
}
}

Now from my understanding, @Init is EJB 3.0 for ejbCreate.
How do I retrieve a reference with the custNo as input?
How do I do with lookup, and how do I do it by dependency injection?

Also, how would you store that stateful session bean in your webapplication (If your using sfsb as the controller)? You can't put the handle in the httpsession? How do you get the handle in ejb 3.0?


SCJP 1.4, SCBCD 1.3, SCBCD 5.0, SCEA J2EE, SCEA 5.0
Mike Keith
author
Ranch Hand

Joined: Jul 14, 2005
Posts: 304
Hi Morten,

The @Init annotation is used for adapting from EJB 2.1 style home interface to EJB 3.0 bean class. The regular way to initialize a stateful session bean is by calling the business method directly after the instance is obtained. The client code would obtain the instance and then invoke initCart(custNo) on it.

You should not typically inject SFSB, unless it is into another SFSB or into a Java EE client. You should use @EJB on the referencing class (e.g. your servlet) to declare the ejb-ref and then do a JNDI lookup in the code to obtain the instance. This instance could then be placed directly in your Http session. There is an example of doing this exact thing in our Pro EJB 3 book, so if you have a copy look near the end of Chapter 3.

-Mike


-Mike
Pro JPA 2: Mastering the Java Persistence API
Morten Franorge
Ranch Hand

Joined: Jul 29, 2005
Posts: 137
I have the book, I'll check it out :-)
Morten Franorge
Ranch Hand

Joined: Jul 29, 2005
Posts: 137
Originally posted by Mike Keith:

There is an example of doing this exact thing in our Pro EJB 3 book, so if you have a copy look near the end of Chapter 3.


Exactly what I was looking for. This will also work if the servlet has a remote reference (ie the servlet container is running on a different server than the ejb container)?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: EJB 3.0 Lookup (or inject) stateful SessionBean