Post on 28-Dec-2015
transcript
2
Agenda
• Enterprise JavaBeans defined
• EJB and Distributed Computing
• EJB Architecture
• Entity beans
• Session bean
• Deployment
• EJB Clients
4
What is Enterprise JavaBeans
The Enterprise JavaBeans architecture is a component architecture for the development and deployment of object-oriented distributed enterprise-level applications. Applications written using the Enterprise JavaBeans architecture is scalable, transactional and multi-user secure. These applications may be written once, and deployed on any server platform that supports the Enterprise JavaBeans specification
Sun Microsystems Enterprise JavaBeans™ Specification,
v1.1, Copyright 1999 by Sun Microsystems, Inc.
5
In English Please!
• Enterprise JavaBeans is a specification – (a piece of paper) not a product– As is XML, CORBA, TCP/IP
• Enterprise JavaBeans is Java based– Defined by Sun Microsystems– Applications are written in Java
• Enterprise JavaBeans is distributed objects– Similar to CORBA | Java RMI (JRMP) | Microsoft MTS
• Enterprise JavaBeans is components– Similar to regular JavaBeans (similar but not the same)– Similar to COM/DCOM/COM+
• Enterprise JavaBeans products are Transactional Monitors– Similar to CICS | TUXEDO– A complete environment for a distributed object
6
Enterprise JavaBeans
• 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, WebLogic, ...– Interoperable -- a miracle!
• Why did they cooperate?– See next slide...
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
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 JavaBeans– 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 JavaBean" is not a JavaBean
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)
23
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 provider 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
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.EnterpriseBean type– Implements business logic and other functionality
28
Implements
Invokes
Creates / uses
EJB Architecture
Client
Server
Home Interface(Factory)
EJB Object(Wrapper)
EnterpriseJava Bean(Biz Logic)
RemoteInterface
Container
RMI
RMI
Naming Service
You write this
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 findBy signatures for entities
30
What to write (cont.)?
• Enterprise Bean Class– implement EntityBean or SessionBean– Implement business method signatures– Does not need to implement Remote Interface– not abstract– Implement ejbCreate methods matching Home
create• 1 ejbCreate for every Home.create• N.B.: “create” in home interface, “ejbCreate” in Bean
31
Remote Interface
import javax.ejb.EJBObject;
import java.rmi.RemoteException;
public interface Customer extends EJBObject {
public Name getName() throws RemoteException;
public void setName(Name name) throws RemoteException;
public Address getAddress() throws RemoteException;
public void setAddress(Address address) throws RemoteException;
}
32
Home Interface
import javax.ejb.EJBHome;
import javax.ejb.CreateException;
import javax.ejb.FinderException;
import java.rmi.RemoteException;
public interface CustomerHome extends EJBHome {
public Customer create(Integer customerNumber)
throws RemoteException, CreateException;
public Customer findByPrimaryKey(Integer customerNumber)
throws RemoteException, FinderException;
public Enumeration findByZipCode(int zipCode)
throws RemoteException, FinderException;
}
33
Bean Class
import javax.ejb.EntityBean;
public class CustomerBean implements EntityBean { Address myAddress; Name myName; CreditCard myCreditCard;
public Name getName() { return myName; } public void setName(Name name) { myName = name; } public Address getAddress() { return myAddress; } public void setAddress(Address address) { myAddress = 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– EJBMetaData getEJBMetaData()– void remove(Handle ejbHandle)– void remove(Object primaryKey)
• 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 findXXX methods– public Foo findByPrimaryKey(Object key);– public Foo findByBar(Bar bar);– public Enumeration findOverdrawnAccounts();
• Implement ejbFindXXX methods in bean– N.B.: “find” in home interface, “ejbFind” 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– getEJBHome(), getPrimaryKey(), getHandle(),
remove(), isIdentical(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
– ejbCreate() methods– ejbFind() methods (for Entity Beans)
• Also callback methods– ejbRemove()– ejbActivate()– ejbPassivate()
• Implement which interface?– javax.ejb.SessionBean– javax.ejb.EntityBean
40
Interfaces and Implementations
EJBObjectgetEJBHome()
getPrimaryKey()getHandle()isIdentical()
remove()deposit()
getBalance()
Remote Interfacedeposit()
getBalance()
EJBejbCreate()
ejbRemove()ejbActivate()ejbPassivate()
deposit()getBalance()
Home Interfacecreate()remove()
42
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
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
47
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
48
Stateless Session Beans
• Perform transitive tasks
• Independent business methods– Act on method arguments, not bean state– Similar to procedures in traditional TPM systems
• Most performant bean type
• Not dedicated to one client
49
Stateful Session Beans
• Act on behalf of one client– Extension of the client– Agent that performs work for the client
• Interdependent business methods– Bean has conversational state– Method depend on conversational state
• Dedicated to exactly one client
51
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
53
Types of Clients
• Enterprise beans
• J2EE Application Components
• Java Applications
• Java Applets
• Servlets
• JSP
54
Example: J2EE Application Component
CustomerHome home;
Object ref;
// Obtain a reference to the CustomerHome
ref = jndiContext.lookup("java:comp/env/ejb/Customer");
// Cast object returned by the JNDI lookup to the
// appropriate datatype
home = PortableRemoteObject.narrow(ref, CustomerHome.class);
// Use the home interface to create a
// new instance of the Customer bean.
Customer customer = home.create(customerID);
// Use a business method on the Customer.
customer.setName(someName);
55
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
56
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
57
Online Resources
• jGuru EJB FAQ– http://www.jguru.com/faq/EJB
• Sun EJB Page– http://java.sun.com/products/EJB
• EJBNow– http://www.ejbnow.com/