Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

EJB 3.0 Lookup (or inject) stateful SessionBean

 
Morten Franorge
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Mike Keith
author
Ranch Hand
Posts: 304
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Morten Franorge
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the book, I'll check it out :-)
 
Morten Franorge
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic