1 / 16

Enterprise JavaBeans

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

akamu
Download Presentation

Enterprise JavaBeans

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Enterprise JavaBeans Session Beans

  2. 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

  3. State managementmodes • 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 @Statelessor @Stateful • The bean interface may be implemented by the developer, or it can be generated automatically

  9. 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{ }

  10. 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; } }

  11. 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

  12. Reusability of Stateless instances • Container transparently reuses beaninstances to serve different clients • Pool of bean instances are created by container atappropriate time (ex: 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

  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 publicinterface ShoppingCart { void buy(String product, int quantity); HashMap<String, Integer> getCartContents(); @Removevoid checkout(); } • Bean interfaces:

  15. 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");} }

  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

More Related