Enterprise Java Beans (EJBs) TAKE IT TO THE NTH
TAKE IT TO THE NTH Andrew Gilbert andrew.gilbert@sun.com www.sun.com/developers/evangcentral Senior Software Engineer Sun Microsystems
Agenda EJB Architecture EJB 2.0 EJB 2.1 Roadmap
EJB Architecture TAKE IT TO THE NTH
Resources and Summary TAKE IT TO THE NTH
EJB Goals Provide standard component architecture for building distributed applications in Java Easy to write applications by eliminating need to write services code Applications follow "Write Once, Run Anywhere" philosophy Address development, deployment and runtime aspects of distributed application's lifecycle
EJB Architecture
EJB Architectural Roles Defines six distinct roles in the application development and deployment life cycle EJB Bean Provider Application Assembler Deployer EJB Server Provider Container Provider System Administrator to add an outline
Types of Beans EJB 2.0 defines Session Beans Entity Beans Message Driven Beans
Session Beans Exhibits following characteristics Does work on behalf of client Can be transactional Is not persistent and hence relatively short lived Does not represent data in data store, although can access/update such data Bean class implements javax.ejb.sessionbean interface Two subtypes viz. Stateful and stateless
Entity Beans Exhibits following characteristics Provides object view of data in data store Allows shared access for multiple, concurrent users Lives as long as data exists in database i.e. Long lived Bean class implements javax.ejb.entitybean interface Persistent state can be either managed by container i.e. CMP or can be managed by bean provider him/herself i.e. BMP
EJB Architecture Contracts Client view contract Contract between client and container Component contract Contract between an Enterprise Bean and its Container
Client View Contract Client of an EJB can be Another EJB in same or different container A Java application / applet / servlet / JSP A non-java client such as CORBA client Provides development model for clients using EJB services
Client View Contract (Contd.) Client view contract comprises of Home interface For local or remote clients Remote interface For local or remote clients Object identity Metadata interface Handle
Component Contract Enabling EJB method invocations from clients Managing life cycle and related notifications Defining and implementing home and remote interfaces Provide persistence for CMP entity beans Making bean class instances context aware Managing transactions, security, exceptions, etc... Implementing callbacks and receiving notifications
EJB Contracts Client view contract Client Enterprise bean instances Container Component contract EJB server
EJB Transactions Support Supports distributed transactions Containers implement low level Transaction Protocols such as two phase commit, distributed two phase commit, transaction context propagation Specifies two types of transaction demarcation Bean managed using javax.transaction.usertransaction Container managed by declaring transaction attributes Supports Flat transaction model Supports resource adapter specific isolation levels
EJB Transactions: JTA and JTS Java Transaction API (JTA) Specification of interfaces between transaction manager and other parties involved in a distributed transaction such as client, container, RM, etc. Java Transaction Service (JTS) Java binding of CORBA's OTS 1.1 specification Provides transaction interoperability across servers if IIOP is supported by both of them
EJB Transactions A sample scenario of updating multiple databases Client X Y Database A Database B Database C
EJB Transactions A sample scenario of updating through multiple EJB servers EJB Server EJB Server Client X Y Database A Database B
EJB Transactions A sample scenario of updates in client demarcated transactions Client Begin X Commit Database A Y Database B
EJB Transactions A sample scenario of updates from a non-transactional client EJB Server Client Begin Commit X Y Database A Database B
EJB Exception Handling Types Two types of exceptions, as defined by EJB specification Application Exceptions Exceptions defined in throws clause of methods defined on Home and Remote interfaces, other than RemoteException For example, javax.ejb.createexception, javax.ejb.finderexception, javax.ejb.removeexception System Exceptions Unexpected exceptions that prevent business methods or container callbacks from executing successfully either due to JVM errors or RemoteException resulting from invocation of other enterprise beans or unexpected RuntimeException and so on
EJB Exception Handling Goals The EJB specification for Exception Handling is designed to meet following goals Application Exception thrown by enterprise bean instance should be reported to client as is Application Exception thrown by enterprise bean instance should not automatically rollback a client's transaction System exceptions that may have left instance's state variables and/or underlying persistent data in an inconsistent state can be handled safely
Support for Distribution EJB specification makes it possible for containers to use RMI-IIOP as object distribution protocol No mandate on containers to use RMI-IIOP though The communication stubs used at the client-side to communicate with server-side objects are considered standard if container uses RMI-IIOP as object distribution protocol, otherwise they are considered to be container-proprietary
EJB Security Model Client1 Principal used: User1 Manager EJB Server Invokes TAHome.create() Client2 Principal used: User 2 Caller Principal mapping to J2EE security roles All X Invokes TAHome.find() Mapping typically done by Deployer Mapping typically done by Application Assembler either Bean Provider
EJB Security Styles Declarative Bean Provider, Assembler and Deployer handles security completely by declaring various settings Programmatic Code against security API exposed via javax.ejb.ejbcontext getcallerprincipal() iscallerinrole(string rolename) getcalleridentity () - deprecated iscallerinrole(java.security.identity identity) - deprecated
EJB Security Concepts Security Domain Principal Mapping Principal Delegation System Principal Runtime Security Enforcement Audit Trail
Ejb-jar file Standard format for packaging enterprise beans Used to package un-assembled and assembled enterprise beans Must contain deployment descriptor For each enterprise bean ejb-jar file must contain following class files Enterprise bean class Enterprise bean home and remote interface Primary key class if the bean is entity bean
Ejb-client JAR file Ejb-jar file producer can create ejb-client JAR file for ejb-jar file Consists of all classes that client program needs to use client view of enterprise beans contained in ejbjar file Can be specified in deployment descriptor of ejbjar file Deployer should ensure that specified ejb-client JAR file is accessible to client program's class loader
Deployment Descriptor Defines contract between producer and consumer of ejb-jar file It is an XML document that must be well formed in XML sense and must be valid with respect to the DTD given in specification Must be stored with the name META-INF/ejbjar.xml in the ejb-jar file Captures two basic kinds of information viz. Structural and Application assembly information
Deployment Descriptor Structural Information Describes structural and external dependencies of enterprise bean Mandate on ejb-jar producer to provide structural information Should not be changed since it may break the enterprise bean's function Example Fully qualified class name of Enterprise bean class, home interface and remote interface State management type of session bean, etc.
Deployment Descriptor Application Assembly Information Describes how enterprise bean(s) in ejb-jar file can be composed into a larger application deployment unit Can be optionally provided by ejb-jar producer Changes may not break functionality of enterprise bean, however doing so may change its behavior Examples Transaction attributes Re-entrancy indication, etc.
EJB 2.0 New Features Integrated support for JMS Support for local interfaces Improved architecture for Container Managed Persistence Support for container managed relationships among entity beans Enterprise JavaBeans TM Query Language (EJB QL) Home methods for entity beans Network interoperability
EJB 2.0 EG Community Allaire Art Technology Group BEA Bluestone Forte Fujitsu Gemstone IBM Inline Software Inprise Iona iplanet Luna Information Systems Oracle Persistence Progress Software Secant Siemens Silverstream Software AG Sun Microsystems Sybase The Object People Tibco Vitria
JMS Support By adding new enterprise bean type called "MessageDrivenBean" Stateless bean without home and remote interface Activated upon message arrival Bean implements javax.jms.messagelistener interface onmessage() method implementation should contain business logic Configured as listener for queue or topic
JMS and EJB JMS APIs for sending messages available to all enterprise beans Use in point-to-point configurations a.k.a Reliable Queuing Use within pub/sub configurations
Local Interfaces For both Entity beans Session beans Enables local call semantics for inprocess calls to EJBs
Container Managed Persistence Goals Allow for scalable, high-performance implementations Allow wide range of modeling options Allow leverage of object-relational mapping technology Completely re-architected in EJB TM 2.0 Feedback from our experts/partners on limitations of EJB 1.1 container managed persistence Support for EJB 1.1 container managed persistence is required
Building blocks of CMP Remote entities Relatively coarse-grained business objects Local entities Finer-grained business objects Value classes Provide a client view of persistent state
Container Managed Relationshps 1-1, 1-N, M-N associations Among local entities Allow transparent navigation Unidirectional or bidirectional navigability Foundation for path expressions in queries
Global View of CMP/CMR Bean operates in a managed environment Persistence Manager provides services to the bean Management of persistent state Relationship management, including Referential integrity Collection management Query service (for finder methods) Deployment descriptor embodies semantic contract between Bean and Persistence Manager Abstract Persistence Schema
Managed entity state access protocol Bean Provider defines abstract entity bean class with abstract get and set methods Persistence manager provides implementation Getters and setters return persistent state (cmpfields) and related objects (cmr-fields). Restrictions on what the Bean Provider can expose through the remote interface No local entity
Persistence Manager Implementation View Persistence manager handles persistent state management of fields and relationships getters/setters and lifecycle methods (e.g., ejbcreate, ejbload, ejbstore,...) provide hooks Referential integrity management of relationships is persistence manager responsibility
Abstract Persistence Schema Defined in Deployment Descriptor Defines Managed State: cmp-fields Managed Relationships: cmr-fields
EJB Query Language Queries defined at logical, abstract schema level in deployment descriptor SQL-like typed language with SELECT, FROM, and WHERE clauses Query domain consists of all entity beans (local and remote) in a logical ejb-jar file Queries based on navigability over abstract schema types Supports parameterized queries
Query Types Two kinds of query methods ejbfind ejbselect Persistence Manager provides implementation based on Bean Provider's declarative query specification in deployment descriptor
Home Methods Additional methods on Home interface for entity beans with both bean and container managed persistence Business methods that are not specific to an entity bean instance Implementation provided by Bean Provider Useful for bulk updates or other aggregate operations
Network Interoperability Three scenarios for invocations across J2EE servers: Server to server over EJB components Application clients to EJB components Web clients (JSP/Servlet) to EJB components RMI/IIOP Transaction context propagation based on OTS 1.2 Interoperable naming based on CORBA CosNaming Security context propagation based on CSIv2
Resources http://java.sun.com/products/ejb http://java.sun.com/j2ee/blueprints http://www.theserverside.com Send your feedback about specification at ejb-spec-comments@eng.sun.com
Call for Action! Download J2EE 1.3 Reference Implementation Start developing EJBs today!
www.sun.com/developers/evangcentral In pursuit of the best software in the universe All presentations Audiocasts Codecamp materials Technology briefings code/articles/links/chats/resources
TAKE IT TO THE NTH Andrew Gilbert andrew.gilbert@sun.com