Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Session Bean Chuyên đề Lập trình Java & J2EE Chương 14 Biên soạn: Th.S Nguyễn văn Lành.

Similar presentations


Presentation on theme: "1 Session Bean Chuyên đề Lập trình Java & J2EE Chương 14 Biên soạn: Th.S Nguyễn văn Lành."— Presentation transcript:

1 1 Session Bean Chuyên đề Lập trình Java & J2EE Chương 14 Biên soạn: Th.S Nguyễn văn Lành

2 2 Concepts Session bean is a non-persistent object that implements some business logic running on the server. A session bean represents a transient conversation with a client A stateless session bean does not maintain a conversational state for its client A stateful session bean maintains a conversational state for its client throughout the session. The state is retained for the duration of the client- bean session and the state is discarded when the client removes the bean or terminates

3 3 Constitution of a session bean Remote interface: defines the business methods of the bean. Home interface: defines the lifecycle methods of the bean (like creating or removing a bean) Note: The remote and home interfaces provide the client's view of the bean. Enterprise Bean class: provides the implementation for the business methods. Deployment Descriptor: This configuration document defines the bean's runtime attributes, such as its transaction or security attributes Helper classes

4 4 Remote Interface Must derive from javax.ejb.EJBObject EJB remote interface must conform to RMI rules Any method must throw RemoteException Parameters passing to method and return data type must be RMI valid types, such as primitives, serializable objects or remote objects public interface Register extends EJBObject { public int signup(Customer cust) throws RemoteException; public int newAccount(Account acc) throws RemoteException; }

5 5 Home Interface Define methods for creating and destroy EJB object Container need Home interface to generate Home object- part of container Client invokes method on Home interface to ask for instantiate and remove EJB object Stateless session bean has only one create() method with no argument Stateful session bean may have multi create() method with different arguments to initiate its state – argument type must be RMI valid types

6 6 Home Interface

7 7 Must extends EJBHome interface Must define at least one create() method that return the remote interface type of the enterprise bean. (But an ejbCreate method returns void.) All create() methods must throw RemoteException and CreateException public interface TransactionHome extends EJBHome { public Transaction create(int accNo) throws RemoteException, CreateException; }

8 8 Enterprise Bean class implements the SessionBean interface. The SessionBean interface extends the EnterpriseBean interface, which in turn extends the Serializable interfaceSessionBeanEnterpriseBean The class is defined as public, cannot be defined as abstract or final. implements one or more ejbCreate methods with the following requirements The access control modifier must be public. The return type must be void Every create method in the home interface corresponds to an ejbCreate method in the bean class

9 9 Enterprise Bean class implements the business methods The method name must not conflict with one defined by the EJB architecture the arguments and return types must be legal types for the Java RMI API. contains a public constructor with no parameters. must not define the finalize method

10 10 Enterprise Bean class

11 11 Enterprise Bean class Container call setSessionContext() to associate a SessionContext with a bean ejbPassivate() should release any holding resources ejbActivate() should acquire any needed resources Passivation & activation do not apply to stateless session bean (pool & reuse do not apply to stateful session bean)

12 12 Deployment Descriptor Used by container for runtime setup of EJB: middle-ware services provided according to deployment descriptor Specify Bean management & life-cycle requirements Name of class, stateless/ful, transaction manage Persistence requirement (entity bean only) Transaction requirement Security requirements Resource references/ mapping Error/Exception mapping

13 13 Client side Client can exist in any scenario Stand-alone application Java-based applet Jsp page, servlet Other enterprise bean Client code typically follow these steps: Look up home object by JNDI name Use the home object to create EJBObject Call business method on EJB object Remove EJB object For remote client of j2ee server, need to install j2sdkee on client machine actualy necessary is j2ee.jar and properties file)

14 14 Web Client The classes needed by the client are declared with a JSP page directive ) Because locating the home interface and creating the enterprise bean are performed only once, this code appears in a JSP declaration enclosed within the characters that contains the initialization method, jspInit(), of the JSP page

15 15 Deployment on J2EE server Start up application server -EJB Container (J2EE server) Using deploytool to generate deployment descriptor, verify and deploy your enterprise application Because EJB client code use home and remote interface, you must deploy these classes in client enviroment (but not bean class) Modify.properties file for port setting and database setting resource.properties, orb.properties, web.properties, ejb.properties setenv.bat


Download ppt "1 Session Bean Chuyên đề Lập trình Java & J2EE Chương 14 Biên soạn: Th.S Nguyễn văn Lành."

Similar presentations


Ads by Google