Transcript Java/RMI

Presentation 18:
RMI introduction
Goals of this lesson
• After this 35 min. lesson you will be:
• Introduced to Java RMI
• Ready to present RMI’s position in the Middleware
technology family on class
• You will not:
• Be an RMI expert. More practice and theory is required
• Later in RMI:
• We will look at more advanced issues
• Activation, Callbacks, RMI over IIOP, tunneling
Outline
• Theory: (35x2 min.)
• Introduction to Java RMI
• Group work: (35 min.)
• Pro’s & con’s of Java RMI vs. SOAP
• When to use which technology…
• Differences and equalities SOAP & RMI
• Plenum: (35 min.)
• Discussion of group work
• One group will present … so prepare!
Java RMI
• In Java 1.0 object communication was confined to
objects in one Virtual Machine (VM)
• Sun decided to introduce inter VM communication
• Remote Method Invocation (RMI) from Java 1.1
supports communication between different VMs,
potentially across the network
• Provides tight OO integration with Java
• Work in heterogeneous environment (servers)
• BUT ONLY with Java (so far) – so no language
transparency
Java RMI features
• Build on Java’s existing object model -> easy
• No need for IDL – use Java interfaces
• Arguments & return values can be all types specializing
java.io.Serializable or java.rmi.Remote
• Dynamic loading of classes
• Use of build-in Java Security Manager
• Distributed Garbage Collection
• Integrates with CORBA (later)
• BUT NOT IN J2ME!!! (use SOAP)
• J2ME CDC has an RMI profile!
Java RMI position Middleware
•Transaction-Oriented
•IBM CICS
•BEA Tuxedo
•Encina
•Message-Oriented
•IBM MQSeries
•DEC Message Queue
•NCR TopEnd
•(SOAP)
•RPC Systems
•ANSA
•Sun ONC
•OSF/DCE
•(SOAP)
•Object-Oriented
•OMG/CORBA
•DCOM
•Java/RMI
•(SOAP)
Wire Protocol
• Java RMI wire protocol:
• JRMP (Java Remote Method Protocol) OR
• IIOP (Internet Inter-ORB Protocol) for CORBA
connectivity
• Both build on top of TCP/IP
• JRMP more advanced than IIOP
• Other Java RMI specification implementors
•
•
•
•
Historic: BEA Weblogic, Object Voyager, NinjaRMI
Object Voyager’s was JRMP compatible
Others were not
IIOP compatibility can not be guaranteed
Local Java call vs. Java RMI call
Similar to SOAP and CORBA – using Proxy
Caller
Caller
Caller
Called
Stub
Called
Stub
Transport Layer (e.g. TCP or UDP)
Development Steps
– RMI & CORBA & SOAP
CORBA
AXIS
SOAP
Design
J2SE JDK
Java2WSDL
Start with Server
Interface Coding: JAVA
Server Stub
Generation
Interface
Definition
CORBA: IDL
SOAP: WSDL
RMI: JAVA interface
WSDL2JAVA
CORBA: IDLC
Client Stub
Generation
RMI: rmic
C++, Java …
Server
Coding
Server
Registration
Client
Coding
ORB
rmiregistry
RMI: JAVA
C++, Java …
Stub Generation
- in SOAP & CORBA
Team.wsdl
Team.idl
WSDL-compiler
Teamcl.hh
Teamsv.hh
Teamcl.cc
Teamsv.cc
included in
generates
reads
Stub Generation in Java RMI
Hello.java
rmic Compiler
NOTE: In fact, it is the
HelloImpl that is used!
From Java v. 1.5 no
rmic comp is needed
From RMI v. 1.2 no
skeleton is generated
HelloImpl_Stub.class
HelloImpl_Skeleton.class
package examples.hello;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Hello extends Remote {
Must Extend from
String sayHello() throws RemoteException;
Interface Remote
void someOther(String param) throws RemoteException;
}
RMI Client and Server
Implementation
Hello.java
HelloClient.java
HelloImpl.java
rmic Compiler
HelloImpl_Stub.class
Java compiler - javac
Client
HelloImpl_Skeleton.class
Java compiler - javac
included in
generates
reads
Server
package examples.hello;
Server object
(HelloImpl.java)
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.RMISecurityManager;
import java.rmi.server.UnicastRemoteObject;
public class HelloImpl extends UnicastRemoteObject implements Hello {
public HelloImpl() throws RemoteException {
super();
}
public String sayHello() {
return "Hello World! ;
}
Extend UnicastRemote
and implemet Hello Interfacet
Implement all methods
from interface Hello.java
public static void main(String args[]) {
// Create and install a security manager
//if (System.getSecurityManager() == null) {
// System.setSecurityManager(new RMISecurityManager());
//}
try {
HelloImpl obj = new HelloImpl();
// Bind this object instance to the name "HelloServer"
Naming.rebind("rmi://192.168.1.101/HelloServer", obj);
Security manager needs a security policy
– for access control (i.e. file system).
Instantiate a new object and register
(bind it) in the ”rmiregistry”
System.out.println("HelloServer bound in registry");
} catch (Exception e) {
System.out.println("HelloImpl err: " + e.getMessage());
e.printStackTrace();
”rmiregistry” is a simpel name server with
}
}
}
methods to bind objects (bind/rebind) – and
Find them again (lookup) –> client
package examples.hello;
import java.rmi.Naming;
import java.rmi.RemoteException;
Client object
(HelloClient.java)
public class HelloClient {
public static void main(String args[])
{
try {
Hello obj = (Hello)Naming.lookup("rmi://192.168.1.101/HelloServer");
String message = obj.sayHello();
System.out.println(message);
”lookup” the HelloServer – and call
Method sayHello() on Stub
} catch (Exception e) {
System.out.println("HelloApplet exception: " + e.getMessage());
e.printStackTrace();
}
}
}
AND THAT’S IT!
Remember – that the stub
and skeleton classes get generated
by the ”rmic” compiler
Architecture
Client
lookup
Stub
rmic generated
Server
coded manually
Registry
Interfaces
bind
Skeleton
rmic generated
Activation
Interfaces
RMI Runtime (rmid,rmiregistry)
Things to Remember
• No attributes / properties in Java Interfaces
-> RMI does not support attributes
• Attributes must be represented as set and get
operations by the designer
Things to remember II
• Parameter passing different than normal Java in
single VM
• Atomic types are passed by value
• Remote objects are passed by reference
• Non-Remote objects are passed by value!
• Reflexive: can return references to other objects
• And of course – if an object is not on the client – the
ByteCode gets transferred (the class incl.
implementation) – if a codebase is defined
Key Points
•
•
•
•
•
•
•
•
True and beautiful OO Middleware
Easy to learn – for Java developers
No need for a separate IDL (use Java Interfaces)
Distributed Garbage Collection
ByteCode transfers automatically (if codebase is defined)
Works in heterogene environments – but only with Java
No build-in services (except for the registry)
Depends on other API’s – JavaSpaces, JINI, JDBC, EJB, JDO
etc. – integrated into a framework
• Not ”firewall friendly”
Exercise
• Discuss for 20 minutes:
•
•
•
•
•
•
•
•
At your table 2 and 2
How Access Transparent is Java RMI?
How Location Transparent?
How about the other levels of transparency?
How is Heterogenity supported?
Where might Java RMI be used?
What deployment issues do you see?
Strengths & Weakness’ compared to Web services
• Plenum:
• Let’s discuss your results
Excerice 2
• Discuss for 15 minutes
• In your assignment groups:
• Map out and discuss how you may extend your code
design from assignment 2 – to include a Java RMI
façade (server-side)
• Discuss what you should change in your Java clients to
use Java RMI as well as Web services
• Discuss how you might get your .NET or C++ client to
communicate with Java RMI
• Plenum:
• Let’s discuss your results