1 / 24

Introduction to EJB

Introduction to EJB. What is an EJB ?. An enterprise java bean is a server-side component that encapsulates the business logic of an application . By invoking the exposed methods, remote clients can access the services provided by the application. When to use EJB ?.

fosterl
Download Presentation

Introduction to EJB

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to EJB

  2. What is an EJB ? An enterprise java bean is a server-side component that encapsulates the business logic of an application. By invoking the exposed methods, remote clients can access the services provided by the application.

  3. When to use EJB ? • The application must be scalable.To accommodate a growing number of users, you may need to transparently distribute an application's components across multiple machines. • Transactions are required to ensure data integrity.Enterprise beans support transactions, the mechanisms that manage the concurrent access of shared objects. • The application will have a variety of clients.With just a few lines of code, remote clients can easily locate enterprise beans. These clients can be thin, various, and numerous.

  4. What is J2EE? The JavaTM 2 Platform, Enterprise Edition (J2EETM) is an integrated platform which allows to build solutions for multi-tier, enterprise applications.It extends the cross-platform portability of Enterprise JavaBeansTM (EJBTM) technology by providing a foundation of common services and APIs, including XML, JavaServer PagesTM technology, and Servlets.The Java 2 SDK, Enterprise Edition (J2EE SDK) is the reference implementation for the J2EETM.

  5. Architectural view

  6. Types of EJB

  7. DB A more detailed architectural view CMP JDBC Image taken from “Mastering EJ2B”

  8. EJB ingredients • Interfaces: The remote and home interfaces are required for remote access. For local access, the local and local home interfaces are required. • Enterprise bean class:Implements the methods defined in the interfaces. • Helper classes: Other classes needed by the enterprise bean class, such as exception and utility classes.

  9. EJB ingredients • Deployment descriptor: An XML file that specifies information about the bean such as its persistence type and transaction attributes. • You package the files in the preceding list into an EJB JAR file, the module that stores the enterprise bean. • To assemble a J2EE application, you package one or more modules--such as EJB JAR files--into an EAR file, the archive file that holds the application.

  10. 1 - Write the beans 3- Write the client 2 -Write the descriptor Architectural view

  11. Entity Beans An entity bean represents a business object in a persistent storage mechanism. Some examples of business objects are customers, orders, and products. The bean represents a business entity, not a procedure. For example, CreditCardEJB would be an entity bean, but CreditCardVerifierEJB would be a session bean. The bean's state must be persistent. If the bean instance terminates or if the J2EE server is shut down or crashes, the bean's state still exists in persistent storage (a database).

  12. Entity Beans: PERSISTENCE Persistence means that the entity bean's state exists beyond the lifetime of the application or the J2EE server process. There are two types of persistence for entity beans: bean-managed and container-managed. With bean-managed persistence (BMP), the entity bean code that you write contains the calls that access the database. If your bean has container-managed persistence (CMP), the EJB container automatically generates the necessary database access calls. The code that you write for the entity bean does not include these calls.

  13. Entity Beans: SHARED ACCESS Entity beans may be shared by multiple clients. Because the clients might want to change the same data, it's important that entity beans work within transactions. Typically, the EJB container provides transaction management. In this case, you specify the transaction attributes in the bean's deployment descriptor. You do not have to code the transaction boundaries in the bean--the container marks the boundaries for you.

  14. Entity Beans: DB-like features • Like in a relational database: • Each entity bean has a unique object identifier; • An entity bean may be related to other entity beans. • The unique identifier, or primary key, enables the client to locate a particular entity bean. • You implement relationships differently for entity beans with BMP and those with CMP: • BMP: the code that you write implements the relationships. • CMP: the EJB container takes care of the relationships for you. (container-managed relationships).

  15. Session Beans A session bean represents a single client inside the J2EE server. To access an application that is deployed on the server, the client invokes the session bean's methods. The session bean performs work for its client, shielding the client from complexity by executing business tasks inside the server. At any given time, only one client has access to the bean instance. The state (i.e. the values of the instance variables) of the bean is not persistent, existing only for a short period of time. A session bean can be stateful or stateless.

  16. Stateful session Beans In a stateful session bean, the instance variables represent the state of a unique client-bean session. This state is often called the conversational state. The state is retained for the duration of the client-bean session. If the client removes the bean or terminates, the session ends and the state disappears.

  17. Stateless session Beans A stateless session bean does not maintain a conversational state for a particular client. When a client invokes the method of a stateless bean, the bean's instance variables may contain a state, but only for the duration of the invocation. When the method is finished, the state is no longer retained.

  18. Stateless vs. stateful session Beans All instances of a stateless bean are equivalent, allowing the EJB container to assign an instance to any client. => Stateless session beans can support multiple clients, and offer better scalability for applications that require large numbers of clients. Typically, an application requires fewer stateless session beans than stateful session beans to support the same number of clients.

  19. Stateless vs. stateful session Beans The EJB container can write a stateful session bean to secondary storage. However, stateless session beans are never written to secondary storage. Therefore, stateless beans may offer better performance than stateful beans.

  20. Stateful session Beans – examples of use • The bean needs to hold information about the client across method invocations. • The bean mediates between the client and the other components of the application, presenting a simplified view to the client. • Behind the scenes, the bean manages the work flow of several enterprise beans.

  21. Stateless session Beans – examples of use • In a single method invocation, the bean performs a generic task for all clients. For example, you might use a stateless session bean to send an e-mail that confirms an online order. • The bean fetches from a database a set of read-only data that is often used by clients.

  22. Message Driven Beans A message-driven bean is an enterprise bean that allows J2EE applications to process messages asynchronously. It acts as a JMS message listener, which is similar to an event listener except that it receives messages instead of events. The messages may be sent by any J2EE component--an application client, another enterprise bean, or a Web component--or by a JMS application or system that does not use J2EE technology.

  23. Message Driven Beans A message-driven bean's instances retain no data or conversational state for a specific client. All instances of a message-driven bean are equivalent, allowing the EJB container to assign a message to any message-driven bean instance. The container can pool these instances to allow streams of messages to be processed concurrently. A single message-driven bean can process messages from multiple clients.

  24. KEY CONCEPTS • Entity beans: synchronous, shared access, concurrency-safe • CMP • BMP • Session Beans: synchronous • Stateful single access, conversational state • Stateless sharable, no state • Message-Drive Beans: asynchronous, sharable, no state

More Related