180 likes | 352 Views
Enterprise JavaBeans. Session Beans. Session Beans. Session beans are a type of Enterprise JavaBean component designed to implement business logic responsible for managing processes , tasks , workflows
E N D
Enterprise JavaBeans Session Beans
Session Beans • Session beans are • a type of Enterprise JavaBean component • designed to implement business logic • responsible for managing processes, tasks, workflows • To accessan application, that is deployed on the server, the client invokes the session bean’smethods • Is similar to an interactive session • Is not shared between clients and is not persistent
State managementmodes There are two types of session beans • Stateless and Stateful
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
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
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
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 @Statelessor @Stateful • The bean interface may be implemented by the developer, or it can be generated automatically
Example: Stateless session bean Bean interfaces: publicinterface Calculator { publicint add(int x, int y); publicint subtract(int x, int y); } @javax.ejb.Remote publicinterface CalculatorRemote extends Calculator{ } @javax.ejb.Local publicinterface CalculatorLocal extends Calculator{ }
Example: Stateless session bean Bean class: @javax.ejb.Stateless publicclass CalculatorBean implements CalculatorRemote, CalculatorLocal { publicint add(int x, int y){ return x + y; } publicint subtract(int x, int y){ return x - y; } }
Example: Stateless session bean Session bean client: publicclass Client{ publicstaticvoid 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)); } } VM arguments: -Djava.naming.factory.initial=org.jnp.interfaces.NamingContextFactory -Djava.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces -Djava.naming.provider.url=localhost
Reusability of Stateless instances Container transparently reuses beaninstances to serve different clients • Pool of bean instances is created by container atappropriate time (e.g. at the time of system boot orwhen 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 asingle time – improves scalability of the system • clients can be idle between calls
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
Example: Stateful session bean publicinterface ShoppingCart { void buy(String product, int quantity); HashMap<String, Integer> getCartContents(); @Removevoid checkout(); } Bean interface:
Example: Stateful session bean @Stateful @Remote(ShoppingCart.class) publicclass ShoppingCartBean implements ShoppingCart, Serializable{ private HashMap<String, Integer> cart = new HashMap<String, Integer>(); publicvoid 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<String, Integer> getCartContents(){returncart;} @Remove publicvoid checkout(){System.out.println("To be implemented");} }
Demo project • Demo project is created to illustrate simple EJBs and explain how to deploy EJB module on JBoss application server • Subversion URL: http://java-eim.googlecode.com/svn/trunk/java-eim-demo-ejb-jboss • Check-out and read README.txt file
References • Book “Mastering Enterprise JavaBeans 3.0” http://www.theserverside.com/tt/books/wiley/masteringEJB3/index.tss • Article at JavaWorld: “EJB fundamentals and session beans” http://www.javaworld.com/javaworld/jw-02-2006/jw-0213-ejb.html
References • A quick look at EJB 3.0 Stateless Session Beans http://www.indicthreads.com/1447/a-quick-look-at-ejb-30-stateless-session-beans/ • EJB 3.0 Tutorial from JBoss http://docs.jboss.org/ejb3/app-server/tutorial/