(1) Overview
1. Enterprise JavaBean (EJB) is a solution for Sun Microsystems's portability and complexity of CORBA. EJB introduces a simpler programming module than CORBA, which allows developers to create portable distributed components calledEnterprise Bean. The EJB programming module allows developers to create secure, transactional, and persistent Business Objects (Enterprise beans). This object uses very simple programming modules and declarative attributes. Unlike CORBA, for example, access control (authorization security) and transaction management are very easy to program. CORBA needs to use complex APIs to use these services, while EJB automatically applies these services to enterprise beans based on the declaration in a feature file called "deployment description. This model ensures that bean developers can concentrate on writing business logic, and containers automatically manage more complex but necessary operations.
The EJB specification has issued a set of clear contracts between the EJB container (supplier server) and the EJB component (Commercial object), so the EJB has achieved portability. These contracts or rules specify that the container must be
2. What services does enterprise Bean provide? What APIs and declared attributes are required by bean developers to create enterprise beans. Because the lifecycle of enterprise Bean is specified in detail, the supplier knows how to manage beans at runtime, and the bean developer knows exactly what enterprise Bean can do during its existence.
3. Enterprise JavaBean simplifies the development, deployment, and access of distributed objects. Developers of EJB distributed objects (an enterprise Bean) only need to implement objects according to the contract and Protocol established for Enterprise JavaBean. The application server that supports EJB can, or indeed, use any distributed network protocol, including the Local Java RMI protocol (jrmp), the proprietary protocol, or the network protocol (IIOP) of CORBA ). Regardless of the basic network protocol used in a particular product, EJB uses the same programming API and semantics to access distributed objects in a Java RMI-IIOP. The details of the Protocol are hidden from the application and bean developers. For all vendors, the method for locating and using distributed beans is the same.
Note:Enterprise Bean is different from Javabean. Javabean is usedjava.beans
Package development, which is part of Java 2 Standard Edition. Javabean is a component running in the same address space on a machine. Javabean is a processInternal Component. Enterprise Bean is usedjavax.ejb
It is a standard JDK extension and is part of Java 2 Enterprise Edition. Enterprise Bean is a component that runs across several address spaces on multiple machines. Therefore, enterprise Bean is a process.RoomComponent. Java Beans are usually used as GUI window widgets, while enterprise beans are used as distributed business objects.
(2) EJB specifications
The Enterprise JavaBean Specification defines an architecture for Component-Based transactional and distributed object systems. This specification has issued a programming module, that is, the contract or protocol that forms the ejb api and a group of classes and interfaces. The EJB programming module provides bean developers and EJB server vendors with a set of contracts, which define the public platform for development. These contracts aim to ensure portability between suppliers while supporting a set of rich functions.
Enterprise Bean depends on the container to obtain its needs. If enterprise Bean needs to access JDBC connections or another enterprise Bean, it needs to use containers to perform this operation. If enterprise Bean needs to access the identity of the caller and obtain its own reference or access features, then it needs to use containers to complete these operations. Enterprise Bean interacts with containers through one of the following three mechanisms: callback method,EJBContext
Interface or JNDI.
- Callback method:
Every bean will implementEnterpriseBean
The subtype of the API. This API defines some methods called callback methods. Each callback method prompts a different event to the bean during its lifecycle, when the container wants to use a bean, store its status to the database, end the transaction, remove the bean from the memory, and other operations, it will call these methods to notify the bean. The callback method allows the bean to execute internal adjustments immediately before or after the event.
- Ejbcontext:
Every bean will getEJBContext
Object, which is a direct reference to the container.EJBContext
The interface provides a method for interacting with the container. Therefore, the bean can request information about the environment, such as the client's identity or transaction status, or the bean can obtain its own remote reference.
- Java Naming and Directory Interface (JNDI ):
JNDI is a standard extension of the Java platform for accessing naming systems, such as LDAP, Netware, and file systems. Each bean automatically has access to a specific naming system (called the environment naming context (ENC. The ENC is managed by the container, and the bean uses JNDI to access the ENC. Jndi enc allows beans to access resources, such as JDBC connections, other enterprise beans, and attributes specific to the bean.
Remote and local interfaces
Remote and local interfaces indicate beans, but containers do not allow direct access to beans from client applications. The container manages the entire process every time a bean is requested, created, or deleted.
Local interface: indicates the life cycle (creation, destruction, and search) of a component ).
Remote interface: the commercial method of bean.
Extended remote and local interfacesjavax.ejb.EJBObject
Andjavax.ejb.EJBHome
Interface. These EJB interface types define a set of standard utility methods and provide common basic types for all remote and local interfaces.
Remote interfaces define bean business methods, which are specific to bean business concepts. The remote interface is fromjavax.ejb.EJBObject
The subclass of the interface, and this interface isjava.rmi.Remote
The subclass of the API.
Import javax. EJB. ejbobject;
Import java. RMI. RemoteException;
Public interface customer extends ejbobject {
Public name getname () throws RemoteException;
Public void setname (name) throws RemoteException;
Public Address getaddress () throws RemoteException;
Public void setaddress (Address) throws RemoteException;
}
Remote interfaces define read and write methods to read and update information about business concepts. This represents a bean called an Entity Bean, which represents a persistent commercial object (a commercial object of data stored in a database ). Entity Bean indicatesBusiness DataAnd add actions specific to the data.
Business Method
Commercial methods can also represent bean executionTask. Although entity beans usually have task-oriented methods, tasks usually represent another Bean called Session Bean. The session bean does not represent data similar to the object bean. They indicate the commercial process or agent that executes the service (such as booking a room in a hotel. The following is the definition of the remote interface of javasclerbean. This bean is a session bean.
Import javax. EJB. ejbobject;
Import java. RMI. RemoteException;
Public interface extends clerk extends ejbobject {
Public void reserveroom (customer Cust, roominfo Ri,
Date from, date)
Throws RemoteException;
Public roominfo availablerooms (location Loc, date from, date)
Throws RemoteException;
}
HotelClerk
The commercial method defined in the remote interface represents a process rather than a simple read method. Hotelclerbean acts as a proxy because it represents a user performing a task, but it itself is not persistent in the database. You do not need information about the clerk; you need the clerk to perform the task for you. This is a typical behavior of Session Bean.
There are two basic enterprise beans: the entity bean, which represents data in the database, and the session bean, which represents the process or acts as the agent for executing the task. When building an EJB application, you will create many enterprise beans, each representing different business concepts. Every commercial concept is represented by an entity bean or Session Bean. You will choose which bean it will become based on how you use the commercial concept.