Presentation is loading. Please wait.

Presentation is loading. Please wait.

Enterprise JavaBeans Ruslana Svidzinska CSE690. What is EJB? l An EJB is a specialized, non-visual JavaBean that runs on a server. l EJB technology supports.

Similar presentations


Presentation on theme: "Enterprise JavaBeans Ruslana Svidzinska CSE690. What is EJB? l An EJB is a specialized, non-visual JavaBean that runs on a server. l EJB technology supports."— Presentation transcript:

1 Enterprise JavaBeans Ruslana Svidzinska CSE690

2 What is EJB? l An EJB is a specialized, non-visual JavaBean that runs on a server. l EJB technology supports application development based on a multiplier, distributed object architecture in which most of application’s logic is moved from the client to the server.

3 Server Components l Server components are application components that run in an application server.

4 Java Application Servers l A Java application server provides an optimized execution environment for server-side Java application components. l A Java application server delivers a high-performance, highly scalable, robust execution environment specifically suited to support Internet enabled application systems.

5 WORA l The Enterprise JavaBeans architecture defines a standard model for Java application servers to support “Write Once, Run Anywhere” (WORA) portability

6 Component Portability EJB technology takes the WORA concept to a new level. EJB completely portable across any vendor’s EJB compliant application server. The EJB environment automatically maps the component to the underlying vendor-specific infrastructure services.

7 Overview of EJB Technology Enterprise JavaBeans component model logically extends the JavaBeans component model to support server component. Server components are reusable, prepackaged pieces of application functionality that are designed to run in an application server. EJB can be assembled and customized at deployment time using tools provided by an EJB- compliant Java application server. EJB Component Model

8 Simplifying Development. The EJB architecture provides an integrated application framework. An EJB server automatically manages a number of tricky middleware services on behalf of the application components. EJB component-builders can concentrate on writing business logic rather than complex middleware

9 Simplifying Development (cont’d) RESULT: Application get developed more quickly Code is of better quality.

10 What EJB Accomplishes You can take any Java class and with little effort make it a distributed, secure, transactional class You can take any data source and make the data source appear to be a collection of Java objects –Eliminates distinction between data from a database and any other source –All information is accessed through Java objects –All SQL is cleanly encapsulated in Java objects l true object-oriented programming l high reusability –Database objects work with the full Java class library

11 What EJB Means to Us l Developers can focus on writing business logic rather than writing low-level infrastructure like data access, concurrency, transactions, threading, etc. –Reduces development time –Reduces complexity –Increases quality and reliability l The knowledge about EJB is portable among many different products because EJB products are based on a common standard l Greater reuse because code is located in shareable, server objects

12 So....let’s take a look at Enterprise JavaBeans H A specification from JavaSoft H Enterprise JavaBeans defines a server component model for the development and deployment of Java applications based on a multi-tier, distributed object architecture H The Enterprise JavaBeans specification defines: –A container model –A definition of the services the container needs to provide to an Enterprise JavaBean, and vice versa –How a container should manage Enterprise JavaBeans

13 Enterprise JavaBeans Architecture The EJB architecture specifies the responsibilities and interactions among EJB entities  EJB Servers  Enterprise Beans  EJB Clients EJB Server EJB Container Enterprise Bean Enterprise Bean  EJB Containers Clients

14 EJB Server l The EJB Server provides system services and manages resources –Process and thread management –System resources management –Database connection pooling and caching –Management API Provides a Runtime Environment

15 EJB Server EJB Container l Hosts the Enterprise JavaBeans l Provides services to Enterprise JavaBeans –Naming –Life cycle management –Persistence (state management) –Transaction Management –Security l Likely provided by server vendor Provides a Run-time Environment for an Enterprise Bean

16 Enterprise JavaBeans l A specialized Java class where the real business logic lives –May be developer-written or tool-generated l Distributed over a network l Transactional l Secure l Server vendors provide tools that automatically generate distribution, transaction and security behavior EJB Server EJB Container Enterprise Bean Enterprise Bean

17 EJB Clients l Client access is controlled by the container in which the enterprise Bean is deployed l Clients locates an Enterprise JavaBean through Java Naming and Directory Interface (JNDI) l RMI is the standard method for accessing a bean over a network EJB Server EJB Container Enterprise Bean Enterprise Bean Clients

18 What’s Unique About EJB l Mandates a container model where common services are declared, not programmed –At development and/or deployment time, attributes defining the bean’s transaction and security characteristics are specified –At deployment time, the container introspects the Enterprise JavaBean attributes for the runtime services it requires and wraps the bean with the required functionality –At runtime, the container intercepts all calls to the object v Provides transactional, threading and security behavior required before the method invocation v Invokes the method on the object v Cleans up after the call Declarative Programming Model

19 Understanding EJB Components

20 JavaBeans vs Enterprise JavaBeans l Enterprise JavaBeans is a framework for building and deploying server-side Java components l JavaBeans is a framework for client-side Java components l Conceptually related because both are components l The specifications are different l The specifications do not build on each other or rely on each other

21 What’s in the EJB Specification ~200 Pages of technical material for EJB vendors l Goals for the Release l Roles and Scenarios l Fundamentals (Scope of EJB) l Session and Entity Beans l Transactions, Exceptions, Distribution l EJB Bean and Container Responsibilities l API Reference EJB Vendors Have to do all the WORK

22 Enterprise EJB Scenario ClientsWeb Server EJB Application Server Existing Enterprise Middleware CICS Programs SAP Modules Browser Application Servlet Shopping Cart Credit Card Inventory EJB Server EJB Container Databases

23 l Bean Class is written by the developer l EJBHome and EJBObject interfaces and classes control access to the Bean class l Deployment Descriptor and MANIFEST describe security and transactional characteristics of the Bean Written by Developer Generated at Deployment Generated at Development EJB Server EJB Container EJBHome Interface EJBHome Interface Deployment Descriptor EJBObject Interface EJBObject Interface EJBHome Class EJBHome Class EJBObject Class EJBObject Class EJB Class Manifest An inside look at the various components of EJB

24 EJB Server EJB Container EJBHome Interface EJBHome Interface EJBHome Class EJBHome Class EJBHome Interface and Class l Used to get a reference to a bean’s remote interface l Provides bean creation services –myFoo = fooHome.create() instead of myFoo = new foo() –Supports multiple signatures to create EJB instances l Similar to class factory in COM and CORBA l May be generated by tools that come with an EJB server l Also manages EJB: –querying (Entity Bean) –deleting (Entity Bean)

25 Interface javax.ejb.EJBHome l Home objects must implement this interface l Returns a reference to a bean by creating or finding it l Every bean has a EJBHome interface that provides methods for getting references to one or more beans –create methods are used to create new beans –there can be many create methods, similar to a class having many constructors l Provides a bean removal interface also l The EJBHome Class implementation is provided by the EJB Server Provider

26 Sample EJBHome Interface public interface CustomerHome extends EJBHome { public abstract Customer create(String id, String name) throws RemoteException, CreateException; public abstract Enumeration findByName(String val) throws RemoteException, FinderException; public abstract RemoteEnumeration findStateByName(String val) throws RemoteException, FinderException; public abstract Customer findByPrimaryKey(CustomerKey pkey) throws RemoteException, FinderException; public abstract Customer findByPrimaryKey(CustomerKey pkey, int findSource) throws RemoteException, FinderException; public abstract CustomerState findStateByPrimaryKey(CustomerKey pkey, int findSource) throws RemoteException, FinderException; }

27 Clients EJB Server EJB Container EJBHome Class create() Client makes invocation to EJBHome Class via the EJBHome Interface l Client calls one of the create() methods on the EJBHome Class. The EJBHome class can have multiple create() signatures.

28 Clients EJB Server EJB Container EJBHome Class EJB Class EJB Class EJBHome Class instantiates the Bean Class l When the create() method is called in the EJBHome Class, it is responsible for instantiating the Bean class.

29 EJB Server EJB Container EJBObject Interface EJBObject Interface EJBObject Class EJBObject Class EJBObject Interface and Class l Intercepts calls to the EJB Class to add support for: –transactions –security –threading l EJBObject class has the same methods as the bean and delegates to the bean as the bean and delegates to the bean for actual behavior l EJBObject class checks security and sets up transaction before delegating method call to the bean l Clients can never get a reference to a bean’s EJB Class, only the EJBObject interface

30 Interface javax.ejb.EJBObject public interface javax.ejb.EJBObject extends java.rmi.Remote { EJBHome getEJBHome() throws RemoteException; Handle getHandle() throws RemoteException; Object getPrimaryKey() throws RemoteException; boolean isIdentical(EJBObject obj) throws RemoteException; void remove() throws RemoteException, RemoveException; } l Represents a specific bean instance l Remote objects must implement this interface l Primary key is an object that represents the primary key for a specific instance of a bean l The EJBObject Class implementation is provided by the EJB Server Provider

31 Sample EJBObject Interface public interface Customer extends EJBObject { public abstract String getId() throws RemoteException; public abstract String getName() throws RemoteException; public abstract void setName(String val) throws RemoteException; public abstract boolean getNameNull() throws RemoteException; public abstract boolean getIdNull() throws RemoteException; public abstract Enumeration getAccounts() throws RemoteException; public abstract void addToAccounts(Account relInst) throws RemoteException; public abstract void removeFromAccounts(Account relInst) throws RemoteException; }

32 Clients EJB Server EJB Container Passes ref of Bean Client now has reference to the EJBObject Class. NOT A REFERENCE TO THE BEAN ITSELF!!!! EJBHome Class EJB Class EJB Class EJBHome Class After the EJBHome Class instantiates the Bean... l The EJBHome Class will instantiate the EJBObject Class, initializing it with the remote object reference to the Bean Class. l The Client will now communicate to the EJBObject Class. The EJBObject Class will delegate the call to the Bean.

33 Clients EJB Server EJB Container EJBHome Class EJB Class EJB Class EJBHome Class Client communicates to the Bean class via the EJBObject Class l Client never has a direct reference to the Bean. l The EJBObject Interface is the interface for the Bean, so the EJBObject Class “delegates” the calls to the Bean. l The EJBObject Class implementation will be generated using vendor tools.

34 EJB Class l A bean has a single Java class at its core –This class is written by a developer if it’s a session bean –This class is sometimes generated by a tool if it’s an entity bean l Implements application-specific business logic l Implements one of the following contracts: –javax.ejb.EntityBean –javax.ejb.SessionBean l These contracts provide for consistent behavior when activating beans, passivating beans, reading data, writing data l Every container can expect these methods in every bean

35 Deployment Descriptor EJB Container EJB Server Deployment Descriptor l Allows you to declare transaction and security attributes, NO PROGRAMMING REQUIRED!!! l An EJB Deployment Descriptor describes the classes, interfaces and declarative behavior of an EJB l Deployment Descriptor format is serialized objects: –javax.ejb.deployment.ControlDescriptor –javax.ejb.deployment.DeploymentDescriptor –javax.ejb.deployment.EntityDescriptor –javax.ejb.deployment.SessionDescriptor l The deployment descriptor is generated by server tools l The deployment descriptor classes are defined in the EJB specification

36 Let’s look at the value of the Deployment Descriptor l Step #1 - Write your Bean implementation. l Step #2 - Compile this Java source into Java bytecode l Step #3 - Developer is responsible for creating a serialized deployment descriptor for the bean. Most likely using a tool from the vendor. EJB tools will use Reflection API on the compiled EJB Bean to determine: –name of Bean class –methods –parameters –return values l Step #4 - The EJB Tool will use the above information to generate a Deployment Descriptor file and an editor with which to set transaction and security attributes.

37 Sample Contents of a Deployment Descriptor PS(TicketDemo,Concert,deployment_descriptor) Entity{ VersionNumber { 1.0 } BeanHomeName { ConcertHome } ControlDescriptor { IsolationLevel { TRANSACTION_READ_COMMITTED } Method { int getId() throws java.rmi.RemoteException } RunAsMode { SYSTEM_IDENTITY } TransactionAttribute { TX_SUPPORTS } Method Level Security Attribute Method Level Transaction Attribute

38 Manifest EJB Container EJB Server Enterprise JavaBean Packaging l Enterprise JavaBeans are comprised of many Java files l These files are put in a JAR file –A JAR file is a ZIP file with a MANIFEST that describes the contents of the file –A MANIFEST is a simple text file Name: bank/AccountDeployment.ser Enterprise-Bean: True Name: bank/AccountDeployment.ser Enterprise-Bean: True l A JAR file can contain more than one Enterprise JavaBean

39 Written by Developer Generated at Development Generated at Deployment EJB Server EJB Container EJBHome Interface EJBHome Interface Deployment Descriptor EJBObject Interface EJBObject Interface EJBHome Class EJBHome Class EJBObject Class EJBObject Class EJB Class Manifest Bean Development Process l Implement the EJB Class l Specify the remote interface l Specify the home interface l Specify security and transactional characteristics using vendor tools (DeploymentDescriptor) l Use vendor tools to generate supporting code and package components in EJB-jar l Iterate...

40 Enterprise Beans: Session Beans & Entity Beans Session Beans vs. Entity Beans Bean-Managed Persistence Container-Managed Persistence EJB Transaction Attributes

41 Comparing Session and Entity Beans l Mandatory for EJB 1.0 l Represents a specific client (1 instance per client) l Short-lived l Transient l Can be any Java class l May be transactional l Business Logic Beans l Optional for EJB 1.0 l Represents underlying data object or context (clients share instance) l Long-lived l Persistent l Can be a class that maps to persistent data (e.g., database) l Always transactional l Beans which represent data Session Beans Entity Beans

42 Session Beans Represents Process l A transient agent for an individual client that executes on a server (e.g., ShoppingCart) l Session beans are often a client of multiple entity beans Implements javax.ejb.SessionBean interface Implements javax.ejb.SessionBean interface l State management types for session EJBs –stateful - session bean may maintain state information across method calls –stateless - session bean may be used to service multiple clients –a stateless session bean can only have a single no-argument create() method in its Home interface

43 Session Beans - Stateful or Stateless l A Stateful Session Bean maintains a one-to-one relationship with a Client. It maintains a user “session”. Most common example is an e-commerce application with a “shopping cart” unique for each user. –Container will automatically “swap out” the Session bean if it is inactive. Here the container calls the ejbPassivate() method to save any private data to some physical storage. –When container receives new request, the Container will call the ejbActivate() method to restore the Session Bean. l A Stateless Session Bean can be accessed by multiple incoming clients and keeps no private data. It does not maintain a unique session with a client. –Keeps no persistent data. If it crashes, container simply starts another one and the client transparently connects. –All access to the Bean is serialized.

44 Entity Bean Represents Data Implements javax.ejb.EntityBean interface Implements javax.ejb.EntityBean interface l Maps a data source to a Java class –table, view, join or stored procedure in a relational database –a set of related records in a database –legacy data l Each instance of an entity bean is one row of data l Each instance of an entity bean is uniquely identified by a primary key l An Entity Bean can also have additional methods for business logic, etc.

45 Defining an Entity Bean l An Entity Bean must implement the javax.ejb.EntityBean interface. In addition, the Entity Bean must implement an ejbCreate() method for each create() method on the Home Interface. l When the Home class gets a call to create(), it calls the ejbCreate() method on the Entity Bean with the corresponding signature. l ejbStore() and ejbLoad() used to synchronize Entity Bean data with the database. l ejbActivate() and ejbPassivate() used for notification that Entity Bean state is being written to the database or restored from the database.

46 Since Entity Beans represent Data... l Each instance of an Entity Bean is uniquely identified by a primary key object l Primary key can be saved and used later to regain access to the same EJB object identity –fooKey = fooBean.getPrimaryKey(); … fooBean = fooHome.findByPrimaryKey(fooKey);

47 Entity Bean Operations l BeanHome.create() is equivalent to an SQL INSERT –Causes a new database record to be inserted into the database: l customerBean = customerHome.create(”Jane", ”Rodgers"); l BeanHome.findSomething() is equivalent to an SQL SELECT –Creates a bean instance that represents queried data from a database –Can also return an enumeration to represent multiple rows of data l There will typically be many findSomething() methods: l customerBean = customerHome.findByName(firstName, lastName); l customerBeans = customerHome.findByZip(94023);

48 Entity Bean Operations l BeanHome.remove() and EJBObject.remove() are equivalent to an SQL DELETE –causes the database record to be deleted from the database customerBean = customerHome.findByName(“Jane”, “Rodgers”); customerBean.remove(); customerBean = customerHome.findByName(“Jane”, “Rodgers”); customerBean.remove(); l Commiting a change on a found object is equivalent to an SQL UPDATE l customerBean = customerHome.findByName(“Jane”, “Rodgers”); customerBean.setEmailAddress(“jane.rodgers@yahoo. com”);

49 EJB Persistence l Provides Entity Beans the ability to store and retrieve their state l Can be implemented by a bean –Bean Managed Persistence l Can be implemented by a container –Container Managed Persistence

50 Bean Managed Persistence l The entity bean is responsible for its persistent behavior l EJB developer must implement database access –ejbCreate(…), ejbLoad(), ejbStore(), ejbRemove() l Not automated, developer manually creates mapping through JDBC calls l Not as reusable –Hard-code database access within class l Advanced features like connection pooling and caching are difficult to support because of reliance on hand written code

51 Container Managed Persistence l The EJB container is responsible for persistence l The container provides tools that generate code in the EJB class that maps methods in the bean to a result set –Can map to a table, view, join or stored procedure in a database –Server provides automated mapping to convert relational data to bean instances l Advanced features like connection pooling and caching are easily supported l High reuse

52 EJB Transaction Support l EJB allows you to either DECLARE transactions or CODE THEM EXPLICITLY. –TO DECLARE: EJB allows you to declare transaction support in the Deployment Descriptor and then have the container take care of the details inside the remote class l Transaction attributes specified at the bean and method level –FOR EXPLICIT CONTROL: An EJB Container is required to implement javax.transaction.UserTransaction l Provides an interface to a transaction service l Can be used by EJB clients and enterprise beans that are declared to manage their own transactions (TX_BEAN_MANAGED) –EJB specification does not support nested transactions

53 EJB Transaction Attributes l TX_NOT_SUPPORTED –Cannot execute within a transaction –Suspends any existing transaction while method is called l TX_SUPPORTS –Executes with or without a transaction –Doesn’t suspend existing transaction l TX_REQUIRED –Executes within a transaction –If no transaction exists, starts a new one and commits it when method completes l TX_REQUIRES_NEW –Always starts a new transaction and commits it when method completes –Suspends existing transaction l TX_BEAN_MANAGED –The bean manages its own transaction control –Suspends existing transaction l TX_MANDATORY –Must execute within a transaction –If no transaction exists, throws the TransactionRequiredException


Download ppt "Enterprise JavaBeans Ruslana Svidzinska CSE690. What is EJB? l An EJB is a specialized, non-visual JavaBean that runs on a server. l EJB technology supports."

Similar presentations


Ads by Google