Enterprise Java Beans Fundamentals 1996 2000 j Guru

Enterprise Java. Beans Fundamentals © 1996 -2000 j. Guru. com

EJB Transaction attributes *Mandatory 2

Agenda • • Enterprise Java. Beans defined EJB and Distributed Computing EJB Architecture Entity beans Session bean Deployment EJB Clients 3

Enterprise Java. Beans: Defined © 1996 -2000 j. Guru. com

What is Enterprise Java. Beans The Enterprise Java. Beans architecture is a component architecture for the development and deployment of object-oriented distributed enterprise-level applications. Applications written using the Enterprise Java. Beans architecture is scalable, transactional and multi-user secure. These applications may be written once, and deployed on any server platform that supports the Enterprise Java. Beans specification Sun Microsystems Enterprise Java. Beans™ Specification, v 1. 1, Copyright 1999 by Sun Microsystems, Inc . 5

In English Please! • Enterprise Java. Beans is a specification – (a piece of paper) not a product – As is XML, CORBA, TCP/IP • Enterprise Java. Beans is Java based – Defined by Sun Microsystems – Applications are written in Java • Enterprise Java. Beans is distributed objects – Similar to CORBA | Java RMI (JRMP) | Microsoft MTS • Enterprise Java. Beans is components – Similar to regular Java. Beans (similar but not the same) – Similar to COM/DCOM/COM+ • Enterprise Java. Beans products are Transactional Monitors – Similar to CICS | TUXEDO – A complete environment for a distributed object 6

Enterprise Java. Beans • Simple Programming Model – – Attribute based programming Focus on Business Logic Simple API Well defined Lifecycle • Portable – Specification ensures basic services – Component Packaging (JARs and XML DD) – Java 7

WORA • Write-Once, Run Anywhere within Middleware • Middleware provides all services – Instance management, transactions, concurrency, persistence • Beans stay simple! 8

EJB Spec • Spec developed by Sun and dozens of competing vendors – IBM, Oracle, BEA, Web. Logic, . . . – Interoperable -- a miracle! • Why did they cooperate? – See next slide. . . 9

The Common Enemy Phenomenon 10

EJB Advantages • With EJB, you can write a business object and easily make it – – – Persistent Distributed Transactional Secure Multithreaded 11

Persistent • Beans need to load and store data • You can let the server do it – (Container-Managed Persistence) • You can do it yourself – (Bean-Managed Persistence) 12

Transactional • Support for distributed transactions • You can let the Server manage all transactions – You will if you know what’s good for you – You can give it hints in deployment descriptor 13

Secure • SSL/RMI protects transmitted data • Client-Server architecture protects – proprietary code – backend database • SSL authenicates client, server • ACLs provide fine-grained control of access to objects, methods, services 14

Multithreaded • Programmer delegates all responsibility for multithreading to server – Programmer literally can’t spawn a thread • Program designer and/or sysadmin establishes multithreading policies • Server enforces them invisibly 15

Naming Services • JNDI • Wrapper for many naming services – CORBA, RMI, etc. 16

CORBA: Sorta Porta(ble) • CORBA: Complex Programming Model – Different API for each Service – Explicit use of service APIs – Lifecycle depends on services used • Fairly Portable – Many Languages, many platforms (pro and con) – Non-Uniform support of Services 17

CORBA and EJB • Transport – EJB uses RMI interface, RMI uses IIOP • CORBA 3. 0 promises object compatibility with EJB – Not quite sure what that means • Some EJB Servers contain an ORB – All EJB Objects are also CORBA objects 18

CORBA and EJB (Cont. ) • All EJB Servers use CORBA Transactions (via JTS) – That means that any client can make a distributed transaction that includes both CORBA and EJB Objects • Not an either-or decision – You can have both EJB and CORBA working together in a single system 19

What’s in a name? • Enterprise Java Beans has absolutely nothing to do with Java. Beans – Except that both are Java-based component architectures • EJB is server-side, JB is client-side – EJB has no GUI, JB usually has GUI • JB is basically naming conventions for fully powered Java classes • EJB rules are much more rigid • EJB classes are less powerful on their own, but more powerful in a container 20

’Tis but thy name that is my enemy • EJB is all about really poor names • Architecture makes sense, object names don’t – – “Home” is a factory “Container” is a helper or wrapper “EJB Object” is an object but is not an EJB "Enterprise Java. Bean" is not a Java. Bean 21

Disadvantages of N-Tier Architecture • Learning curve • $$ cost of products • $$ cost of extra servers to achieve performance • time + $$ cost of maintenance • Object-relational mapping • Basically, app servers are good for large, general solutions (e. g. multiple data sources, multiple applications) 22

EJB Architecture © 1996 -2000 j. Guru. com

EJB Roles • Bean developer: creates JAR with: – – Remote interface with business functions Home for accessing instances of bean Bean itself Properties and descriptor • Assembler: integrates beans into clients • Deployer: modifies and deploys beans – Organization-specifics, ie security 24

EJB Roles (cont. ) • Server provides Server run-time • Container provider: provides tools and containers – Creates container classes that wrap bean – Manage installed beans 25

The EJB Container • Think of the container as the server, holding your objects • Containers host enterprise beans • Containers isolate beans from clients – Intercept client requests – Set up transactions, security, persistence, etc. before and after calling EJB methods – Beans can also call methods of the container interface to get services • Containers provide JNDI services too 26

27

Component API: Interfaces and Classes • Home Interface – Extends javax. ejb. EJBHome – Provides remote access to create, find, remove beans • Remote Interface – Extends javax. ejb. EJBObject – Provides remote access to business methods • Bean Class – Extends a javax. ejb. Enterprise. Bean type – Implements business logic and other functionality 28

EJB Architecture Naming Service Server RMI Home Interface (Factory) Container Client RMI You write this Implements Remote Interface EJB Object (Wrapper) Enterprise Java Bean (Biz Logic) Invokes Creates / uses 29

Bean provider: What to write? • Remote Interface – extend EJBObject interface – Define business method signatures • Home Interface – extend EJBHome interface – Define create signatures – May define find. By signatures for entities 30

What to write (cont. )? • Enterprise Bean Class – – – implement Entity. Bean or Session. Bean Implement business method signatures Does not need to implement Remote Interface not abstract Implement ejb. Create methods matching Home create • 1 ejb. Create for every Home. create • N. B. : “create” in home interface, “ejb. Create” in Bean 31

Remote Interface import javax. ejb. EJBObject; import java. rmi. Remote. Exception; public interface Customer extends EJBObject { public Name get. Name() throws Remote. Exception; public void set. Name(Name name) throws Remote. Exception; public Address get. Address() throws Remote. Exception; public void set. Address(Address address) throws Remote. Exception; } 32

Home Interface import javax. ejb. EJBHome; javax. ejb. Create. Exception; javax. ejb. Finder. Exception; java. rmi. Remote. Exception; public interface Customer. Home extends EJBHome { public Customer create(Integer customer. Number) throws Remote. Exception, Create. Exception; public Customer find. By. Primary. Key(Integer customer. Number) throws Remote. Exception, Finder. Exception; public Enumeration find. By. Zip. Code(int zip. Code) throws Remote. Exception, Finder. Exception; } 33

Bean Class import javax. ejb. Entity. Bean; public class Customer. Bean implements Entity. Bean { Address my. Address; Name my. Name; Credit. Card my. Credit. Card; public Name get. Name() { return my. Name; } public void set. Name(Name name) { my. Name = name; } public Address get. Address() { return my. Address; } public void set. Address(Address address) { my. Address = address; }. . . 34

Types of Beans • Entity Beans – Represent data in the database – Include behavior (business methods) • Session Beans – Represent tasks or processes – Do not represent data, but can access data – Manage the interactions of other beans 35

Home Interface • Defined by Bean developer • Implemented by server tools (autogenerated) • Must extend interface EJBHome – EJBMeta. Data get. EJBMeta. Data() – void remove(Handle ejb. Handle) – void remove(Object primary. Key) • Must provide your own create() methods – Foo create() – Foo create(Bar b, Baz z)… 36

Home Interface: Entity Beans • Entity Beans are persistent, therefore they need more than a “create” method • Need find. XXX methods – public Foo find. By. Primary. Key(Object key); – public Foo find. By. Bar(Bar bar); – public Enumeration find. Overdrawn. Accounts(); • Implement ejb. Find. XXX methods in bean – N. B. : “find” in home interface, “ejb. Find” in Bean 37

Remote Interface • Written by developer • Defines methods accessible by client – Your business methods go here • extends javax. ejb. EJBObject – standard methods provided by all EJBs – get. EJBHome(), get. Primary. Key(), get. Handle(), remove(), is. Identical(EJBObject obj) 38

Remote Interface vs. EJBObject vs. EJB • Developer writes Remote Interface • Tool uses RI to automatically generate the EJBObject class • Developer writes the EJB source file • N. B. : The EJB does not implement the Remote Interface – However, you still must implement all the business methods – Lame-o-rama: no type safety provided by compiler – Better: if EJB tool auto-created a new interface for the EJBObject (oh well) 39

Implementing the EJB • Implements all your business methods • Must also implement – ejb. Create() methods – ejb. Find() methods (for Entity Beans) • Also callback methods – ejb. Remove() – ejb. Activate() – ejb. Passivate() • Implement which interface? – javax. ejb. Session. Bean – javax. ejb. Entity. Bean 40

Interfaces and Implementations Remote Interface deposit() get. Balance() Home Interface create() remove() EJBObject get. EJBHome() get. Primary. Key() get. Handle() is. Identical() remove() deposit() get. Balance() EJB ejb. Create() ejb. Remove() ejb. Activate() ejb. Passivate() deposit() get. Balance() 41

Beans and Persistence © 1996 -2000 j. Guru. com

Persistence • Container-Managed Persistence – Server reads values from your bean – Server stores/loads data for you – Very easy to write - just define data and server magically takes care of persistence 43

Container-Managed Persistence (CMP) • Persistence is automatic – – inserts, updates, and deletes are automatic transactions managed automatically server reads/writes your bean instance variables Server also informs you before/after it's done something • you implement callback methods 44

Container-Managed Persistence (CMP) cont. • Easier to write beans – Focus on business logic, not persistence – But, requires sophisticated, vendor-specific data mapping tools • Components more portable – not backend-specific – shrink-wrapped components 45

Bean-Managed Persistence • Bean-Managed Persistence – You code access database directly – Callback methods dictate when to insert, update, delete • More control over persistence, performance • Much harder to write • Don’t need sophisticated data mapping tools 46

A skeleton of a bean managed persistence bean Public class my. BMPBean implements Entity. Bean { Public void ejbload() { //read data from database } Public void ejbstore() { //save data to a database } Public void ejbcreate() { //insert a record into the database } 47

Public void ejbremote() { //remove a record from a database } Public product findproduct (Integer prod) { //find the specified product } } 48

Entity Beans • Model entities in a system – represent their data and associated behavior • one or many relational database tables • an object in an object database • an entity in a legacy system – Nouns: People, Places or Things • Customer, Employee, Student • City, Building, Hotel Room • Order, Organization, Health Benefit 49

A skeleton of an entity bean Import javax. ejb. Entitybean; Import javax. ejb. Entity Public class myejb implements entitybean{ Int id; Private void ejbpostcreate(); { } Public product getproduct() { Return myproduct; } Public void setproduct(product prod) { Myproduct=prod; 50

} Public void setentitycontext(entitycontext cntx) { } Public void unsetentitycontext() { } Public void ejbload() { } Public void ejbstore() { } Public void ejb. Activate() } } 51

Session Beans • Not persistent, but can access database • Model tasks, process or agents – Charge a credit card – Process a stock purchase – Perform hotel reservations • Manage interactions of other beans 52

A code skeleton of a session bean Import javax. ejb. *; Public class MYEJBBean implements session. Bean{ Public void ejb. Activate() { System. out. println(“called ejbactivate”); } Public void ejb. Remove() { System. out. println(“called ejbremove”); } Public void ejbpassivate() { system. out. println(“called ejbpassivate()”); } 53

Public void setsessioncontex(sessioncontext ctx) { System. out. println(“called setsessioncontext()”); } Public void ejbcreate() { System. out. println(“called ejbcreate()”); } Public string mymethod() { Return(“called mymethod()”); } 54

Stateless Session Beans • Perform transitive tasks • Independent business methods – Act on method arguments, not bean state – Similar to procedures in traditional TPM systems • Most performance bean type • Not dedicated to one client 55

Stateful Session Beans • Act on behalf of one client – Extension of the client – Agent that performs work for the client • Intedependent business methods – Bean has conversational state – Method depend on conversational state • Dedicated to exactly one client 56

EJB Deployment © 1996 -2000 j. Guru. com

Function of a Deployment Descriptor • Describe bean(s) to the container – interfaces and class – type of bean (entity or session) – identify primary key & container-managed fields • Declare runtime behavior – transaction attributes of methods – authorization access to method – persistence type (BMP vs. CMP) • Written in XML 58

EJB Clients © 1996 -2000 j. Guru. com

Types of Clients • • • Enterprise beans J 2 EE Application Components Java Applications Java Applets Servlets JSP 60

Example: J 2 EE Application Component Customer. Home home; Object ref; // Obtain a reference to the Customer. Home ref = jndi. Context. lookup("java: comp/env/ejb/Customer"); // Cast object returned by the JNDI lookup to the // appropriate datatype home = Portable. Remote. Object. narrow(ref, Customer. Home. class); // Use the home interface to create a // new instance of the Customer bean. Customer customer = home. create(customer. ID); // Use a business method on the Customer. customer. set. Name(some. Name); 61

Messaging Beans • New with EJB 2. 0 • Use JMS for asynchronous message queue services • Allows asynchronous response – I will gladly pay you Tuesday for a hamburger today – Get some of the missing Thread functionality back • Not completely supported yet – Must sit on a message queue product 62

Value Objects • Fact: – EJBs are RMI remote objects • Problem: – Calling each setter or getter goes across the network – Slow if you're accessing more than one value at once • Solution: – Value object which mimics the EJB, only it isn't remote – EJB has methods to get and set value object – Client sets/gets values locally, sends/receives en masse 63

Online Resources • j. Guru EJB FAQ – http: //www. jguru. com/faq/EJB • Sun EJB Page – http: //java. sun. com/products/EJB • EJBNow – http: //www. ejbnow. com/ 64

Thanks To • Richard Monson-Haefel • Dave Orchard • Random Walk Computing 65
- Slides: 65