Distributed Objects Java Remote Method Invocation Enterprise Java
Distributed Objects Java Remote Method Invocation Enterprise Java Beans 15 - RMI/EJB CSC 407 1
DO Basic Idea distributed object registry A B C clerver/servent A C B 15 - RMI/EJB CSC 407 2
Marshalling Parameters client 15 - RMI/EJB stub skeleton CSC 407 server 3
Marshalling Parameters int result = arithmetic. Server. add. One(1) stub 2 oid, “add. One”, 1 skeleton public int add. One(int x) { return x+1; } 15 - RMI/EJB CSC 407 4
Three Major Standards • CORBA – Common Object Request Broker Architecture – Industry sponsored standard • DCOM – Distributed Component Object Model • Microsoft • from COM from OLE • Java RMI – Remote Method Invocation • all can be made to be inter-operable 15 - RMI/EJB CSC 407 5
Java RMI Client Code public interface Arithmetic. Server extends java. rmi. Remote { public int add. One(int i) throws java. rmi. Remote. Exception; } public class Arithmetic. Client { public static void main(String args[]) throws Exception { Arithmetic. Server = (Arithmetic. Server)java. rmi. Naming. lookup( "rmi: //penny. dhcp/Arithmetic. Server"); System. out. println(as. add. One(1)); } } 15 - RMI/EJB CSC 407 6
Java RMI Server Code public interface Arithmetic. Server extends java. rmi. Remote { public int add. One(int i) throws java. rmi. Remote. Exception; } public class Arithmetic. Server. Impl extends java. rmi. server. Unicast. Remote. Object implements Arithmetic. Server { public Arithmetic. Server. Impl() throws java. rmi. Remote. Exception { super(); } public int add. One(int i) { return i+1; } public static void main(String[] args) throws Exception { java. rmi. Naming. rebind("Arithmetic. Server", new Arithmetic. Server. Impl()); } } 15 - RMI/EJB CSC 407 7
Compilation [CLIENT] % javac Arithmetic. Server. java Arithmetic. Client. java [SERVER] % javac Arithmetic. Server. java Arithmetic. Server. Impl. java % rmic –keep Arithmetic. Server. Impl % javac Arithmetic. Server_Stub. java Arithmetic. Server_Skel. java 15 - RMI/EJB CSC 407 8
Generated Stub Code public final class Arithmetic. Server. Impl_Stub extends Remote. Stub implements Arithmetic. Server, Remote { private static final java. rmi. server. Operation[] operations = { new java. rmi. server. Operation("int add. One(int)") }; private static final long interface. Hash = 2100571976616716783 L; public int add. One(int param_int_1) throws java. rmi. Remote. Exception { java. rmi. server. Remote. Call call = super. ref. new. Call( (java. rmi. server. Remote. Object) this, operations, 0, interface. Hash); java. io. Object. Output out = call. get. Output. Stream(); out. write. Int(param_int_1); super. ref. invoke(call); int result; java. io. Object. Input in = call. get. Input. Stream(); result = in. read. Int(); ref. done(call); return result; } } 15 - RMI/EJB CSC 407 9
Generated Skeleton Code public final class Arithmetic. Server. Impl_Skel implements java. rmi. server. Skeleton { public void dispatch(Remote obj, Remote. Call call, int opnum, long hash) { if (hash != interface. Hash) throw new Skeleton. Mismatch. Exception ("interface hash mismatch"); Arithmetic. Server. Impl server = (Arithmetic. Server. Impl) obj; switch (opnum) { case 0: // add. One(int) { int param_int_1; java. io. Object. Input in = call. get. Input. Stream(); param_int_1 = in. read. Int(); call. release. Input. Stream(); int $result = server. add. One(param_int_1); java. io. Object. Output out = call. get. Result. Stream(true); out. write. Int($result); break; } default: throw new Unmarshal. Exception(“invalid method number”); } } 15 - RMI/EJB CSC 407 10
Execution % rmiregistry & % httpd & % java -Djava. security. policy=my. Server. Policy. prop -Djava. rmi. server. codebase=http: //peny. dhcp/Time. Server/ Time. Server. Impl. class Time. Server. class % rmic Time. Server. Impl_Stub. class Time. Server. Impl_Skel. class % java -Djava. security. policy=my. Client. Policy. prop Time. Client. class 15 - RMI/EJB CSC 407 11
Performance • Latency: arithmetic. Server. add. One(1); – Local method calls • . 07 usec – Remote method call (same machine) • 656 usec – Remote method call (network) • 2000 usec • DB access • 1600 usec 15 - RMI/EJB CSC 407 12
Enterprise Java Beans • Component Object Model – – Distributed Persistent Secure Transactional • ACID – Atomicity: all or none – Consistency: database will always be in a consistent state – Isolation: intermediate state not visible until completed – Durability: when completed, the changes are stored permanently • EJBs are a standard – allows application developers to write simple, standard code – allows implementers to get all the underlying stuff done well 15 - RMI/EJB CSC 407 13
EJB Architecture EJB Server EJB Container Client EJB Home Interface EJB Remote Interface 15 - RMI/EJB CSC 407 EJB Bean 14
Context public interface javax. ejb. EJBContext { public abstract Identity get. Caller. Identity(); public abstract EJBHome get. EJBHome(); public abstract Properties get. Environment(); public abstract boolean get. Rollback. Only(); public abstract User. Transaction get. User. Transaction(); public abstract boolean is. Caller. In. Role(Identity role); public abstract void set. Rollback. Only(); } 15 - RMI/EJB CSC 407 15
Types of EJBs • Two types of beans: –Session bean • encapsulates transactional operations • stateful/stateless –Entity bean • encapsulates persistent state • container-managed persistence / bean-managed persistence 15 - RMI/EJB CSC 407 16
EJBs • Remote Interface public interface Grocery. Order extends javax. ejb. EJBObject { public Date get. Date() throws Remote. Exception; public void set. Date() throws Remote. Exception; … } • Home Interface public interface Grocery. Order. Home extends javax. ejb. EJBHome { public Grocery. Order create(int id) throws Create. Exception, Remote. Exception; public Grocery. Order find. By. Primary. Key(Grocery. Order. PK pk) throws Finder. Exception, Remote. Exception; } 15 - RMI/EJB CSC 407 17
EJB Implementation Class public class Grocery. Order. Bean implements javax. ejb. Entity. Bean { public int id; public Date date; public public public void Date void void ejb. Create(int id) { this. id = id; } ejb. Post. Create(int id) { } get. Date() { return date; } set. Date(Date date) { this. date = date; } set. Entity. Context(Entity. Context ctx) { } unset. Entity. Context() { } ejb. Activate() { } ejb. Passivate() { } ejb. Load() { } ejb. Store() { } ejb. Remote() { } } 15 - RMI/EJB CSC 407 18
Session Beans public class Shopper. Bean implement javax. ejb. Session. Bean { public Customer customer; public Gorcery. Order order; public void ejb. Create(Customer cust) { customer = cust; } public Receipt process. Order(Credit. Card card) throws Remote. Exception, Incomplete. Conversational. State , Bad. Credit { if(customer==null||order==null) throw new Incomplete. Conversational. State (); Process. Order. Home poh = (Process. Order. Home)get. Home(“Process. Order. Home”); Process. Order po = poh. create(customer, order); Process. Payment. Home pph = (Process. Payment. Home)get. Home(“Process. Payment. Home”); Process. Payment pp = pp. Home. create(); pp. by. Credit. Card(customer, card, order. price()); po. process(); Receipt r = new Receipt(customer, order, card); return r; } 15 - RMI/EJB CSC 407 19
EJB Summary • Transparent – Distribution • ejb can be anywhere – Replication & Load-Balancing • ejb can be moved around • ejb can be replicated (e. g. , Toronto – London) – Resource Management • ejb shells can be reused • persistent data can be cached – Persistence Management • ejb automatically mapped to persistent storage – Transaction Management • session beans mapped to transactional system – Security • Identities, roles, access control lists 15 - RMI/EJB CSC 407 20
EJB Implementations • Still pretty flaky and none support everything on the previous list. – – – – – Web. Logic EJBHome Sapphire. Web BEA Gemstone IBM CICS/EJB, Component. Broker, Web. Sphere Net. Dynamics Oracle Application Server … 15 - RMI/EJB CSC 407 21
- Slides: 21