350 likes | 502 Views
Overview. Topics: Java Remote Method Invocation (RMI) Enterprise JavaBeans (EJB). Java RMI. distributed object computing de velopment of distributed Java programs same syntax and semantics as in non-distributed programs. Computer A class AClass: myAClassMethod(){}. Computer B
E N D
Overview • Topics: • Java Remote Method Invocation (RMI) • Enterprise JavaBeans (EJB)
Java RMI • distributed object computing • development of distributed Java programs • same syntax and semantics as in non-distributed programs Computer A class AClass: myAClassMethod(){} Computer B class BClass: AClass a; /* initialize a */ a.myClassMethod(); RMI
RMI architecture • The RMI achitecture defines • how objects behave • how and when exceptions can occur • how memory is managed • how parameters are passed to and returned from remote methods
Principle of RMI (1) • RMI separates: • definition of behaviour • implementation of that behaviour • each of them is allowed to run on different JVMs • interfaces: define definition • classes: define implementation
Principle of RMI (2) • 2 classes that implement the same interface • service implementation on server • service proxy on client • client program makes method calls to proxy • RMI sends request to remote JVM • return values are sent back to proxy / client program
RMI architecture layers • 3 abstract layers: • Stubs & SkeletonsLayer • Remote Reference Layer • Transport Layer • advantages of layer architecture: • implementation of layers independent from each other • each layer can be enhanced / replaced without affecting rest of the system
Stubs & Skeletons Layer (1) • lies just beneath developer • intercepts method calls made by client • redirects them to remote RMI service (= remote object) • Proxy Pattern (Book: Design Patterns by Gamma, Helm, Johnson): • one object in one context • represented by another object (the proxy) in another context • proxy forwards method calls between participating objects • in RMI, stub class is the proxy
Stubs & Skeletons Layer (2) proxy represents the RealSubject in RMI: stub class
Stubs & Skeletons Layer (3) • skeleton class: • helper class on server • generated for RMI to use • communicates with stub accross the link • reads parameters for the method call from the link • makes the call to the service object • accepts the return value, writes it back to the stub Server << parameters Client service object client program skeleton network link stub return values >>
Remote Reference Layer • defines & supports the invocation semantics of the RMI connection • client gets remote reference to remote objects via naming service (discussed later) • Remote Reference Layer manages these remote reference • provides RemoteRef object (represents link to remote object) • stub object: forwards method callsbyRemoteRef.invoke() • in JDK1.1: • unicast point-to-point connection • service must be instantiated and exported to the RMI system, before client can use it • in Java 2 SDK: • multicast and other types of connection semantics • activatable objects
Transport Layer • Transport Layer: • stream-based network connections that use TCP/IP • basic connectivity and firewall penetration strategies • on top of TCP/IP, RMI uses Java Remote Method Protocol (JRMP) • RMI-IIOP: instead of JRMP, OMG IIOP is used to communicate between clients and servers
RMI-IIOP • RMI-IIOP: co-production of Sun and IBM • uses Object Management Group Internet Inter-ORB Protocol (OMG IIOP) • OMG: defines a vendor-neutral, distributed object architecture called Common Object Request Broker Architecture (CORBA) • CORBA Object Request Broker (ORB) servers and clients use IIOP for communication • RMI-IIOP: RMI programs can inter-operate with CORBA ORBs • modifications on the source code: RMI programs can be compiled with rmic –iiop to use IIOP • EJB: • based on RMI / RMI-IIOP • communication with CORBA ORBs easier with same protocol
Naming Remote Objects (1) • clients: find remote services by using a naming or directory service, running on a well known host and port number • RMI • can use different directory services, e.g. the Java Naming and Directory Service (JNDI) • includes simple service called RMI Registry (rmiregistry, default on port 1099) • host: server program creates a remote service by • creating a local object that implements the service • exporting that object to RMI • after exporting, server registers object in the RMI Registry under a public name • RMI creates a listening service that waits for client connections and requests for the service
Naming Remote Objects (2) • host (continued): • provides needed class files for client (HTTP, FTP, ...) • client: • RMI Registry is accessed through the static class Naming • client uses its method lookup() to query registry • methodlookup() accepts a URL that specifies the server host name and the name of the desired service • if needed: class files downloaded from server • method returns a remote reference to the service object URLs accepted by Naming.lookup(): rmi://<host_name>[:<name_service_port>]/<service_name>
Using RMI • working RMI system: • interface definitions for the remote services • implementation of the remote services • stub and skeleton files • a server to host the remote services • an RMI Naming service for clients to find the remote services • a class file provider (HTTP or FTP server) • a client program that needs the remote services
Enterprise Java Beans (EJB) • architecture for development of • transactional • 'distributed object applications'-based • server-side software • based on RMI / RMI-IIOP • enterprise beans: • server-side components • distributed objects • short name for such a component: "bean" • hosted in container (Enterprise JavaBeans) • provide remote services for clients distributed thoughout the network
EJB Containers (1) • enterprise beans: software components that run in a special environment called EJB container • container: • hosts and manages an enterprise bean • hosting comparable to • webserver that hosts a servlet • browser that hosts an applet • enterprise bean can't function outside an EJB container • EJB container manages at runtime: • remote access, security • persistence, transactions • concurrency • access to and pooling of resources
EJB Containers (2) • EJB container: isolates enterprise bean from direct access by client application • client invokes a method call on an enterprise bean: • container intercepts invocation • automatically manages persistence, transactions, security • developer doesn't have to care about writing that code • bean doesn't contain any of this logic
EJB Containers (3) • containers: • pool resources, manage life cycles of all beans • reduces memory consumption and processing (scalability) • client application is unaware of this resource management • enterprise bean depends on container for everything: • JDBC connections • connections to other beans • identity of caller • reference to itself
Interaction bean-container (1) • 3 types of interaction enterprise bean EJB container • callback methods • every bean implements a subtype of the EnterpriseBean interface • this i/f defines several "callback methods" • container invokes these methods to • activate a bean • persist its state to the database • end a transaction • remove the bean from memory • ...
Interaction bean-container (2) • EJBContext • object EJBContext: reference to container • provides methods to interact with the container • e.g. request information about the bean's environment: • identity of its client • status of transaction • remote reference to itself • ...
Interaction bean-container (3) • Java Naming and Directory Service (JNDI) • standard extension to the Java platform for accessing naming systens like • LDAP • NetWare • file systens • ... • bean has access to a special naming system Environment Naming Context (ENC) • JNDI ENC allows access to • JDBC connections • other enterprise beans • properties specific to that bean
Enterprise Beans • 2 interfaces + 1 implementation class • client uses interfaces to interact with the bean • interfaces: • remote & home i/f • expose the capabilities of the bean • provide all methods needed to create, update, interact with, delete the bean • implementation class: • represents a business concept (like a addressbook / contact)
Interfaces & types of beans • interfaces: • home interface • remote (or business) interface • entity beans • container managed persistence (CMP) • bean managed persistence (BMP) • session beans • stateful beans • stateless beans
Remote & Home interfaces • home interface: • extends javax.ejb.EJBHome • life cycle methods to create, find and destroy a bean • remote interface: • extends javax.ejb.EJBObject • represents business methods
Life cycle methods • home interface provides life cycle methods • for creating • for destroying • for locating beans • represent behaviours that are not specific to a single bean instance • separated from remote interface /* some imports */ public interface ContactHome extends EJBHome{ public Contact create(int number) throws RemoteException, CreateException; public Contact findByPrimaryKey(int pk) throws FinderException; }
Business methods (1) • can perform tasks • this type of bean is called session bean import javax.ejb.EJBObject; import java.rmi.RemoteException; public interface AddressBook extends EJBObject{ public Contact findByContactData(String name, String firstname, String email) throws RemoteException; public Enumeration findByName(String firstname) throws RemoteException; public Contact findByEmail(String email) throws RemoteException; }
Business methods (2) • can also represent business data stored in a database • this type of business object is called entity bean • each instance of an enterprise bean represents 1 row in a table entity beans database table bean1 bean2 bean3
Entity type beans (1) • example for an entity bean (remote interface): import javax.ejb.EJBObject; import java.rmi.RemoteException; public interface Contact extends EJBObject{ public String getName() throws RemoteException; public String setName(String name) throws RemoteException; public String getFirstName() throws RemoteException; public String setFirstName(String firstname) throws RemoteException; public String getEmailAddress() throws RemoteException; public String setEmailAddress(String emailaddres) throws RemoteException; }
Entity type beans (2) • two types of entity beans: • Container-Managed Persistence (CMP) • Bean-Managed Persistence (BMP) • CMP: • container (EJB server) manages persistence • developer doesn't have to write db access code • programmer has to specify in deployment desciptor which fields (instance variables/columns) are to be managed • BMP: • entity bean contains db access code (via JDBC) • responsible for reading and writing its own state to the database
Session type beans (1) • session beans: • manage interactions of entity and other session beans • access resources • perform tasks on behalf of the client • not persistent business objects, do not represent data in db • interface javax.ejb.SessionBean provides methods ejcActivate() and ejbPassivate() • two types of session beans: • stateless session beans • stateful session beans
Session type beans (2) • stateless beans: • made up of business methods that behave like procedures • operate only on passed through arguments on invocation • do not maintain business state between method invocations • each invocation of a business method is independent from previous invocations • doesn't remember anything from one invocation to the other • faster request processing, less resources used • don't use ejbActivate() and ejbPassivate() methods (overridden with empty implementation)
Session type beans (3) • stateful beans: • encapsulate business logic and state specific to a client • dedicated to one client • clients do not share stateful beans • maintain business state / conversational state between method invocations • held in memory, not persistent
Access to beans • access to beans from outside: • client application (as mentioned) • Java Server Pages (JSP) or Servlets • popularly used method: JSP • HTML pages with embedded Java code • client opens JSP page in browser • server processes page • beans can be used to perform tasks or access database • results are sent back to browser (HTML)
Using EJB • working EJB system: • for each bean • home interface • remote interface • implementation • EJB server (e.g. j2ee) • naming service (usually JNDI) • a class file provider (HTTP or FTP server) • for entity beans: relational database (e.g. cloudscape) • client program