EJB 3.0 : Stateful Session Bean
What is Stateful Session Bean? |
- A stateful bean contains a conversational state that is retained across method calls and transactions.
- It is having some state which differentiate from the stateless session bean.
- The state of stateful session bean represents the conversation between the client and the bean,
- It is like the one-to-one mapping.
Life Cycle for the Stateful Session Bean
- Does not Exist State
- Ready State
- Passive State
Stateless Vs Stateful
Stateless | Stateful |
---|---|
a pool of bean instances serve all clients | a bean instance is allocated to a particilar client |
client calls can be served by different bean instances | method calls from a same client is always served by the same bean instance |
a client cannot set instance variables on a per-client basis since as the client call can be handled by different bean instance possibly on a different server | methods can set instance variables and they remain valid for the client |
Interaction between Client, Bean instance, Container for Stateful Session Bean
Sequence of Operations
1. Client calls a create(…) method on the home object
2. The home object or container instantiates bean instance
3. The home object or container creates a Session Context object, and passes it to the instance in a setSessionContext() call
– SessionContext object contains information on runtime context
4. The home object or container calls ejbCreate(…) method whose arguments match the create(…) call on the home interface
– Bean instance initializes itself and is ready for use
5. The home object or container instantiates an EJB object for the bean instance
– There is typically a one-to-one relationship between clients, EJB objects, and bean instances for stateful session beans
Sequence of Operations
6. The client calls whatever business methods it needs on the EJB object
7. EJB object handles system operations
8. EJB object passes these calls on to the bean instance
9. Client calls remove() on the EJB object
10. EJB object or container calls ejbRemove() on the bean instance
Example Of Stateful Session Bean |
Click Here to Download the Code with jar file
Stateless Session Bean Code for Deployment
ShoppingCart.java
package com.javaskool.example;
import java.util.ArrayList;
import javax.ejb.Remote;
@Remote
public interface ShoppingCart
{
void addItem(String item);
void removeItem(String item);
ArrayList getCartItems();
}
ShoppingCartLocal.java
package com.javaskool.example;
import java.util.ArrayList;
import javax.ejb.Local;
@Local
public interface ShoppingCartLocal
{
void addItem(String item);
void removeItem(String item);
ArrayList getCartItems();
}
ShoppingCartBean.java
package com.javaskool.example;
import java.util.ArrayList;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.Remove;
import javax.ejb.Stateful;
@Stateful(mappedName = "ccc")
public class ShoppingCartBean implements ShoppingCart, ShoppingCartLocal
{
public ArrayList cartItems;
public ShoppingCartBean() {
}
public void addItem(String item) {
cartItems.add(item);
}
public void removeItem(String item) {
cartItems.remove(item);
}
public void setCartItems(ArrayList cartItems) {
this.cartItems = cartItems;
}
public ArrayList getCartItems() {
return cartItems;
}
@PostConstruct
public void initialize() {
cartItems = new ArrayList();
}
@PreDestroy
public void exit() {
System.out.println("Saved items list into database");
}
}
ShoppingCartTest.java
package com.javaskool.example;
import com.javaskool.example.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import javax.ejb.EJB;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.naming.InitialContext.*;
public class ShoppingCartTest
{
public ShoppingCartTest() {}
@EJB
static ShoppingCart shCart;
public static void main(String[] args) {
ShoppingCartTest shoppingCartTest = new ShoppingCartTest();
shoppingCartTest.doTest();
}
void doTest()
{
InitialContext ictx;
try {
Properties p= new Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
p.put(Context.PROVIDER_URL,"t3://localhost:7001");
ictx = new InitialContext(p);
shCart = (ShoppingCart) ictx.lookup("ccc#com.javaskool.example.ShoppingCart");
System.out.println("ShoppingCart Lookup....");
System.out.println("Adding Books.....");
shCart.addItem("Core Java Book");
shCart.addItem("Servlet Book");
shCart.addItem("JSP Book");
System.out.println("Printing Cart Items");
ArrayList cartItems = shCart.getCartItems();
for (String item: (List<String>)cartItems) {
System.out.println(item);
}
}
catch (NamingException e) {
e.printStackTrace();
}
}
}
Recent Comments