The second part of this article describes the role of Java interfaces and classes required to create Enterprise JavaBean components. In addition to encoding the Bean class itself, EJB developers must also define a local interface and a remote interface for beans. The implementation class of these interfaces is typically generated by the container, so the deployment EJB component is a cooperation behavior of developers and EJB containers. The second part also distinguishes two main types of Enterprise Beans, namely session beans, and entity beans, and illustrates the relationship between EJB containers and EJB servers.
The three key features of the Enterprise Bean programming model are: object-oriented, object distributed and use a proxy object. Since this programming model uses Java technology, it is inherently object-oriented. This model is also distributed, refers to Bean theoretically, transparent. According to Enterprise JavaBeans (EJB) specification, "Generally, the actual location of the EJB class and EJB containers is transparent to the client." "When the client wants to access the EJB component, use the proxy object. Bean itself is unacceptable to the client, and access to the bean method is provided by the Helper class.
Interfaces, delegates and proxy When the Java programmer writes an Enterprise JavaBeans component, the classes they have created must implement an EJB interface, and it must contain a method called EJBCREATE (). An EJB interface - such as a sessionBean interface - specifies some methods, including the following:
Ejbactivate () ejbpassivate () ejbremove () setSessionContext ()
The ejbactivate () and EJBPassivate () methods notify a bean that manages the state of the container component that is actively and passive (this is usually referred to in the memory or swap to disk). The ejbremove () method makes the bean know it has been removed from the container. The setSessionContext () method enables the bean associated with a context object, which is for convenience of communicating with its containers.
The EJBCREATE () method is not from zero to create Enterprise Bean. When the client wants to create a new Enterprise Bean, the BEAN's container will call the NewInstance () method of this bean class to instantiate the new bean object. The container then calls the setSessionContext () method to create a context object for communication with beans. Finally, the container calls the ejbcreate () method in the new bean. Methods like ejbcreate (), ejbactivate (), and ejbpassivate () are sometimes referred to as an object survival cycle method to distinguish between business logic methods.
When developers designed a new EJB component, the code written in the ENTERPRISE BEAN class is not enough. EJB programmers must also write two Java interfaces that will be used by the Helper class. These mandatory interfaces must extend the standard EJBOBJECT and EJBHOME interfaces, and the two interfaces are extensions in the Java.rmi.Remote Marker interface. The interface of the extended standard EJBObject interface is called the remote interface of Enterprise Bean, which specifies the business method defined in Bean itself. When the application calls the business method in the Enterprise Bean, the application does not access the Bean itself. In fact, the method call is passed to the object that implements the EJBObject interface extension. This approach is called a delegate, it is a design point in the EJB architecture:
"The client never access the instance of the Enterprise Bean class. The client always uses the Enterprise BEAN's remote interface to access the Enterprise Bean instance. The class that implements the remote interface of the Enterprise Bean is provided by the container. This type implemented distributed object The EJB object. "(Enterprise JavaBeans Specification 1.0) Bean is called its remote interface for the EJBObject interface, and the object that implements the remote interface is called the EJB object.
Enterprise Bean must also have a local interface. This interface is an extension of the standard EJBHOME interface. Objects to implement the local interface of the bean are called local objects. Local objects contain a create () method, this method is called by the application, and the application must create a bean instance. The CREATE () method in the local object creates a new EJB object. It does not create a new Enterprise Bean instance because it is not allowed to access beans directly.
EJB objects and local objects serve as a proxy for bean objects because they call on the bean receiving method. The EJB object is mainly served as a proxy for the Bean business method; the local object is mainly served as a proxy for the BEAN survival cycle method.
Use the create () method for EJB components and do not have to instantiate new beans. The container determines how best to meet the creation request, for some types of beans, it can reuse the existing instance:
"Client uses the Create and Remove method on the local interface of the session bean. Although the client thinks it is controlling the living cycle of the EJB instance, it is a container to process Create and REMOVE calls, not necessarily to create and delete an EJB instance. There is no fixed mapping between clients and ... instances. The container is just a useful instance that the client's work is delegated to any way. "(Enterprise JavaBeans Specification 1.0)
Creating a new bean instance is controlled by the container and can publish the CREATE () method with the client.
When creating an EJB component, the developer is responsible for defining the EJBObject interface and the EJBHOME interface, but does not need to write code of the class of these interfaces. EJB container software components automatically create these classes.
The following code segment explains how the client application may use EnterPrise Bean called CartBean for online shopping:
CartHome cartHome = javax.rmi.PortableRemoteObject.narrow (initialContext.lookup ( "applications / shopping_cart"), CartHome.class); Cart cart = cartHome.create (); cart.addItem (item29); cart.addItem (item67); Cart.additem (item91); cart.purchase (); cart.remove ();
CARTHOME is a class that implements a local interface (extension of the EJBHome interface). CART is a class that implements a remote interface (extension of the EJBObject interface). When the client calls an application method (such as addItem () and purchase ()), they are called on the CART object, and this object then delegates the implementation of these methods to the bean itself. The functionality of Enterprise Bean is obtained by its agent EJB object (ie, Cart). What happens if you visit Cart Bean at the same time? Enterprise Bean developers do not need to write code to support concurrency access. Concurrently supported by EJB container.
The following figure illustrates the relationship between each EJB object: the server and container EJB architecture includes two concepts of EJB servers and EJB containers. The EJB server acts as a component execution system, as described in the EJB White Paper:
"Enterprise JavaBeans Specifications define a standard model for each support fully portable Java application server. Any vendor can use this model to implement support for Enterprise JavaBeans components. A variety of systems (such as TP monitor, CORBA operation When the system, COM runtime system, database system, web server system, or other server-based runtime system can be adjusted to support portable Enterprise JavaBeans components. "(Thomas, Enterprise JavaBeans Technology: Server Component Model for the Java Platform)
The EJB server provides an operating environment for applications using EJB components, and provides all necessary services to support EJB architectures. Packing EJB server software does not pre-specify. One way is to enhance it as a functional enhancement into the application server, which is the method used in IBM WebSphere Application Server, Advanced Edition, Version 2.0.
The EJB component is not directly executed at the top of the EJB server. A intermediate software component called an EJB container runs in an EJB server environment, providing operational environments for these beans themselves. The EJB container is completely transparent to the EJB application, but it plays a key role in supporting Bean operations.
In order to enable Enterprise Beans to act as reusable software components, they cannot have built-in correlations for specific servers or platform functions. Several common types of server-side functions have been "separated from" in the bean design, and the responsibility of this feature is transferred to the container component. For example, the container will be used to take over security, concurrency, transaction, exchange to the auxiliary memory and other services, so that Bean is protected from server-dependent, and will be optimized according to business logic, not by service. Logic is optimized.
EJB white paper describes the role of the container:
"EJB container management is deployed in among the Enterprise Bean. Client applications do not interact directly with Enterprise Bean. In contrast, client applications are through two encapsulation interfaces generated by the container (EJB HOME interface and EJB Object interface) and Enterprise Bean interacts. When the client calls various operations using the package interface, the container intercepts each method call and inserts management services. "(Thomas, Enterprise JavaBeans Technology: Server Component Model for the Java Platform)
It can be desired that EJB container software generally supplies with EJB server software, although these components are allowed to be separated. In addition to providing access to runtime services such as transaction processing and security), it is also desirable that EJB containers include a variety of necessary tools to support Enterprise Beans installation, operation, and management. For example, there is a need to explain the content of the EJB JAR file. There is a tool generated database access to obtain persistence provided by the container, and tool monitoring is running the behavior, and implement security.
Bean style EJB components are divided into two main categories - session beans and entity beans. These categories can also be further subdivided according to the method of processing status, transaction and persistence. Session beans typically have the following properties:
Representing a single client can be a transactionality that can update the data survival in the shared database relatively short. It is usually the service of the client's surplus. Any persistence data is managed by the bean management can be deleted in EJB. When the server fails, the entity bean is typically has the following attributes: the data representing the database is transactional allows multiple users to access multiple users can have a long-term persistence data can continue to survive the EJB specification for session after the EJB server fails. And the instructions of the entity bean are as follows:
"For clients, session Enterprise Beans is an unmusttable object that implements business logic running on the server. It is a way to imagine a session object: the session object is a client program running on the server. Logical extension. The session object is not shared between multiple clients.
"For clients, entity Enterprise Beans is a persistent object that represents an object view that stores entities stored in persistent memory (eg, a database), or an entity implemented by existing enterprise applications." (Enterprise JavaBeans Specification 1.0)
With a rough statement, the session bean represents this operation, it retrieves or stores data to meet user requests; and the entity bean represents a data set, you can access these data sets to meet the user request.
The easiest of the easiest of the session bean is a stateless session bean. Because these beans can distinguish between their state, all instances are identical. The container manages the survival period of the stateless session bean, which is to accommodate the client's workload by creating a sufficient number of such beans and deletes them without them. Passivation, the upcoming bean is written to the disk, not for a stateful session. To call the bean, the client program invokes the Standard Create () method in the local interface, although this operation does not necessarily lead to instance of the new Bean instance. The container can choose to send client requests to an existing object. Conversely, the container can create a new instance according to its choice, and independently of the CREATE () method published by the client.
The create () call released on the EJB local object returns a reference to the EJB object, and this EJB object represents Enterprise Bean. Once the client has an EJB object reference, it can publish the business method to the EJB object, and the container will entrust these methods to the bean itself. The container component responsible for the management session bean does not need to inrerite whether the session bean is stateless. The session bean is stateless or stateful in the installation.
If the session bean reserves status information between method calls, it is stateful. By calling the EJBPassivate () method, the container can passivate the state session bean, or write to the auxiliary memory according to it. The EJB specification does not require containers to use Java serialization protocols while passivating Beans, but they must provide equivalents. When the container decides to exchange a non-active session bean back to the memory, it cancels the serialization of the passive bean and calls the ejbactivate () method. Developers with stateful session beans are responsible for ensuring status data is serialized. Be aware of the status session bean when you implement a status session bean in the cluster's application server environment, because all the servers support the synchronization of the status session bean of the cluster.
Stateful session beans can be transactional. By using the method in the Javax.Transaction.UserTransaction interface, such as Begin (), Commit () and Rollback (), beans can control the transaction; BEAN can receive a notification on transaction status by implementing a Javax.ejb.Sessionsynchronization interface. EJB containers do not need to infer which beans require transaction support; the UserTransaction interface can only be used for Beans that are labeled as transactions when installing. The entity bean entity bean is similar to meeting beans in the architecture, but they provide access to corporate data, not to support user sessions. An entity bean can support multiple concurrent users, while the container makes access and transaction synchronization. Entity Beans also has primary keys for supporting the Finder method in the local object. A client that knows the primary key of entity beans can get an object reference by calling the Findby PrimaryKey () method on the local object. Unlike session beans, the local object of the entity bean has a Finder method except for the Create method.
Persistence is a basic attribute of entity beans. The EJB specification allows two forms of entity persistence: BEAN management persistence and container management persistence. For entity beans in the representative database, Bean's management of persistence means that calls to database access are directly written in the enterprise bean (using JDBC or SQLJ). This method is straightforward, but it reduces portability. The management of containers on persistence means that Beans are not affected by database calls. Inform the container during installation, the persistence of the bean data is installed, while the container is responsible for generating a code that implements persistence. This approach allows the bean to be more portable, even achievable extent to which different data sources can be used. However, this method requires complex functions in the container.
When the entity bean object is associated with the EJB object, the former is in the ready state; otherwise they will be in a shared state. When the client calls the method in the EJB object, the container looks for an instance of the associated entity bean (if present), or transmits an instance from the shared state. Entity beans in ready state can receive a business method call to them by commissioning from the client. They can also perform the EJBLOAD () and EJBSTORE () methods when the container request is requested. The LOAD method and the Store method are intended to maintain the consistency between the entity beans and the underlying data storage.
Entity beans support multiple users and access data. EJB specification statement, maintaining data integrity is the responsibility of the container:
"Enterprise Bean Developers do not need to worry about the concurrent access from multiple transactions when writing business methods .Enterprise bean developers can assume that each entity bean accessed by multiple transactions will ensure proper synchronization Enterprise JavaBeans Specification 1.0)
Container Complete this task is usually used by locking data in the database and enables access to serialization, or by creating a plurality of instances of entity beans, and allows you to manage access.