Comments
Description
Transcript
V Corso Sodalia - Marco Ronchetti
Marco Ronchetti - [email protected] “Basi di Dati Web e Distribuite” – Laurea Specialistica in Informatica – Università di Trento J0 1 Introduction to EJB “Basi di Dati Web e Distribuite” – Laurea Specialistica in Informatica – Università di Trento Marco Ronchetti - [email protected] What is an EJB ? J0 2 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. “Basi di Dati Web e Distribuite” – Laurea Specialistica in Informatica – Università di Trento Marco Ronchetti - [email protected] Architectural view J0 3 “Basi di Dati Web e Distribuite” – Laurea Specialistica in Informatica – Università di Trento Marco Ronchetti - [email protected] Simplify the development of large distributed apps J0 4 The EJB container provides system-level services to enterprise beans. EASIER DEVELOPMENT => The EJB container--not the bean developer--is responsible for services such as transaction management and security authorization. “Basi di Dati Web e Distribuite” – Laurea Specialistica in Informatica – Università di Trento Simplify the development of large distributed apps Marco Ronchetti - [email protected] Separation of responsability among tiers: J0 5 EASIER DEVELOPMENT => the bean developer can concentrate on solving business problems. EASIER DEVELOPMENT => the client developer can focus on the presentation of the client business rules or access databases. BETTER PRODUCTS => The clients are thinner, a benefit that is particularly important for clients that run on small devices. “Basi di Dati Web e Distribuite” – Laurea Specialistica in Informatica – Università di Trento Marco Ronchetti - [email protected] Simplify the development of large distributed apps J0 6 Enterprise beans are portable components: REUSABILITY => application assembler can build new applications from existing beans. PORTABILITY => These applications can run on any compliant J2EE server. “Basi di Dati Web e Distribuite” – Laurea Specialistica in Informatica – Università di Trento Architectural view Marco Ronchetti - [email protected] 1 - Write the beans J0 7 3- Write the client 2 -Write the descriptor “Basi di Dati Web e Distribuite” – Laurea Specialistica in Informatica – Università di Trento Marco Ronchetti - [email protected] EJB ingredients J0 8 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. “Basi di Dati Web e Distribuite” – Laurea Specialistica in Informatica – Università di Trento Marco Ronchetti - [email protected] EJB ingredients J0 9 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. “Basi di Dati Web e Distribuite” – Laurea Specialistica in Informatica – Università di Trento When to use EJB ? Marco Ronchetti - [email protected] •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 easily locate enterprise beans. These clients can be thin, various, and numerous. J0 10 can “Basi di Dati Web e Distribuite” – Laurea Specialistica in Informatica – Università di Trento Marco Ronchetti - [email protected] Types of EJB J0 11 Enterprise Bean Type Purpose Session Performs a task for a client Entity Represents a business entity object that exists in persistent storage Message-Driven Acts as a listener for the Java Message Service API, processing messages asynchronously “Basi di Dati Web e Distribuite” – Laurea Specialistica in Informatica – Università di Trento Marco Ronchetti - [email protected] Entity Beans J0 12 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). “Basi di Dati Web e Distribuite” – Laurea Specialistica in Informatica – Università di Trento Marco Ronchetti - [email protected] Session Beans J0 13 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. “Basi di Dati Web e Distribuite” – Laurea Specialistica in Informatica – Università di Trento Marco Ronchetti - [email protected] Stateful session Beans J0 14 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. “Basi di Dati Web e Distribuite” – Laurea Specialistica in Informatica – Università di Trento Marco Ronchetti - [email protected] Stateless session Beans J0 15 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. “Basi di Dati Web e Distribuite” – Laurea Specialistica in Informatica – Università di Trento Marco Ronchetti - [email protected] Stateless vs. stateful session Beans J0 16 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. “Basi di Dati Web e Distribuite” – Laurea Specialistica in Informatica – Università di Trento Marco Ronchetti - [email protected] Stateless vs. stateful session Beans J0 17 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. “Basi di Dati Web e Distribuite” – Laurea Specialistica in Informatica – Università di Trento Marco Ronchetti - [email protected] Stateful session Beans – examples of use J0 18 •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. “Basi di Dati Web e Distribuite” – Laurea Specialistica in Informatica – Università di Trento Marco Ronchetti - [email protected] Stateless session Beans – examples of use J0 19 •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. “Basi di Dati Web e Distribuite” – Laurea Specialistica in Informatica – Università di Trento Marco Ronchetti - [email protected] Message Driven Beans J0 20 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. “Basi di Dati Web e Distribuite” – Laurea Specialistica in Informatica – Università di Trento Marco Ronchetti - [email protected] Message Driven Beans J0 21 A message-driven bean's instances retain no data or conversational state for a specific client. All instances of a message-driven bean equivalent, allowing the EJB container assign a message to any message-driven instance. The container can pool these instances to allow streams of messages processed concurrently. are to bean to be A single message-driven bean can process messages from multiple clients. “Basi di Dati Web e Distribuite” – Laurea Specialistica in Informatica – Università di Trento Marco Ronchetti - [email protected] A more detailed architectural view CMP JDBC J0 22 Image taken from “Mastering EJ2B” DB “Basi di Dati Web e Distribuite” – Laurea Specialistica in Informatica – Università di Trento Marco Ronchetti - [email protected] More on Entity Beans: PERSISTENCE J0 23 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. “Basi di Dati Web e Distribuite” – Laurea Specialistica in Informatica – Università di Trento More on Entity Beans: SHARED ACCESS Marco Ronchetti - [email protected] Entity beans may be shared by multiple clients. J0 24 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. “Basi di Dati Web e Distribuite” – Laurea Specialistica in Informatica – Università di Trento Marco Ronchetti - [email protected] More on Entity Beans: DB-like features J0 25 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). “Basi di Dati Web e Distribuite” – Laurea Specialistica in Informatica – Università di Trento More on Entity Beans: SHARED ACCESS Marco Ronchetti - [email protected] Entity beans may be shared by multiple clients. J0 26 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. “Basi di Dati Web e Distribuite” – Laurea Specialistica in Informatica – Università di Trento Marco Ronchetti - [email protected] KEY CONCEPTS J0 27 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