Component-Based Software Engineering ECE493-Topic 5 Winter 2007 Lecture 24 Java Enterprise (Part B) Ladan Tahvildari Assistant Professor Dept. of Elect. & Comp. Eng. University of Waterloo J2EE Application Server Java 2 Enterprise Edition standardizes interfaces for Application Server components 2 Java versus.net for the Enterprise EJB Overview.NET COM+ Components Transactions Java EJB Session Beans (Stateless, Stateful) Entity Beans Message-Driven Beans Java Transaction Service (JTS) Loosely Coupled Events Java Message Service (JMS) Queued Components Can be implemented through JMS Object Pooling Instance Pooling JIT Activation ASP.NET Activation/Passivate (on stateful beans) Java Server Page (JSP) 3 4
Types of Enterprise Java Beans Session Beans For transient functions Represents conversational state Typically one per request Data is non-persistent Lifetime is limited by the client s once the client exits, the session bean and data are gone Simple and easy to program Light-weight 5 6 Stateful Session Bean s Life Cycle Stateful Session Bean s Life Cycle The client invoke the create method The EJB container : Instantiates the bean Invokes the setsessioncontext Invokes ejbcreate The bean is ready,. While in the ready state EJB container may passivate the bean moving it from memory to secondary storage A client may invoke a business method EJB container may activate a bean,moving it back to the ready stage, and then calls the bean's ejbactivate method A client may invoke the remove method and the container calls the bean's ejbremove method 7 8
Stateless Session Bean s Life Cycle Stateless Session Bean s Life Cycle The client invoke the create method The EJB container : Instantiates the bean Invokes the setsessioncontext Invokes ejbcreate The bean is ready. While in the ready state A client may invoke a business method A client may invoke the remove method and the container calls the bean's ejbremove method It s never passivate 9 10 Types of Enterprise Java Beans Entity Bean Transactional in behavior Can be shared among clients Persistent: data exists permanently after client quits Corresponds to a row of a relational database The persistence (storing into the database) can be done: automatically by the container (CMP) or explicitly by the bean (BMP) In BMP, you write the code for database access calls This may be additional responsibility but it gives control to the bean developer. 11 12
Entity Bean s Life Cycle Entity Bean s Life Cycle The EJB container: Creates the instance Calls the setentitycontext The entity bean moves to a pool of available instances While in the pool: Instance is not associated with any particular object identity All instances in the pool are identical EJB container may assign an identity to an instance when moving it to the ready stage invoking the ejbactivate method A client may invoke the create method EJB container calls ejbcreate and ejbpostcreate EJB container may remove the instance invoking unsetentitycontext While in the ready state: A client may invoke entity bean's business methods A client may invoke the remove method EJB container calls the ejbremove method EJB container may invoke the ejbpassivate method 13 14 Types of Enterprise Java Beans Message-Driven Bean A message driven bean is an enterprise bean that allows J2EE applications to process messages asynchronously It acts as a JMS listener, which is similar to an event listener except that it receives messages instead of events. The messages can be sent by any J2EE component: an application client another enterprise bean a web component a non-j2ee system using JMS Retain no data or conversational state 15 16
Message Driven Bean s Life Cycle EJB container creates a pool of message-driven bean instances For each instance, the EJB container instantiates the bean : It calls the setmessagedrivencontext It calls the instance's ejbcreate Like a stateless session bean,it s never passivated, It has only two states: Nonexistent Ready to receive messages While in the ready state : EJB container may call onmessage EJB container may call the ejbremove Message Driven Bean s Life Cycle 17 18