Beruflich Dokumente
Kultur Dokumente
Agenda
Enterprise JavaBeans defined EJB and Distributed Computing
EJB Architecture
Entity beans Session bean Deployment EJB Clients
Scalable
transactional multi-user secure
In English Please!
Enterprise JavaBeans is a specification
(a piece of paper) not a product As is XML, CORBA, TCP/IP
Monitors
Similar to CICS | TUXEDO A complete environment for a distributed object
5
Enterprise JavaBeans
Simple Programming Model
Attribute based programming
Portable
Specification ensures basic services
Component Packaging (JARs and XML DD) Java
WORA
Write-Once, Run Anywhere within Middleware Middleware provides all services
Instance management, transactions, concurrency,
persistence
Beans stay simple!
EJB Advantages
With EJB, you can write a business object and
easily make it
Persistent
Distributed Transactional Secure
Multithreaded
Persistent
Beans need to load and store data You can let the server do it
(Container-Managed Persistence)
Transactional
Support for distributed transactions You can let the Server manage all transactions
You will if you know whats good for you
You can give it hints in deployment descriptor
10
Secure
SSL/RMI protects transmitted data Client-Server architecture protects
proprietary code
backend database
11
Multithreaded
Programmer delegates all responsibility for
multithreading to server
Programmer literally cant spawn a thread
12
Naming Services
JNDI Wrapper for many naming services
CORBA, RMI, etc.
13
EJB
Not quite sure what that means
14
EJB Architecture
EJB Roles
Bean developer: creates JAR with:
Remote interface with business functions
components with other software to make a complete application Deployer: modifies and deploys beans in EJB server
Organization-specifics, ie security
16
17
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
18
EJB Provider
Application Assembler
Deployer
Session Beans
Not persistent, but can access database Model tasks, process or agents
Charge a credit card Process a stock purchase Perform hotel reservations
20
21
22
EJB Conversation
Finding the Bean Getting access to a bean
23
with an object) Use JNDI to provide a uniform interface to all naming services or CORBAs COS Naming Services
DNS UNIX /etc/hosts file system
24
interface object
Client uses the home interface to find existing EJB
object
Client uses remote interface to interact with EJB
objects
25
space for other bean Stateless session returns the instance to a pool Entity remove the underlying object from persistent storage ie. database
26
27
expects
Create a manifest file for creating the ejb-jar file (medium to
distribute EJBs)
Deploying EJB Ejb container reads the ejb-jar file Create implementations for the home & remote interfaces Read the deployment descriptor Add the beans property settings to the environment
Connecting to the EJB Either RMI or CORBA to connect to the EJB
28
Written in XML
29
Deployment Descriptor
Name of the EJB class Name of the EJB home interface
method For Entity beans, a list of container-managed fields For session beans, a value denoting stateful or stateless
30
31
EJB Architecture
Naming Service Server
Container
Remote Interface
Creates / uses
Home Interface
extend EJBHome interface Define create signatures May define findBy signatures for entities
33
Remote Interface
import javax.ejb.EJBObject; import java.rmi.RemoteException;
34
Customer interface during deployment time The class passes method invocations to the EJB implementation class Allows the client to do:
Get a reference to the beans home interface (thru
getEJBHome() method) Get a reference that can be saved and then restored (thru getHandle() method) Get rid of the objet (thru remove() method) Test whether two remote objects are identical (isIdentical())
35
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()throws RemoteException, CreateException;
36
container maintain a cache of already instantiated objects Stateless only one create() with no arguments Must be public Return type must be the type of the remote interface Throws clause must include RemoteException and CreateException
37
38
create
One ejbCreate for every Home.create N.B.: create in home interface, ejbCreate in Bean
39
Bean Class
import javax.ejb.*;
public class CustomerBean implements SessionBean { SessionContext ctx; public void ejbCreate() { } public Name getName(String n) { return How are you, + n; } public public public public }
40
void ejbRemove() { } void ejbPassivate() { } void ejbActivate() { } void setSessionContext(SessionContext ctx) { this.ctx = ctx;}
beans state has been restored from semipersistent storage ejbPassivate() called by container before swapping the object out to semi persistent storage ejbRemove() invoked as result of client calling the remove() setSessionContext()
Container passes context object to session bean at
41
the very beginning of beans life Bean stores this object in an object variable Use this to interact with container provided services like security & transaction management
same as the number and type of arguments in the create() method No need of throwing exceptions bcos exceptions are thrown from create() method
42
43
44
import javax.ejb.deployment.*;; import java.io.*; import java.util.Properties; Public class DDWrite { public static void main(String argv[]) { SessionDescriptor sd = new SessionDescriptor(); sd.setEnterpriseBeanClassName(Customer); sd.setHomeInterfaceClassName(CustomerHome); sd.setRemoteInterfaceClassName(CustomerRemote); Properties p = new Properties(); p.put(myprop1, myval0; sd.setEnvironmentProperties(p); sd.setSessionTimeout(0); contd
45
sd.setStateManagementType(SessionDescriptor.STATEL ESS_SESSION) try { FileOutputStream fos = new FileOutputStream (foo.ser); ObjectOutputStream oos = new ObjectOutputStream (fos); oos.writeObject(sd); oos.close(); } catch (IOException ioe) { }
file
Name:directory/ CustomerBeanDD.ser
Enterprise-Bean: True
47
Writing a client(9)
import project directory; import java.rmi.*; import javax.naming.*; Public class HelloClient { public static void main(String argv[]) { try { InitialContext ic = new InitialContext(); CustomerHome home= (CustomerHome) ic.lookup(CustomerHome); Customer cust = home.create(); String name = cust.getName(Suriya); S.O.P. (returned + name); cust.remove(); } catch (java.rmi.RemoteException e) { } catch (javax.ejb.CreateException e) {} catch (javax.ejb.RemoveException e) {} catch (javax.naming.NamingException e) {}} }
48
Home Interface
Defined by Bean developer Implemented by server tools (autogenerated)
49
50
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(),
51
Persistence
Container-Managed
Persistence
54
something
you implement callback methods
55
mapping tools
Components more portable
not backend-specific
shrink-wrapped components
56
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
57
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
58
Online Resources
jGuru EJB FAQ
http://www.jguru.com/faq/EJB
EJBNow
http://www.ejbnow.com/
59
Remote Interface
import javax.ejb.EJBObject; import java.rmi.RemoteException;
60
Home Interface
import import import import javax.ejb.EJBHome; javax.ejb.CreateException; javax.ejb.FinderException; java.rmi.RemoteException;
public interface CustomerHome extends EJBHome { // required in session bean also public Customer create(Integer customerNumber) throws RemoteException, CreateException; //not required in session bean public Customer findByPrimaryKey(Integer customerNumber) throws RemoteException, FinderException;
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; } ... }
62