in"
EJB Components: In Basic Structure Analysis, we have described the HOME interface, Remote interface, and EJB component classes of EJB components. Today, we will analyze the application and classification of EJB session components.
Session component application method
Session Type EJB Components is a client application creates and serves the client-only component type through the HOME interface of the EJB component, and the session component object instance created in the EJB server is not shared with other client applications. A session component often implements a business process, a simple computing process or logical process, and the client's session role does not change during the application.
For example, in an e-commerce application of online shopping, the customer creates a shopping cart before browsing shopping, and the function of this shopping cart can be done by a session type component. Customers in the shopping process of the shopping cart are customers who create shopping carts, other customers cannot add goods to shopping carts that do not belong to their own shopping carts. After the customer's checkout completes a shopping, a session component that plays a shopping cart role is removed from the instance pool of the EJB container, and the session process is ended.
EJB components of the session type usually have two types of applications in distributed multi-layer applications:
Application method in three-layer architecture
In a distributed application of a conventional three-layer architecture, the system's display logic is deployed in the client by encapsulating code of the business logic to the EJB component deployed in the EJB server, making the entire system structure clearer, as shown below Indicated.
Figure 1 Layer 3 Structure Distributed Application Session Type EJB Component Deployment Map
A plurality of EJB component objects are deployed in the EJB server shown in the above figure. For session type EJB components, its main role is to make business logic processing based on service requests proposed by the client application and interact with the deployment of the database. The interaction of the session type EJB component and the database is implemented by Java language database access methods such as JDBC, rather than the component itself as the entity type EJB component is a record in the database table.
Application mode based on web distributed applications
The session component is based on a web-based distributed application, and the client browser uses the HTTP protocol to interact with the WEB components deployed in the web server in a Web server. The dynamically generated code of Hypertext pages such as JSP or Servlet is encapsulated in the web component to dynamically create the page content displayed in the client browser according to the processing of business logic. The session type EJB component is based on the web application as shown below:
Figure 2 WEB application mode EJB component deployment diagram
In web-based distributed applications, the code of the business logic processing section can be written directly as the code of the web component in the web server. However, the business rules code package into EJB components such that different types of client applications can share components objects deployed in the EJB server, improve the reusability and portability of the code.
The inheritance relationship of the session component session type EJB component is defined to implement the sessionBean interface object. The definition of this interface is shown in the following code:
Public Interface Javax.ejb.SessionBean Extends Javax.ejb.EnterpriseBean
{
Public void ejbactivate () throws remoteException;
Public void ejbpassivate () throws remoteexception;
Public void ejbremove () throws remoteException;
Public void setsessionContext (sessioncontext context)
Throws RemoteException;
}
In the above SessionBean interface definition, the EnterpriseBean interface is an empty interface definition, as a parent interface common to the session type EJB component and the entity type EJB component. The sessionBean interface inherits the EnterpriseBean interface.
Conversation type EJB component activation and close
In order to improve the work efficiency of the EJB server, reduce the consumption of the EJB component to server resources, the EJB specification defines the activation and closing mechanism of the session type EJB component, and is used to dynamically adjust the number of active EJB component instance objects in the component instance pool of the EJB container. status. The method of adjusting the component instance status of the EJB server is: a session type EJB component is in an inactive state in an inactive state at the properties of the EJB container. This time attribute is set by the deployers of the EJB component based on the number of components of the application system and the performance of the EJB server. When an EJB component object instance is in an inactive state, the EJB container copies this EJB component instance into the secondary storage device and removes the component instance from the instance pool, and releases this EJB component to occupy and Reference system resources. If a method in the EJB component is part of the transaction is running, the activation state of the EJB component instance cannot be turned off. When the client application calls the method in the session type EJB component, if the EJB component instance is turned off, the EJB container reactivates the component object, and an instance of the component is created in the EJB container is used to respond to the client's call request. Since the stateless session type EJB component does not maintain any client status, there is no need to save the client information contained in the component, and the EJB container can directly reach the EJB component that reaches the non-active state time value from the EJB container. Since these state conversion is completed by the EJB server, the client does not feel the status of the EJB component instance. Accordingly, the EJBActivate method and the EJBPassivate method are defined in the SessionBean interface, and the method of calling the EJB component is converted for the EJB container. Thus, the method of controlling the EJB component state defined in the sessionBean interface can control the number and state of the EJB component instance, greatly improve the work efficiency of the EJB server, reduce the consumption of the EJB component to server-side resources. Classification of session type EJB components
Depending on whether the client is maintained in the state in the EJB server, session type EJB components can be divided into stateful and stateless. There is a corresponding relationship between the stateful session type EJB component has a status session type EJB component instance and a client application that creates EJB components, and the EJB container proxy client applies to the call to the method of state session type EJB component instance. The correspondence between the client application and the stateful session type EJB component instance in the EJB container is shown in the following figure:
Figure 3 corresponds to the correspondence between the client application and the stateful session type EJB component instance
As can be seen from the above figure: the client creates an EJB component object instance gets the EJBObject type object. When the client calls the business method in the component object instance, the EJB container is interactively interacts with the RMI type object instance of the component as the remote call proxy and the state of the state session type EJB component instance in the instance pool, thereby calling the method defined in the EJB component. . For each stateful session type EJB component defined in the EJB container, the EJB container creates a remote object of the RMI type as a proxy between the EJB component and the client. Status Session Type EJB Components Status Session Type EJB Components Do not maintain status information of any client during the method of the client call. EJB containers maintain all created stateless session type EJB components instances in a component instance pool, and the client can be done by the same EJB component object instance for the calling process of the same EJB component. The correspondence between the client application and the stateless session type EJB component object instance in the EJB container is shown in the following figure:
Figure 4 corresponds to the correspondence between the client application and the state of state of the state