Universität Bonn, Seminar Softwaretechnologie SS 2003, Duc Luu 1 Topics: Java Remote Method...
-
date post
19-Dec-2015 -
Category
Documents
-
view
216 -
download
1
Transcript of Universität Bonn, Seminar Softwaretechnologie SS 2003, Duc Luu 1 Topics: Java Remote Method...
Universität Bonn, Seminar Softwaretechnologie SS 2003, Duc Luu 1
• Topics:• Java Remote Method Invocation (RMI)• Enterprise JavaBeans (EJB)
Overview
Universität Bonn, Seminar Softwaretechnologie SS 2003, Duc Luu 2
• 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
Java RMI
Universität Bonn, Seminar Softwaretechnologie SS 2003, Duc Luu 3
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
RMI architecture
Universität Bonn, Seminar Softwaretechnologie SS 2003, Duc Luu 4
• 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 (1)
Universität Bonn, Seminar Softwaretechnologie SS 2003, Duc Luu 5
• 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
Principle of RMI (2)
Universität Bonn, Seminar Softwaretechnologie SS 2003, Duc Luu 6
• 3 abstract layers:• Stubs & Skeletons Layer
• Remote Reference Layer
• Transport Layer
RMI architecture layers
• advantages of layer architecture:• implementation of layers independent from each other
• each layer can be enhanced / replaced without affecting rest of the system
Universität Bonn, Seminar Softwaretechnologie SS 2003, Duc Luu 7
• lies just beneath developer• intercepts method calls made by client• redirects them to remote RMI service (= remote object)
Stubs & Skeletons Layer (1)
• 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
Universität Bonn, Seminar Softwaretechnologie SS 2003, Duc Luu 8
proxy represents the RealSubject
in RMI: stub class
Stubs & Skeletons Layer (2)
Universität Bonn, Seminar Softwaretechnologie SS 2003, Duc Luu 9
ClientServer
• 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
service object skeleton stubnetwork link
<< parameters
return values >>
Stubs & Skeletons Layer (3)
client program
Universität Bonn, Seminar Softwaretechnologie SS 2003, Duc Luu 10
• 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 calls by RemoteRef.invoke()• in JDK 1.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
Remote Reference Layer
Universität Bonn, Seminar Softwaretechnologie SS 2003, Duc Luu 11
• 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
Transport Layer
Universität Bonn, Seminar Softwaretechnologie SS 2003, Duc Luu 12
• 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
RMI-IIOP
Universität Bonn, Seminar Softwaretechnologie SS 2003, Duc Luu 13
• 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 (1)
Universität Bonn, Seminar Softwaretechnologie SS 2003, Duc Luu 14
• client:• RMI Registry is accessed through the static class Naming• client uses its method lookup() to query registry• method lookup() 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>
Naming Remote Objects (2)
• host (continued):• provides needed class files for client (HTTP, FTP, ...)
Universität Bonn, Seminar Softwaretechnologie SS 2003, Duc Luu 15
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
Universität Bonn, Seminar Softwaretechnologie SS 2003, Duc Luu 16
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
Universität Bonn, Seminar Softwaretechnologie SS 2003, Duc Luu 17
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
Universität Bonn, Seminar Softwaretechnologie SS 2003, Duc Luu 18
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
Universität Bonn, Seminar Softwaretechnologie SS 2003, Duc Luu 19
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
Universität Bonn, Seminar Softwaretechnologie SS 2003, Duc Luu 20
Interaction bean-container (1)
• 3 types of interaction enterprise bean EJB container
1. 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
• ...
Universität Bonn, Seminar Softwaretechnologie SS 2003, Duc Luu 21
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 (2)
Universität Bonn, Seminar Softwaretechnologie SS 2003, Duc Luu 22
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
Interaction bean-container (3)
Universität Bonn, Seminar Softwaretechnologie SS 2003, Duc Luu 23
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)
Universität Bonn, Seminar Softwaretechnologie SS 2003, Duc Luu 24
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
Universität Bonn, Seminar Softwaretechnologie SS 2003, Duc Luu 25
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
Universität Bonn, Seminar Softwaretechnologie SS 2003, Duc Luu 26
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;}
/* some imports */public interface ContactHome extends EJBHome{
public Contact create(int number) throws RemoteException, CreateException;
public Contact findByPrimaryKey(int pk) throws FinderException;}
Universität Bonn, Seminar Softwaretechnologie SS 2003, Duc Luu 27
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;
}
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;
}
Universität Bonn, Seminar Softwaretechnologie SS 2003, Duc Luu 28
Business methods (2)
• can also represent business data stored in a database• this type of business object is called entity bean
id name firstname email
1 Vega Vincent [email protected]
2 Winfield Jules [email protected]
3 Wolf Winston [email protected]
• each instance of an enterprise bean represents 1 row in a table
database tableentity beans
bean1
bean2
bean3
Universität Bonn, Seminar Softwaretechnologie SS 2003, Duc Luu 29
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;}
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;}
Universität Bonn, Seminar Softwaretechnologie SS 2003, Duc Luu 30
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
Universität Bonn, Seminar Softwaretechnologie SS 2003, Duc Luu 31
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
Universität Bonn, Seminar Softwaretechnologie SS 2003, Duc Luu 32
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)
Universität Bonn, Seminar Softwaretechnologie SS 2003, Duc Luu 33
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
Universität Bonn, Seminar Softwaretechnologie SS 2003, Duc Luu 34
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)
Universität Bonn, Seminar Softwaretechnologie SS 2003, Duc Luu 35
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