1 / 140

Enterprise JavaBeans

Enterprise JavaBeans. Instructors: Geoffrey Fox and Bryan Carpenter Dept. of Computer Science School of Computational Science and Information Technology 400 Dirac Science Library Florida State University Tallahassee Florida 32306-4120 http://www.csit.fsu.edu Acknowledgements: Xi Rao

bettyc
Download Presentation

Enterprise JavaBeans

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. Enterprise JavaBeans Instructors: Geoffrey Fox and Bryan Carpenter Dept. of Computer Science School of Computational Science and Information Technology 400 Dirac Science LibraryFlorida State UniversityTallahassee Florida 32306-4120 http://www.csit.fsu.edu Acknowledgements: Xi Rao http://aspen.csit.fsu.edu/webtech/xml/ it2ejb http://aspen.csit.fsu.edu/it2spring01

  2. What is EJB? • Enterprise JavaBeans (EJB) combines server-side components with distributed object technologies such as CORBA and Java RMI to greatly simplify the task of application development • in a specialized, albeit pervasive, domain of application. it2ejb http://aspen.csit.fsu.edu/it2spring01

  3. References • Enterprise JavaBeans, 2nd Edition, Richard Monson-Haefel, O’Reilley, 2000. • Enterprise JavaBeans, specification documents: http://java.sun.com/products/ejb/docs.html it2ejb http://aspen.csit.fsu.edu/it2spring01

  4. Component Transaction Monitors • EJB is a specification for Component Transaction Monitoring (CTM) software. • CTMs provide an infrastructure that can automatically manage transactions, object distribution, concurrency, security, persistence, and resource management. • They can handle huge user populations and mission-critical work. it2ejb http://aspen.csit.fsu.edu/it2spring01

  5. Transaction Processing • Transactional integrity is important in business environments. • It is important to ensure that particular sets of events occur atomically or not at all. • For example a single transaction may involve: • Billing a customer • Dispatching the goods • Updating a database of orders • If any one of these operations fail for any reason we should ensure that none of them proceed. • Dispatching goods if billing failed costs a company money; billing the customer if the goods cannot be dispatched loses customers. . . it2ejb http://aspen.csit.fsu.edu/it2spring01

  6. TP Monitors • TP (Transaction Processing) Monitors automatically manage the entire environment that a business system runs in, including transactions, resource management, and fault tolerance. • Advantage: • a long time running with solid technology • Disadvantage: • Not object oriented (may use RPC—Remote Procedure Call—but not remote method invocation) • Thus not flexible, extensible, reusable it2ejb http://aspen.csit.fsu.edu/it2spring01

  7. Distributed Objects • Different objects reside on different servers and interact to get the job done. • Allow “business logic” and data to be accessed from remote locations. • Important implementations of distributed objects: • Java RMI, • CORBA, • DCOM. it2ejb http://aspen.csit.fsu.edu/it2spring01

  8. Server-Side Components • Advantage of OOP: • flexible • extensible • reusable • The encapsulation of business logic into a business object is important. • For purposes of this course, “business logic” will just mean the application code in the methods of an object, or bean. it2ejb http://aspen.csit.fsu.edu/it2spring01

  9. Distributed Object Architecture it2ejb http://aspen.csit.fsu.edu/it2spring01

  10. Remote Method Invocation it2ejb http://aspen.csit.fsu.edu/it2spring01

  11. Object Request Brokers • A distributed object technology. • They allow unique objects that have state and identity to be distributed across a network. • Advantage: • Object Oriented • Disadvantages: • Leave to developer burdens of managing concurrency, transactional integrity, resource management and fault tolerance. it2ejb http://aspen.csit.fsu.edu/it2spring01

  12. Component Transaction Monitors • Component Transaction Monitors (CTMs) are an extension of traditional transaction monitors (IBM CICS, etc.) • CTM industry builds on both ORBs (object request brokers) and TPMs. • CTMs are a hybrid of these technologies that provides powerful, robust, distributed object platforms. • They provide an infrastructure that can automatically manage transactions, object distribution, concurrency, security, persistence, and resource management. • They may also incorporate instance swapping, resource pooling, and activation, etc. • Thus CTMs can handle large user populations and “mission-critical” work. it2ejb http://aspen.csit.fsu.edu/it2spring01

  13. Example CTMs • Microsoft Transaction Server (MTS): • Only runs on the Microsoft platform—limited opportunity for other vendors. • CORBA: • Both language and platform independent. • Transaction processing, concurrency, persistence, security are services additional to the basic model, and hard to use. • Enterprise JavaBeans: • Services integrated into basic model. • Specification deliberately loose enough to be implemented as wrappers to “legacy” software from many vendors. • First appeared in Dec, 1997. • Most recent update is Oct, 2000 (EJB specification 2.0). it2ejb http://aspen.csit.fsu.edu/it2spring01

  14. Enterprise JavaBeans it2ejb http://aspen.csit.fsu.edu/it2spring01

  15. Sun’s Definition of EJB • The Enterprise JavaBeans architecture is a component architecture for the development and deployment of component-based distributed business applications. Applications written using the Enterprise JavaBeans architecture are scalable, transactional, and multi-user secure. These application may be written once, and then deployed on any server platform that supports the Enterprise JavaBeans specification. it2ejb http://aspen.csit.fsu.edu/it2spring01

  16. Relation to JavaBeans • Enterprise JavaBeans is somewhat unrelated to “conventional” JavaBeans. • They are both component architectures, and they share some basic ideas about properties, naming conventions, etc. • But the associated technologies are highly specialized to their particular domains of applicability: • In practice, standard JavaBeans technology is mostly geared to rapid development of GUIs. Enterprise Javabeans is geared to the (radically different) area of distributed transaction processing. • A conventional bean is a local object. An Enterprise bean is always (part of) a distributed object. it2ejb http://aspen.csit.fsu.edu/it2spring01

  17. Comparision of JavaBeans and EJB it2ejb http://aspen.csit.fsu.edu/it2spring01

  18. Enterprise Bean Component • There are two distinct kinds of Enterprise beans: entity beans and session beans. • Entity beans: • typically model business concepts that can be expressed as nouns, e.g. “customer”. • model real world objects that usually have associated persistent record in the company database. • Session beans: • are an extension of the client application and manage processes or tasks such as making a reservation. • do not represent something in a database, they may change update the database it2ejb http://aspen.csit.fsu.edu/it2spring01

  19. Classes and Interfaces • A client interacts with an Enterprise bean by invoking remote methods in Java RMI-style remote interfaces. • In simple Java RMI, the application developer supplies a remote interface and a remote implementation class that implements this interface. • In EJB, the application developer supplies two remote interfaces and one class: • the home interface, • the bean remote interface, and • the bean implementation class. • The bean class itself does not directly implement either of the interfaces. • Instead they are implemented by classes generated by the EJB container. it2ejb http://aspen.csit.fsu.edu/it2spring01

  20. Classes and Interfaces • The remote interface: • Defines bean’s “business methods”. • Extends javax.ejb.EJBObject which in turn extends java.rmi.Remote • The home interface: • Define bean’s “life cycle” methods. • Extends javax.ejb.EJBHome which in turn extends java.rmi.Remote • The bean class: • Actually implements the bean’s business and initialization methods. • May inherit from javax.ejb.SessionBean or javax.ejb.EntityBean. it2ejb http://aspen.csit.fsu.edu/it2spring01

  21. EJB Interfaces and Classes it2ejb http://aspen.csit.fsu.edu/it2spring01

  22. Container and Server • The client does not interact with a bean class directly. • The client always uses the methods of the bean’s home and remote interfaces to do its work • There may also be various kinds of interaction between the bean and its server. • This interaction interaction managed by a “container”, which is responsible for presenting a uniform interface between beans and server. • The container is responsible for creating new instances of bean, making sure that they are stored properly by the server, and so on. • The concepts “container” and “server” are closely related, but they are considered distinct. it2ejb http://aspen.csit.fsu.edu/it2spring01

  23. Container vs Server • The EJB server-provider is a specialist in the area of distributed transaction management, distributed object, and other lower-level system-level services. • The EJB container-provider provides deployment tool and runtime support for deployed enterprise bean instances. • Currently the container-provider and server-provider are the same vendor, but it is foreseen that eventually vendors may specialize, and supply EJB servers and EJB containers as separate, plug in components. it2ejb http://aspen.csit.fsu.edu/it2spring01

  24. Example Remote interface • The remote interface for an Enterprise bean defines its so-called “business methods”, e.g.: import java.rmi.RemoteException; public interface Book extends javax.ejb.EJBObject { public String getName() throws RemoteException ; public void setName(String str) throws RemoteException ; } • Here, getName()and setName() are the business methods. • They are remote methods, and must throw RemoteException. it2ejb http://aspen.csit.fsu.edu/it2spring01

  25. Example Home interface • The home interface defines “life-cycle” methods, used for managing a bean, e.g.: import java.rmi.RemoteException; import javax.ejb.CreateException; import javax.ejb.FinderException; public interface BookHome extends javax.ejb.EJBHome { public Book create(int isbn) throws CreateException, RemoteException; public Book findByPrimaryKey(BookPK pk) throws FinderException, RemoteException; } it2ejb http://aspen.csit.fsu.edu/it2spring01

  26. Home interface • The create() method will be responsible for initializing an instance of our bean. If your application needs it, you can provide other create() methods, with different arguments. • In addition to the findByPrimaryKey(), you are free to define other methods that provide convenient ways to look up already-existing Book beans. • BookPK stands for “book primary key”—primary keys will be discussed shortly. it2ejb http://aspen.csit.fsu.edu/it2spring01

  27. Example Bean class import javax.ejb.EntityContext; public class BookBean implements javax.ejb.EntityBean { public int isbn ; public String name ; public BookPK ejbCreate(int isbn){ this.isbn = isbn; return null ; // Assumes “container managed persistence” } public void ejbPostCreate(int isbn){} public String getName(){ return name; } public void setName(String str){ name = str; } . . . } it2ejb http://aspen.csit.fsu.edu/it2spring01

  28. Bean class • The business methods are the only methods that are directly visible to the client application; the other methods are visible only to the EJB container. • The ejbCreate() and ejbPostCreate() methods initialize the instance of bean class. • The are invoked indirectly by the container, when the client invokes create() on the home interface. • ejbCreate() creates a record in the database. • After ejbCreate() succeeds, the container establishes a primary key for the entity. • ejbPostCreate() can be used to do any necessary “post-initialization”—for example, initialization operations that rely on the existence of a primary key. it2ejb http://aspen.csit.fsu.edu/it2spring01

  29. Example Bean class (continued) • The EntityBean interface also requires one to implement a series of so-called call-back methods. For now we can leave their bodies empty, e.g.: public class BookBean implements javax.ejb.EntityBean { . . . public void setEntityContext(EntityContext ctx){} public void unsetEntityContext(){} public void ejbActivate(){} public void ejbPassivate(){} public void ejbLoad(){} public void ejbStore(){} public void ejbRemove(){} } • The EJB Server invokes the setEntityContext(), unsetEntityContext(), ejbActivate(), ejbPassivate(), ejbLoad(), ejbStore(), ejbRemove()methods when important state management events occur. it2ejb http://aspen.csit.fsu.edu/it2spring01

  30. Primary Key • When a entity bean is deployed in an EJB server, it’s persistent fields are mapped to a database. • The primary key is a “pointer” that helps locate the unique record or entity in a database that describes the bean. • The bean can be retrieved from the database by using its primary key. • Using the findByPrimaryKey() method in home interface. • A primary key class must be a Serializable object. • A primary key class must override the hashCode() and equals() methods from java.lang.Object. • For every field of the primary key, the associated bean class must have a public field with exactly the same name and type. it2ejb http://aspen.csit.fsu.edu/it2spring01

  31. Example Primary Key public class BookPK implements java.io.Serializable { public int isbn; public BookPK() {} // required! public BookPK(int isbn) { this.isbn = isbn ; } public int hashCode(){ return isbn; } public boolean equals(Object obj){ if (obj instanceof BookPK){ if (((BookPK) obj).isbn == isbn) return true; } return false; } } it2ejb http://aspen.csit.fsu.edu/it2spring01

  32. Compound Primary Keys • In general the primary key can have more than one field. These must identically match fields in the associated bean class. • For example, here is an alternative, more complex, organization for a book bean: public class BookBean implements javax.ejb.EntityBean { String Title ; String Author ; String publisher ; String price ; . . . etc, etc } public class BookPK implements java.io.Serializable { String Title ; String Author ; } • The assumption is that every book is uniquely characterized by the combination of title and author. it2ejb http://aspen.csit.fsu.edu/it2spring01

  33. Using the Bean • Armed with the specification of the two remote interface classes, the bean class, and the public key class, we can write the client code. • Of course we still have some way to go before we can deploy this bean, but the deployment details in general shouldn’t change the code for the client. it2ejb http://aspen.csit.fsu.edu/it2spring01

  34. Example EJB Client public static void main(String [] args) throws RemoteException { Context initial = new InitialContext() ; Object objref = initial.lookup(“MyBook”) ; BookHome home = (BookHome) PortableRemoteObject.narrow(objref, BookHome.class); Book book1 = home.create(735710201) ; book1.setName(“Inside XML”); // Business method Book book2 = home.create(1565928695) ; book2.setName(“Enterprise JavaBeans”); // Business method // . . . then, perhaps much later, perhaps in a different program . . . BookPK pk = new BookBK(1565928695); Book book3 = home.findByPrimaryKey(pk); System.out.println(book3.getName()) ; // Prints “Enterprise JavaBeans” } it2ejb http://aspen.csit.fsu.edu/it2spring01

  35. Obtaining the Remote Reference • The first few lines are used to obtain a remote reference to the home interface of the bean. • Roughly this corresponds to looking up a remote reference in an RMI registry, but it is more complicated for a couple of reasons: • We use the generic JNDI (Java Name and Directory Interface) services rather than rmiregistry. • For reasons of CORBA-compliancy, the stub object returned by the initial.lookup() call does not implement the BookHome remote interface. • A subsequent call to PortableRemoteObject.narrow() is needed to generate a stub that does implement this interface. • Why could the two operations not be combined? it2ejb http://aspen.csit.fsu.edu/it2spring01

  36. Locating Beans with JNDI • JNDI allows the application client to view the EJB server as a set of directories, like directories in a common filesystem. • JNDI allows us to find and access beans regardless of their location on the network. • After the client application locates and obtains a remote reference to the EJB home using JNDI, the client can use the EJB home to obtain a remote reference to a bean. it2ejb http://aspen.csit.fsu.edu/it2spring01

  37. Creating and Finding Entity Beans • Once we have the reference to the home object, the rest is straightforward. • We create a couple of Book entities, and call the setName() business methods on them. • This is somewhat different from raw RMI, which has no intrinsic method for creating a remote object from a client—a remote object is usually deployed “manually” on the server. • The created Book bean is implicitly persistent. • It will live forever, unless it is explicitly deleted (or the underlying database is destroyed!) • Clients that run subsequently can retrieve a bean created earlier through suitable find methods—the most common way is to pass a suitable primary key object to the findByPrimaryKey() method. it2ejb http://aspen.csit.fsu.edu/it2spring01

  38. The Deployment Descriptor • A deployment descriptor serves a function similar to a property file. It allows us to customize the behavior of enterprise bean at deployment time (or “run-time”), without having to change the program itself. • EJB 1.1 Deployment descriptors use a flexible file format based on XML. • Many EJB containers will provide GUIs to generate the XML documents “automatically”. it2ejb http://aspen.csit.fsu.edu/it2spring01

  39. Example Deployment Descriptor <?xml version="1.0"?> <!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 1.1//EN” "http://java.sun.com/j2ee/dtds/ejb-jar_1_1.dtd"> <ejb-jar> <enterprise-beans> <entity> <ejb-name> BOOKEJB </ejb-name> <home>BookHome</home> <remote>Book</remote> <ejb-class>BookBean</ejb-class> <prim-key-class>BookPK</prim-key-class> <persistence-type>Container</persistence-type> <reentrant>False</reentrant> </entity> </enterprise-beans> </ejb-jar> it2ejb http://aspen.csit.fsu.edu/it2spring01

  40. Elements of the Deployment Descriptor • ejb-jar • root of XML deployment descriptor. Describes contents of a jar archive file. • enterprise-beans • may contain descriptions of entity beans and session beans. • entity • describes an entity bean and its deployment information. • ejb-name • descriptive name of the bean. • home • the name of the home interface class. • remote • the name of the remote interface class . • ejb-class • the name of the bean class • prim-key-class • the name of bean’s primary key class it2ejb http://aspen.csit.fsu.edu/it2spring01

  41. JAR Files • JAR (Java archive) files are archive files that are used specifically for packaging Java class (and other resources, such as images) which are ready for use in some type of application. • In particular, JARs are used for packaging Enterprise JavaBeans. They may contain that bean class, remote interface, home interface, primary key and descriptor file. • The Container can read JAR file. The it use the deployment descriptor to learn about the beans contained in the JAR file. it2ejb http://aspen.csit.fsu.edu/it2spring01

  42. Deploying an Entity Bean it2ejb http://aspen.csit.fsu.edu/it2spring01

  43. Deploying an EJB Application • Download SDK, Enterprise Edition, from http://java.sun.com/j2ee/j2sdkee Unpack the file in a suitable directory. • Follow the installation instructions: edit the bin/userconfig.sh script. • Run bin/deploytool. • Select new application from the file menu. • Give application “display name” (eg BookApp) and a project file name. it2ejb http://aspen.csit.fsu.edu/it2spring01

  44. it2ejb http://aspen.csit.fsu.edu/it2spring01

  45. Adding a Bean • Under the file menu, choose new enterprise bean. • You get a New Enterprise Bean Wizard. • Choose a JAR display name—a logical name for the JAR file we are creating. • Add the “contents” to the JAR file—the class files for your interface and bean classes—and other requested fields. it2ejb http://aspen.csit.fsu.edu/it2spring01

  46. New Enterprise Bean Wizard it2ejb http://aspen.csit.fsu.edu/it2spring01

  47. Define Interfaces and Classes • In the next screen of the wizard you specify which class files in the JAR correspond to the home and remote interfaces, and the bean class. • Specify we are defining an entity bean (or session bean). it2ejb http://aspen.csit.fsu.edu/it2spring01

  48. New Enterprise Bean Wizard it2ejb http://aspen.csit.fsu.edu/it2spring01

  49. Choose Persistence Mechanism • In the next screen we will choose “container managed persistence”. • You select which of the public fields in the class are two be persisted by the container. • You also define the primary key class. • If we are using a compound primary key like BookPK, the Primary KeyField Name box should be left empty! it2ejb http://aspen.csit.fsu.edu/it2spring01

  50. New Enterprise Bean Wizard it2ejb http://aspen.csit.fsu.edu/it2spring01

More Related