cd56fabf07cb7d98f03de53ce79d546a.ppt
- Количество слайдов: 16
Enterprise Java. Beans Session Beans
Session Beans • Session beans are • • • a type of Enterprise Java. Bean 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
State management modes • 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
Stateless vs. Stateful
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. Session. Bean interface or be annotated with @Stateless or @Stateful • The bean interface may be implemented by the developer, or it can be generated automatically
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 Calculator. Remote extends Calculator{ } @javax. ejb. Local public interface Calculator. Local extends Calculator{ }
Example: Stateless session bean • Bean class: @javax. ejb. Stateless public class Calculator. Bean implements Calculator. Remote, Calculator. Local { public int add(int x, int y){ return x + y; } public int subtract(int x, int y){ return x - y; } }
Example: Stateless session bean • Session bean client: public class Client{ public static void main(String[] args) throws Exception { Context ctx = new Initial. Context(); Calculator calculator. Remote = (Calculator) ctx. lookup("Calculator. Bean/remote"); System. out. println("1 + 1 = " + calculator. Remote. add(1, 1)); } } • VM arguments: -Djava. naming. factory. initial=org. jnp. interfaces. Naming. Context. Factory -Djava. naming. factory. url. pkgs=org. jboss. naming: org. jnp. interfaces -Djava. naming. provider. url=localhost
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
When to use stateful beans? 1. Typical example – shopping cart 2. Use it when bean needs to hold information about the client across method invocations 3. Advantages 1. Transient information can be stored in the instance variables, not it the database 2. Save bandwidth on method invocations 4. Disadvantages 1. Bad performance and poor scalability 2. Can be swapped in and out of memory (activated and deactivated), state gets stored
Example: Stateful session bean • Bean interfaces: public interface Shopping. Cart { void buy(String product, int quantity); Hash. Map<String, Integer> get. Cart. Contents(); @Remove void checkout(); }
Example: Stateful session bean @Stateful @Remote(Shopping. Cart. class) public class Shopping. Cart. Bean implements Shopping. Cart, Serializable{ private Hash. Map<String, Integer> cart = new Hash. Map<String, Integer>(); public void buy(String product, int quantity){ if (cart. contains. Key(product)){ int currq = cart. get(product); currq += quantity; cart. put(product, currq); } else { cart. put(product, quantity); } } public Hash. Map<String, Integer> get. Cart. Contents(){ return cart; } @Remove public void checkout(){ System. out. println("To be implemented"); } }
References • EJB fundamentals and session beans http: //www. javaworld. com/javaworld/jw-02 -2006/jw 0213 -ejb. html • Session. Bean Presentation http: //www. javapassion. com/j 2 ee/Session. Bean. pdf • A quick look at EJB 3. 0 Stateless Session Beans http: //www. indicthreads. com/articles/361/ejb 3_ stateless_session_ejb_tutorial. html
cd56fabf07cb7d98f03de53ce79d546a.ppt