What is the Enterprise JavaBeans component? Part 2: EJB programming model

Source: Internet
Author: User
Tags websphere application server

Part 2: EJB programming model

Document options

<Tr valign = "TOP"> <TD width = "8"> </TD> <TD width = "16"> </ TD> <TD class = "small" width = "122"> <p> <SPAN class = "Ast"> the required JavaScript document options are not displayed </span> </P> </TD> </tr>


Send this page as an email


Level: elementary

Ken Nordby, software engineer, IBM

June 20, 2000

The second part describes the functions of Java interfaces and classes required to create Enterprise JavaBean components. In addition to coding the Bean class, EJB developers must define a local interface and a remote interface for the bean. The implementation classes of these interfaces are usually generated by containers. Therefore, deploying EJB components is a cooperative action between developers and EJB containers. The second part also distinguishes two main types of enterprise Bean, namely Session Bean and Entity Bean, and illustrates the relationship between the EJB container and the EJB server.

Three key features of the enterprise Bean programming model are object-oriented and distributed objects and the use of proxy objects. Because this programming model uses Java technology, it is essentially object-oriented. This model is also distributed, which means that the bean is theoretically transparent. According to the Enterprise JavaBeans (EJB) specification, "in general, the actual location of EJB classes and EJB containers is transparent to the client ." Use a proxy object when the client wants to access the EJB component. Bean itself is inaccessible to clients, and access to bean methods is provided by the helper class.

Interface, delegate, and proxy

When Java programmers compile an Enterprise JavaBeans component, the class they create must implement an EJB interface, and it must containejbCreate(). An EJB interface, such as the sessionbean interface, specifies some methods, including the following:

  • ejbActivate()
  • ejbPassivate()
  • ejbRemove()
  • setSessionContext()

ejbActivate()AndejbPassivate()Method to notify a bean that the container component managing the bean is switching the bean status between active and passive (usually in memory or switching to disk ).ejbRemove()Method To make the bean know that it has been deleted from the container.setSessionContext()Method to associate bean with a context object. This context object is used to facilitate bean communication with its container.

ejbCreate()The method does not start from scratch to create an enterprise Bean. When the client wants to create a new enterprise Bean, the bean container will callnewInstance()Method to create a new bean object. Then the container callssetSessionContext()Method To establish a context object for communication with bean. Finally, the container callsejbCreate()Method. ImageejbCreate(),ejbActivate()AndejbPassivate()This method is sometimes calledObject LifecycleMethod to distinguishBusiness LogicMethod.

When developers design 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 be extended to the standard ejbobject and ejbhome interfaces, both of which arejava.rmi.RemoteThe extension of the marker interface. Extended StandardEJBObjectThe interface is called the enterprise BeanRemote InterfaceIt specifies the business method defined in bean itself. When an application calls the Business Method in enterprise Bean, the application does not access the bean itself. Actually, the method call is passed to the implementationEJBObjectThe object of the interface extension. This is calledDelegateIt is a key design point in the EJB architecture:

"The client never directly accesses an enterprise Bean instance. The client always uses the remote interface of enterprise Bean to access the instance of enterprise Bean. The class that implements the remote interface of enterprise Bean is provided by the container. The Distributed Object implemented by this class is calledEJB object." (Enterprise JavaBeans specification1.0)

Bean's extension to the ejbobject interface is called itsRemote InterfaceThe object that implements the remote interface is calledEJB object.

Enterprise Bean must also have local interfaces. This interface is an extension of the standard ejbhome interface. The object that implements the bean Local interface is calledLocal object. The local object containscreate()Method, which is called by the application, and the application must create a bean instance. In the local objectcreate()Method to create a new EJB object. It does not directly create a new enterprise Bean instance because it does not allow direct access to beans.

EJB objects and local Objects Act as proxies for bean objects because they represent bean receiving method calls. EJB Objects Act as proxies for bean business methods, while local Objects Act as proxies for bean lifecycle methods.

Used for EJB componentscreate()The method does not have to be used to create new beans. The container determines how to best meet the creation request. For some types of beans, it can reuse existing instances:

"The client usesCreateAndRemoveMethod. Although the client assumes that it is controlling the lifecycle of the EJB instanceCreateAndRemoveInstead of creating or deleting an EJB instance. There is no fixed ing between the client and the... instance. The container only delegates the work of the customer's machine to an available instance ready for any method ." (Enterprise JavaBeans Specification 1.0)

Creating a new bean instance is controlled by the container and can be released with the client.create()The method is asynchronous.

When creating an EJB component, developers are responsible for defining the ejbobject interface and ejbhome interface, but do not need to write code for classes that implement these interfaces. The EJB container software component automatically creates these classes.

The following code snippet describes how a client application may be calledCartBeanEnterprise Bean 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();

CartHomeIs the class that implements the local interface (the extension of the ejbhome interface ).CartIs the class that implements the remote interface (the extension of the ejbobject interface ). When the client calls an application method (suchaddItem()Andpurchase()), They are called on the cart object, which then delegates the execution of these methods to the bean itself. The function of enterprise Bean is obtained through its proxy EJB object (that is, cart. If multiple clients access the cart bean at the same time, what will happen? Enterprise Bean developers do not need to write code to support concurrent access. Concurrency is supported by EJB containers.

Describes the relationship between various EJB objects:



Back to Top

Servers and containers

The EJB architecture includes two concepts: EJB server and EJB container. The EJB server acts as a component execution system, as described in the EJB White Paper:

"The Enterprise JavaBeans Specification defines a standard model for every Java application server that supports full portability. Any vendor can use this model to support Enterprise JavaBeans components. Multiple systems (such as TP monitor, CORBA runtime system, com runtime system, database system, web server system, or other server-based runtime system) can be adjusted to the Enterprise JavaBeans component that can be moved." (Thomas,Enterprise JavaBeans technology: Server Component Model for the javaplatform)

The EJB server provides an operating environment for applications that use EJB components and provides all necessary services to support the EJB architecture. There is no pre-defined method for packaging EJB server software. One way is to include it as a feature enhancement to 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. An intermediate software component called an EJB container runs in the EJB server environment and provides the operating environment for these beans themselves. The EJB container is completely transparent to the EJB application, but plays a key role in supporting bean operations.

To enable enterprise beans to act as reusable software components, they have no built-in relevance for specific servers or platform functions. Several common types of server functions have been "separated" from bean design, and the responsibility for this function has been transferred to container components. For example, the container will be used to take over the responsibilities of Security, concurrency, transaction processing, and switching to the secondary storage and other services, so as to protect the bean from the constraints of server relevance, it will be optimized based on the business logic, rather than the service logic.

The EJB White Paper describes the role of containers as follows:

"Enterprise beans deployed in EJB container management. The client application does not directly interact with the enterprise Bean. On the contrary, the client application interacts with the enterprise Bean through two encapsulated interfaces (EJB home interface and EJB object interface) generated by the container. When the client uses an encapsulated interface to call various operations, the container intercepts each method call and inserts it into the management service ." (Thomas,Enterprise JavaBeans technology: Server Component Model for the Java platform)

It is expected that the EJB container software is generally provided along with the EJB server software, although the specification allows the separation of these components. In addition to providing access to runtime services (such as transaction processing and security), it is expected that the EJB container includes various necessary tools to support enterprise Bean installation, operations, and management. For example, you need tools to explain the content of the ejb jar file, tools to generate database access to obtain the persistence provided by the container, tools to monitor the behavior of running beans, and implement security.



Back to Top

Bean Style

EJB components are divided into two main categories --Session BeanAndEntity Bean. The categories can be further subdivided Based on the bean processing status, transaction, and persistence methods. Session beans generally have the following attributes:

  • Execute on behalf of a single client
  • It can be transactional.
  • Allows you to update data in a shared database.
  • Relatively short lifetime
  • The lifetime is usually the client's lifetime.
  • Any persistent data is managed by bean
  • Can be deleted based on container judgment
  • Will be deleted when the EJB server fails.

Entity beans generally have the following attributes:

  • Represents the data in the database
  • Is transactional
  • Allow multiple users to access
  • Can exist for a long time
  • Persistent data can be managed by containers
  • Continue to survive when the EJB server fails

The EJB specification describes the Session Bean and Entity Bean as follows:

"For clients, session enterprise Bean is a non-persistent object that implements some business logic running on the server. One way to imagine a session object is that the session object is a logical extension of the client program running on the server. Session objects are not shared among multiple clients.

"For clients, the entity enterprise Bean is a persistent object that represents an object view of an object stored in persistent storage (for example, a database, or an entity implemented by an existing enterprise application." (Enterprise JavaBeans Specification 1.0)

In a rough way,Session Bean represents an operation that retrieves or stores data to meet user requests. Entity Bean represents a dataset that can be accessed to meet user requests.



Back to Top

Session Bean

The simplest Enterprise JavaBeans component isStateless Session Bean. Because these beans cannot differentiate their statuses, all instances are identical. Container manages the lifecycle of stateless session beans by creating enough of these beans to adapt to the client workload and delete them when they are not needed.PassivationThe idle bean is written to the disk and is not used for stateless sessions. To call the bean, the client callsstandard create()Method, although this operation may not necessarily lead to the creation of a new bean instance. The container can send client requests to existing objects. On the other hand, the container can create a new instance based on its selection, and is independent ofcreate()Method.

Released on the local EJB objectcreate()The call returns a reference to the EJB object, which represents the enterprise Bean. Once the client has an EJB object reference, it can publish the business methods to the EJB object, and the container will then delegate these methods to the bean itself. The container component responsible for managing session beans does not need to infer whether the session bean is stateless. Whether the session bean is stateless or stateful is declared during installation.

If the Session Bean retains the status information between method calls, it isStateful. By callingejbPassivate()Method, the container can deactivate the stateful Session Bean based on its judgment, or write it to the secondary memory. The EJB specification does not require containers to use the Java serialization protocol when activating beans, but they must provide equivalent functionality. When the container decides to switch an inactive Session Bean back to the memory, it will cancel the serialization of the passive bean and callejbActivate()Method. Developers with stateful session beans are responsible for ensuring that status data is serializable. Be careful when implementing the existing State session bean in the Cluster's application server environment, because not all servers support the synchronization of state session beans in the cluster.

Stateful session beans can be transactional. Usejavax.transaction.UserTransactionMethods In the interface, suchbegin(),commit()Androllback()Bean can control transactions.javax.ejb.SessionSynchronizationThe bean can receive notifications about the transaction status. The EJB container does not need to infer which beans need transaction support;UserTransactionThe interface can only be used for beans marked as transactional during installation.



Back to Top

Entity Bean

Entity beans are similar in architecture to session beans, but they provide access to enterprise data rather than support user sessions. One entity bean supports multiple concurrent users, while the container synchronizes access and transactions. The Entity Bean also has a primary key that supports the finder method in the local object. Clients that know the primary key of the object bean can callfindBy PrimaryKey()Method To obtain object references. Unlike Session Bean, the local object of the object bean has the create method and the finder method.

Persistence is a basic attribute of the object bean. The EJB specification allows two forms of entity Persistence: persistence of bean management and persistence of container management. For entity beans that represent data in relational databases, Bean's persistent management means that database access calls are directly written in enterprise Bean methods (using JDBC or sqlj ). This method is straightforward, but it reduces portability. Container persistence management means bean is not affected by database calls. Inform the container about the persistence required by bean data during installation, and the container is responsible for generating code to implement persistence. This method allows the bean to be more portable, or even to the extent that durability can use different data sources. However, this method requires complicated functions in the container.

When the object bean object is associated with the EJB object, the former isReadyStatus; otherwise they will be considered inShareStatus. When the client calls the method in the EJB object, the container searches for the instance of the associated entity bean (if any), or sends an instance from the shared state. Entity beans in the ready state can receive the business method calls that are distributed to them from the client through delegation. They can also be executed in container requests.ejbLoad()AndejbStore()Method. The load and store methods are designed to maintain data consistency between the object bean and the basic data storage.

Entity Bean supports concurrent data access by multiple users. The EJB specification declares that it is the container's responsibility to maintain data integrity:

"Enterprise Bean developers do not have to worry about concurrent access from multiple tasks when writing business methods. When writing methods, enterprise Bean developers can assume that appropriate same-step operations can be ensured for each entity bean simultaneously accessed by multiple tasks ." (Enterprise JavaBeans Specification 1.0)

The container usually locks the data in the database and serializes the access, or creates multiple instances of the Entity Bean and allows concurrent control in the basic data storage, in this way, access is managed.



Back to Top

Part 3 Content preview

"What is the Enterprise JavaBeans component ?" The third part will discuss the special deployment process for installing the EJB component. It will also show whether the client is a competitor of the EJB component (the answer is "no"-see how the EJB technology supplements the client ). Finally, you will see the usage of an EJB-based three-layer programming model.

References

  • For more information about the EJB architecture, see Anne Thomas'sEnterprise JavaBeans technology: Server Component Model for the Java platform, Patricia Seybold group.

  • For more information about Java and EJB architecture, visit Sun's website.

About the author

 

Ken Nordby is a software engineer at the IBM software development lab in Research Triangle Park, North Carolina Lina. As a member of the SWG product affinity services business team, Ken works with IBM to develop and consult IBM WebSphere Application Server (IBM Implementation of Enterprise JavaBeans technology. You can contact the Ken through the nordby@us.ibm.com.

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.