Bài giảng Lập trình mạng 2 - Stateful Session Bean - Nguyễn Xuân Vinh
Tóm tắt Bài giảng Lập trình mạng 2 - Stateful Session Bean - Nguyễn Xuân Vinh: ...cess is called activation. To passivate a bean a container uses Least Recently Used method.To activate a bean a container uses Just-in-Time (JIT) method.Rules for Conversational StateStorageConverted toMemory freedBit - blobConversational state of a beanWritten to storage in case of passivation Read...e()public void ejbPassivate()public void setSessionContext(SessionContext ctx)The Home Interfaceimport javax.ejb.*;import java.rmi.*; public interface Numberhome extends EJBHome{ Number create( int answer ) throws RemoteException, CreateException ; }Goals of the Client CodeIt acquires a JNDI init... conversations is comparatively less in a stateless session bean DisadvantagesClient-specific data needs to be given for every method invocationBandwidth could be reduced for other processes, as data has to be passed each timeInput/Output bottlenecks are more in stateless session beansAdvantages and...
EJB: Stateful Session BeanPresenter: Nguyễn Xuân VinhInformation Technology FacultyNong Lam UniversitySession ObjectivesDefine a Stateful Session BeanList the characteristics of a Stateful Session Bean.Write Stateful session bean programs.Differentiate between Stateless and Stateful Session beans.Review of Session 3 (1 of 3)The bean class, the EJB object, the remote interface, the home interface, the home object, the deployment descriptors, and the jar files constitute the enterprise bean.Bean class contains the implementation of the business logic methods. EJB container performs important management functions when it intercepts client requests such as:* Transaction logic* Security logic* Bean instance logicThe Remote interface duplicates the methods exposed by the bean class. Responsibilities of the EJB home object:* Creating EJB objects* Searching for existing EJB Objects* Removing EJB ObjectsReview of Session 3 (2 of 3)The deployment descriptor: A file that tells the EJB server about the classes, the home interface, and the remote interface that form the bean.The lifetime of a session bean may last till such time as that of a client session. It could be as long as a window is open or as long as an application is open. Session beans do not, therefore, survive application server crashes or machine crashes.Three classes are essential for deployment: Home Interface Remote Interface Bean classReview of Session 3 (3 of 3)The ejb-jar.xml file is a compressed file that contains the declarations of the enterprise bean class, the remote interface and the home interface.An EJB client can be:* An ordinary JavaBean* Another EJB* A JSP Page* A servlet* An applet*A stand-alone applicationCharacteristics of Stateful Session BeansConversional state occurs when a client calls a method on a bean.Pooling has to be done to conserve resources and enhances scalability.The container swaps out a bean and saves the conversational state to the hard disk or other storage devices. This process is called passivation.When the client requests for a method, the passivated conversational state is returned to the bean. The bean is again ready to meet the request. This process is called activation. To passivate a bean a container uses Least Recently Used method.To activate a bean a container uses Just-in-Time (JIT) method.Rules for Conversational StateStorageConverted toMemory freedBit - blobConversational state of a beanWritten to storage in case of passivation Read from storage into memory in case of activationConverted into data from bit-blobPassivation of a Stateful Bean ClientEJB ObjectEnterprise beanOther Enterprise beansStorage1. Invoke business methods5. Store passivated bean stateThe EJB Container/Server2. Take the least recently used bean3. Call ejbPassivate() 4. Serialize the bean stateActivation of a Stateful BeanEnterprise BeansEJB ObjectOther EnterpriseBeansStorageClientEJB Container/Server3. Reconstruct bean4. Call ejbActivate()5. Invoke business method1. Invoke business methods2. Retrieve the passivated state ofbeanRequirements for Stateful session beans. Remote InterfaceThe Bean ClassThe Home InterfaceThe Client CodeDeployment DescriptorThe Remote InterfaceStateful Session Bean Class1. number(){/ Implementation}Remote Interface(Defines business methods of bean)1. number()Number extends EJBObjectClientThe Bean ClassStateful Session Bean Class public int number(){ /* Actual Implementation*/ }public class Numberbean implements SessionBeanprivate SessionContext ctx;public int answer; public void ejbCreate(int answer) throws CreateExceptionpublic void ejbRemove()public void ejbActivate()public void ejbPassivate()public void setSessionContext(SessionContext ctx)The Home Interfaceimport javax.ejb.*;import java.rmi.*; public interface Numberhome extends EJBHome{ Number create( int answer ) throws RemoteException, CreateException ; }Goals of the Client CodeIt acquires a JNDI initial context and the naming context.Home object is located using JNDI.Client calls methods on the bean using the Home Object. Conversations are created using home interface and used from the client codeDeployment descriptors limit the pool size. Lastly, the EJB objects are removed.The Deployment Descriptor :ejb-jar.xml ejb-jar.xml file contains the declarations of Enterprise bean Home interface Remote interface Bean class Session Type Transaction typejboss.xmlIt contains the declaration of the actual ejb name and the JNDI name of the Session bean. Client code maps the actual ejb name from this JNDI name. Number Number Creating the jar fileThe jar file is created using java appropriate statementPackages the remote interface, the bean class, the home interface and the XML files.The jar file is copied into the deploy directory on the server and then deployed. The creation and copying of the file in the deploy directory of the server marks the completion of the bean. Advantages and Disadvantages of Stateless Session beanAdvantagesPooling is simple in stateless session bean as there is reuse of beans and this reduces overloadLoss of conversations is comparatively less in a stateless session bean DisadvantagesClient-specific data needs to be given for every method invocationBandwidth could be reduced for other processes, as data has to be passed each timeInput/Output bottlenecks are more in stateless session beansAdvantages and Disadvantages of Stateful Session BeansAdvantagesData is not pushed to the bean for every method invocationAll resources are stored in a database when the bean is passiveDisadvantagesConversation with the client maybe lost as there is caching of client conversation each time a bean is used.Bean state has to be activated and passivated in between method invocations. Therefore there can be I/O bottlenecks.Practices used for writing Stateful Session BeansAlways try to keep the conversations short.Try to use an EJB product that persists stateful conversations.Always write client code that takes into account bean failures.Summary - 1Removing a bean out of the container, and saving its resources to the database or file is known as passivation.The process of bringing back the passivated bean into the container with all its resources is known as activation.The strategy of passivating a bean that has been used the longest while ago is called the Least Recently Used method.Rules for the conversational state are put forward by Java object serialization.The container calls the ejbPassivate() method and tells the bean that it is going to passivate the bean. During activation, the ejbActivate() method is called, and the conversational state is read back into the memory. The container then re-constructs the memory-state using object serialization.Summary - 2This ejb-jar.xml file has to be present in the directory called META-INF. A jar file is created in order to package the three files, namely, the remote interface, the bean class, and the home interface.To deploy the bean, the newly created .jar file has to be copied into the deploy directory on the server.If the business process requires multiple invocations, a stateful session bean has to be used. However, if the business process is only for a single method call, the stateless session bean will suffice.HỎI ĐÁP
File đính kèm:
- bai_giang_lap_trinh_mang_2_stateful_session_bean_nguyen_xuan.ppt