Enterprise JavaBeans Components Overview II

Source: Internet
Author: User

EJB programming Model

The second part of this article explains the Java interfaces and classes needed to create Enterprise JavaBean components. In addition to encoding the Bean class itself, the EJB developer must also define a local interface and a remote interface for the bean. The implementation classes for these interfaces are typically generated by the container, so deploying the EJB component is a collaborative behavior between the developer and the EJB container. The second section also distinguishes between the two main types of the enterprise bean, session beans and entity beans, and describes the relationship between the EJB container and the EJB server.

The three key features of the enterprise Bean's programming model are object-oriented, object-oriented, distributed, and use proxy objects. Because this programming model uses Java technology, it is object-oriented in nature. This model is also distributed, which means that the bean is theoretically transparent in position. According to the Enterprise JavaBeans (EJB) specification, "Generally speaking, the actual location of the EJB class and the EJB container is transparent to the client." "Uses proxy objects when the client wants to access the EJB component. The bean itself is inaccessible to the client, and access to the Bean method is provided by the helper class.

interfaces, delegates, and proxies

When Java programmers write a Enterprise JavaBeans component, the class they create must implement an EJB interface, and it must contain a method named Ejbcreate (). An EJB interface--such as the Sessionbean interface--specifies methods that include the following:

Ejbactivate ()

Ejbpassivate ()

Ejbremove ()

Setsessioncontext ()

The Ejbactivate () and Ejbpassivate () methods notify a bean that the container component that manages the bean is switching between active and passive to switch the state of the bean (this usually refers to in memory or to disk). The Ejbremove () method makes the Bean aware that it has been removed from the container. The Setsessioncontext () method causes the bean to be associated with a context object that facilitates the communication between the bean and its container.

The Ejbcreate () method does not create a enterprise bean from scratch. When the client wants to create a new enterprise bean, the Bean's container invokes the Newinstance () method of the Bean's class to instantiate the new Bean object. The container then invokes the Setsessioncontext () method to establish the context object that is used to communicate with the bean. Finally, the container invokes the Ejbcreate () method in the new bean. Methods such as Ejbcreate (), Ejbactivate (), and ejbpassivate () are sometimes referred to as Object life cycle methods to distinguish them from business logic methods.

When a developer designs a new EJB component, writing the code that makes up the enterprise Bean class is not enough. The EJB programmer 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, both of which are extensions of the Java.rmi.Remote marker interface. An interface that extends the standard Ejbobject interface is called the remote interface of the enterprise bean, which specifies the business methods defined in the bean itself. When an application calls a business method in a 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, called a delegate, is a design point in the EJB architecture:

"The client never accesses an instance of the enterprise bean class directly. The client always uses the remote interface of the enterprise bean to access an instance of the enterprise bean. The class that implements the remote interface of the enterprise bean is provided by the container. The distributed objects implemented by this class are called EJB objects. "(Enterprise JavaBeans Specification 1.0)

The extension of the bean to the Ejbobject interface is called its remote interface, and the object that implements the remote interface is called the EJB object.

The enterprise bean must also have a local interface. This interface is an extension of the standard EJBHome interface. The object that implements the bean's local interface is called a local object. The local object contains a create () method, which is invoked 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 directly create a new enterprise bean instance because direct access to the bean is not allowed.

EJB Objects and Local Objects act as proxies for bean objects because they represent the bean to receive method calls. The EJB object acts primarily as a proxy for the bean business method, and the local object primarily acts as a proxy for the bean lifecycle method.

Using the Create () method for an EJB component does not necessarily instantiate a new bean. The container determines how best to satisfy the creation request, and for some types of beans, it can reuse an existing instance:

The client uses the Create and remove methods on the local interface of the session bean. Although the client thinks it is controlling the lifetime of the EJB instance, it is the container that handles the create and remove calls without necessarily creating and deleting the EJB instance. In the client and ... There is no fixed mapping between instances. The container simply delegates the client's work to a usable instance where any method is ready. "(Enterprise JavaBeans Specification 1.0)

Creating a new instance of the bean is controlled by the container and can be asynchronously published by the client with the Create () method.

When creating an EJB component, the developer is responsible for defining the Ejbobject interface and the EJBHome interface, but does not need to write the code for the class that implements the interfaces. These classes are created automatically by the EJB container software component.

The following code snippet shows how a client application might use a enterprise bean called Cartbean to shop online:

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();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.