Presentation is loading. Please wait.

Presentation is loading. Please wait.

Enterprise JavaBeans Session Beans. Session beans are a type of Enterprise JavaBean component designed to implement business logic responsible for managing.

Similar presentations


Presentation on theme: "Enterprise JavaBeans Session Beans. Session beans are a type of Enterprise JavaBean component designed to implement business logic responsible for managing."— Presentation transcript:

1 Enterprise JavaBeans Session Beans

2 Session beans are a type of Enterprise JavaBean component designed to implement business logic responsible for managing processes, tasks, workflows To access an application that is deployed on the server, the client invokes the session bean’s methods Is similar to an interactive session Is not shared between clients and is not persistent

3 State management modes There are two types of session beans Stateless and Stateful

4 Stateless session beans Does NOT maintain a conversational state with the client Instance variables may contain a state specific to client, but ONLY for the duration of the invocation Offer BETTER scalability for applications that require large numbers of clients CAN implement a web service, but other types of EJBs cannot

5 Stateful session beans Instance variables represent the state of a unique client-bean session The state is retained for the duration of the client-bean session If the client removes the bean or terminates, the session ends and the state disappears Storing the state is a very resource-intensive process, so an application that uses stateful beans may not be easily scalable

6 Stateless vs. Stateful

7 Elements of a session bean Every session bean requires the presence of a bean interface and a bean class http://www.javaworld.com/javaworld/jw-02-2006/jw-0213-ejb.html

8 Bean interface and class A bean interface is the mechanism by which client code will interact with the bean internals The implementation of the business logic of a session bean is located in its bean class The bean class must either implement the javax.ejb.SessionBean interface or be annotated with @Stateless or @Stateful The bean interface may be implemented by the developer, or it can be generated automatically

9 Example: Stateless session bean Bean interfaces: public interface Calculator { public int add(int x, int y); public int subtract(int x, int y); } @javax.ejb.Remote public interface CalculatorRemote extends Calculator{ } @javax.ejb.Local public interface CalculatorLocal extends Calculator{ }

10 Example: Stateless session bean Bean class: @javax.ejb.Stateless public class CalculatorBean implements CalculatorRemote, CalculatorLocal { public int add(int x, int y){ return x + y; } public int subtract(int x, int y){ return x - y; }

11 Example: Stateless session bean Session bean client: public class Client{ public static void main(String[] args) throws Exception { Context ctx = new InitialContext(); Calculator calculatorRemote = (Calculator) ctx.lookup("CalculatorBean/remote"); System.out.println("1 + 1 = " + calculatorRemote.add(1, 1)); } -Djava.naming.factory.initial=org.jnp.interfaces.NamingContextFactory -Djava.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces -Djava.naming.provider.url=localhost VM arguments:

12 Reusability of Stateless instances Container transparently reuses bean instances to serve different clients Pool of bean instances are created by container at appropriate time (ex: at the time of system boot or when the size of pool becomes too small) Bean instances are then recycled Smaller number of bean instances (pool of bean instances) can serve larger number of clients at a single time – improves scalability of the system clients can be idle between calls

13 When to use stateful beans? Typical example – shopping cart Use it when bean needs to hold information about the client across method invocations Advantages Transient information can be stored in the instance variables, not it the database Save bandwidth on method invocations Disadvantages Bad performance and poor scalability Can be swapped in and out of memory (activated and deactivated), state gets stored

14 Example: Stateful session bean public interface ShoppingCart { void buy(String product, int quantity); HashMap getCartContents(); @Remove void checkout(); } Bean interfaces:

15 Example: Stateful session bean @Stateful @Remote(ShoppingCart.class) public class ShoppingCartBean implements ShoppingCart, Serializable{ private HashMap cart = new HashMap (); public void buy(String product, int quantity){ if (cart.containsKey(product)){ int currq = cart.get(product); currq += quantity; cart.put(product, currq); } else { cart.put(product, quantity); } public HashMap getCartContents(){ return cart;} @Remove public void checkout(){ System.out.println("To be implemented"); } }

16 References EJB fundamentals and session beans http://www.javaworld.com/javaworld/jw-02-2006/jw- 0213-ejb.html SessionBean Presentation http://www.javapassion.com/j2ee/SessionBean.pdf A quick look at EJB 3.0 Stateless Session Beans http://www.indicthreads.com/articles/361/ejb3_ stateless_session_ejb_tutorial.html


Download ppt "Enterprise JavaBeans Session Beans. Session beans are a type of Enterprise JavaBean component designed to implement business logic responsible for managing."

Similar presentations


Ads by Google