Bài giảng Lập trình mạng 2 - EJB: Stateless Session Bean - Nguyễn Xuân Vinh

Tóm tắt Bài giảng Lập trình mạng 2 - EJB: Stateless Session Bean - Nguyễn Xuân Vinh: ...tjava.rmi.RemoteRemote ObjectJVMJVMACCP2005/EJB 2.0/Session 3/10 of 31The Home InterfaceHome Interface Find EJB ObjectsCreate EJB Objects Destroy EJBObjectsEJB specifies certain methods that the home interface has to support. These methods are defined in the javax.ejb.EJBHomepublic interface javax.e...ed while writing a session bean setSessionContext(SessionContext ctx)ejbCreate()ejbPassivate()Business methodsejbRemove()ejbActivate()ACCP2005/EJB 2.0/Session 3/17 of 31The setSessionContext (SessionContext ctx)ContainersetSessionContext()Beanpublic class theBean implements SessionBean{ private Sess...ing information about enterprise bean, remote interface and home interface. ejb.jar has to be present in the directory called META-INF. ACCP2005/EJB 2.0/Session 3/23 of 31jboss.xmlProvides the container information about the JNDI mapping, persistence information and database mapping. This file also ...

ppt30 trang | Chia sẻ: havih72 | Lượt xem: 266 | Lượt tải: 0download
Nội dung tài liệu Bài giảng Lập trình mạng 2 - EJB: Stateless Session Bean - Nguyễn Xuân Vinh, để tải tài liệu về máy bạn click vào nút DOWNLOAD ở trên
EJB: Stateless Session BeanPresenter: Nguyễn Xuân VinhInformation Technology FacultyNong Lam UniversityACCP2005/EJB 2.0/Session 3/2 of 31Session ObjectivesIdentify the constituents of an Enterprise JavaBean.Define a bean class, EJB object, home interface, home object and deployment descriptors.Define a stateless session beanWrite programs related to stateless session beansCompile and deploy stateless session beansACCP2005/EJB 2.0/Session 3/3 of 31Review of Session 2-(1of 2)In session 2 we discussedFour stages are followed while developing business solutionsSix parties are involved while deploying Enterprise JavaBeansLogical three-tier architecture of EJB: The ClientThe EJB ServerThe DatabaseThe EJB Container resides inside the EJB server. The container acts as a buffer between the bean and the outside world.The responsibility of the EJB Server and ContainerACCP2005/EJB 2.0/Session 3/4 of 31Review of Session 2-(2 of 2)The server and the container provide following services to the developer * Transaction support * Security support * Persistence support * Support for management of multiple instances .An Enterprise Java Bean can be classified into: * Session Beans * Entity Beans * Message-Driven BeansACCP2005/EJB 2.0/Session 3/5 of 31Components of an Enterprise BeanComponents of an enterprise bean The bean classThe EJB objectThe Remote interfaceDeploymentDescriptorsThe Home InterfaceThe EJB-jar fileThe Home objectThe Local InterfaceThe LocalHome InterfaceACCP2005/EJB 2.0/Session 3/6 of 31The Bean classWell-defined interfaceThe BeanBound Communicates to the client through the interfaceContainerWorks in any container with the help of these interfacespublic interface javax. ejb. EnterpriseBean extends java.io.Serializable {}Once the above interface is implemented, the bean class is confirmed ACCP2005/EJB 2.0/Session 3/7 of 31The EJB Object Client CodeEJB Container/Server1 .Calls a method4.Returns method  to clientHome ObjectEJB Object3. Returns the MethodEnterprise Bean Instance 2. Delegates method to beanThe container is the middleman between the client and the bean. It manifests itself as a single network-aware object. This network-aware object is called the EJB ObjectACCP2005/EJB 2.0/Session 3/8 of 31The Remote InterfaceRemote Interfaces derived from javax.ejb.EJBObject Business MethodsDefinePerform Functionality of the beanjavax.ejb.EJBObjectpublic interface java.rmi.RemoteInterfaceextends javax.ejb.EJBObject{	public abstract javax.ejb.EJBHome getEJBHome() 	throws java.rmi.RemoteException;}ACCP2005/EJB 2.0/Session 3/9 of 31Relationship between Java RMI and EJB ObjectsBeanJava Virtual MachineBeanJava Virtual MachineLocation TransparencyRemote Method InvocationPortability of Client CodeEJB Objectjava.rmi.RemoteRemote ObjectJVMJVMACCP2005/EJB 2.0/Session 3/10 of 31The Home InterfaceHome Interface Find EJB ObjectsCreate EJB Objects	Destroy EJBObjectsEJB specifies certain methods that the home interface has to support. These methods are defined in the javax.ejb.EJBHomepublic interface javax.ejb.EJBHome extends java.rmi.Remote { public abstract EJBMetaData getEJBMetaData()	throws java.rmi.RemoteException;}ACCP2005/EJB 2.0/Session 3/11 of 31The Methods in EJB HomegetEJBMetaData(): It is this method that gets information about the beans that are being worked onremove(): This method destroys an EJB objectThe methods that fall under the EJB Home areACCP2005/EJB 2.0/Session 3/12 of 31The Home Object Client CodeHome ObjectEJB ObjectEnterprise BeansEJB Container/Server1. Requests new EJB Object3. Returns the objects2. Creates new objectACCP2005/EJB 2.0/Session 3/13 of 31Deployment DescriptorsDeployment DescriptorsLife cycle requirements and Bean managementPersistence RequirementsTransaction Requirements Security ManagementClasses that form the beanHome InterfaceRemote InterfaceEJB ServerACCP2005/EJB 2.0/Session 3/14 of 31Life Cycle of a Session BeanA session bean may last as long as the client session. Will not survive if the application server changes or crashes. They are objects which are present in-memory which die along with the surrounding environment and are not persisted in a database.ACCP2005/EJB 2.0/Session 3/15 of 31Conversational and Non Conversational BeansA conversation stretches across a business process with respect to the client.A stateless session bean conducts a conversation that spreads over a single method call.Stateful session beans can retain their conversational state.ClientConversationBeanACCP2005/EJB 2.0/Session 3/16 of 31Writing a Session BeanThe six methods to be followed while writing a session bean setSessionContext(SessionContext ctx)ejbCreate()ejbPassivate()Business methodsejbRemove()ejbActivate()ACCP2005/EJB 2.0/Session 3/17 of 31The setSessionContext (SessionContext ctx)ContainersetSessionContext()Beanpublic class theBean implements SessionBean{	private SessionContext ctx;	public void setSessionContext(SessionContext ctx)	{	this.ctx=ctx;	}	. . . . . . . }Session Context(Gateway)AssociatesACCP2005/EJB 2.0/Session 3/18 of 31Business Methods	import javax.ejb.*;	public class sess implements Sessionbean{	public int multiply(int a, int b){	return (a*b); 	}	 ejbPassivate() ejbCreate() ejbActivate() ejbRemove() . . . . . . . 	 }Business methods are written to solve business logic problems. Business methods are contained in the remote interface of the bean for the client to access them.ACCP2005/EJB 2.0/Session 3/19 of 31Using JNDI to lookup Home ObjectsHome Object Enterprise beansEJB ObjectClientJNDINaming Service1. Retrieve Home Object3. Creates an EJB Object5. Return Object reference6. Invokes business methodsEJB Container/Server4. Create EJB Object2. Returns reference to the home object7. Delegates request to beanACCP2005/EJB 2.0/Session 3/20 of 31Steps in accessing Home ObjectsThe steps followed by the client code to get a referenceThe setting up of the EnvironmentDestroying the EJB ObjectCalling a MethodCreating an ObjectRetrieving the home objectsThe Initial ContextACCP2005/EJB 2.0/Session 3/21 of 31Pooling of Stateless Session BeanClientEJB ObjectInvokesEJB Server/ContainerBeanBeanBeanBean InvokesACCP2005/EJB 2.0/Session 3/22 of 31Deployment Descriptorsspecifies how the container is supposed to create and manage the enterprise bean object. defines the name of the enterprise bean class, and the names of the home and remote interfaces.ejb-jar file provides naming information about enterprise bean, remote interface and home interface. ejb.jar has to be present in the directory called META-INF. ACCP2005/EJB 2.0/Session 3/23 of 31jboss.xmlProvides the container information about the JNDI mapping, persistence information and database mapping. This file also has to be put into the META-INF file. 	 	Welcome	 	Welcome 	 ACCP2005/EJB 2.0/Session 3/24 of 31Creating the jar fileWelcome.classDeployment DescriptorsWelcome.jarWelcomebean.classWelcomehome.classA jar file is created to package the three java files, namely the bean class, the home interface and the remote interface. The XML files Namely ejb-jar.xml and jboss.xml are also present in the jar file. ACCP2005/EJB 2.0/Session 3/25 of 31Deploying the beanthe newly created .jar file has to be copied into the deploy directory on the server. D:\bin\jboss\deploy.jar filejar cvf welcome.jar Welcome.class Welcomehome.class Welcomebean.class META-INFACCP2005/EJB 2.0/Session 3/26 of 31Accessing from Client sidePossible ClientsOrdinary JavaBeanEnterprise JavaBeanJSP PageServletAppletEnterprise JavaBeanHomeObjectEJB ObjectJNDI lookupcreate()Business MethodsACCP2005/EJB 2.0/Session 3/27 of 31Summary - 1The 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.The bean class contains the implementation of the business logic methods. The EJB container performs certain important management functions when it intercepts client requests. These management functions are:* Transaction logic* Security logic* Bean instance logicThe Remote interface duplicates the methods exposed by the bean class. ACCP2005/EJB 2.0/Session 3/28 of 31Summary - 2Responsibilities of the EJB home object:* Creating EJB objects* Searching for existing EJB Objects* Removing EJB ObjectsThe 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.ACCP2005/EJB 2.0/Session 3/29 of 31Summary - 3Three classes are essential for deployment: * Home Interface* Remote Interface* Bean classThe ejb-jar.xml file is a compressed file that contains the declarations of the enterprise bean class, the remote interface and the home interface.It is important to have a client because EJB will not function without the client. This client can be:* An ordinary JavaBean* Another EJB* A JSP Page* A servlet* An applet*A stand-alone applicationHỎI ĐÁP

File đính kèm:

  • pptbai_giang_lap_trinh_mang_2_ejb_stateless_session_bean_nguyen.ppt