J2EE--EJB,
I. EJB basics:
EJB is sun's JavaEE server component model. The design goal and core application are to deploy distributed applications. With the advantages of cross-platform java, distributed systems deployed with EJB technology can be not limited to specific platforms. As part of J2EE (javaEE), EJB defines a standard for developing multiple Enterprise applications based on components. It features network service support and core development tools (sdks ). In J2EE, Enterprise Java Beans (ejbs) are called Java Enterprise Beans and are the core code of Java, namely Session Bean and Entity Bean) and MessageDriven Bean ). ------------- From Baidu encyclopedia, let's take a look at the official explanation of EJB.
The core of business software is its business license. The business pipeline abstracts the entire business process and uses computer languages to implement it.
J2EE solves this problem by extracting the business license from the client software and encapsulating it in a component. This component runs on an independent server, and the client software calls the services provided by the component through the network to implement service renewal, the client software is only responsible for sending call requests and displaying processing results. In J2EE, the component that runs on an independent server and encapsulates the business Metrics is the EJB (Enterprise Java Bean) component.
As the saying goes, EJB is: "The classes that need to execute the specific tasks in the software you write are not placed on the client software, instead, pack it into a server ".
Three types of EJB:
1. Session Bean is used to implement business logic. It can be stateful or stateless. Each time a client requests a Session Bean, the container selects a Session Bean to serve the client. Session Bean can directly access the database, but in more cases, it will implement data access through Entity Bean.
2. entity Bean is a domain model object used for O/R ing. It maps table records in the database to Entity objects in the memory. In fact, creating an Entity Bean object is equivalent to creating a new record, deleting an Entity Bean deletes corresponding records from the database at the same time. When an Entity Bean is modified, the container automatically synchronizes the status of the Entity Bean with the database. 3. MessageDriven Bean is a new enterprise Bean introduced in EJB2.0. Based on JMS messages, it can only receive and process JMS messages sent by clients. MDB is actually an asynchronous stateless Session Bean. After the client calls MDB, it does not need to wait and returns immediately. MDB processes client requests asynchronously. This is suitable for scenarios where requests need to be processed asynchronously, such as order processing. This avoids the client from waiting for a method call for a long time until the returned result is returned.
Ii. EJB implementation principle: EJB is a component running on an independent server, and the client calls the EJB object through the network. In Java, RMI is the technology that can realize remote object calling, while RMI is the basis of EJB technology. Through RMI technology, J2EE creates the EJB component as a remote object, and the client can call the EJB object over the network.
The location of EJB in the system:
Iii. Use the EJB Running Mechanism: on the server side: After the EJB component is deployed to the container, the container automatically generates three objects. The three objects are: home object, Remote object, Local object, and Enterprise Bean object. Client call process: bind and locate ejbs and Context using the JNDI mechanism. lookup is used to retrieve the Home object, and then create a Remote object using the create method of the Home interface; call the Business Method in the Remote interface; call the remove method after using EJB to delete the object. As follows:
(1) It is found that the EJB client needs to call the local interface of ejb, so it needs to obtain remote reference of the local interface. In the J2EE environment, this type of Naming Service can be accessed through JNDI, that is, you can use JNDI to obtain the reference of the local EJB interface, because the EJB container registers the local interface with the JNDI name specified in the deployment. (You can set the JNDI name when it is an EJB member, and the client will find this EJB through the JNDI name.) Try {InitialContext ic = new InitialContext (); Object lookup = ic. lookup ("java: comp/env/ejb/Agency"); AgencyHome home = (AgencyHome) PortableRemoteObject. narrow (lookup, AgencyHome. class);} (2) use Try {Agency agency = home. create (); // return a remote reference to the newly created EJB. System. out. println (agency. getAgencyName ();} (3) Delete Agency = null when the local RMI allows remote object detection that there is no local reference, a remote garbage bin is triggered for this object, this means that the remote reference times out, which causes the server to reference the object again and then destroy the bean.
Summary: The implementation principle of EJB is to place the code originally implemented on the client to the server and communicate with each other through RMI.