package java.rmi.registry

Download Report

Transcript package java.rmi.registry

Presentation:
RMI Continued 2
Using The Registry & Callbacks
Goals of this lesson
• After this 1x35 lessons you will be
– Introduced to the RMI registry (rmiregistry)
• And how to use it
– Introduced to RMI Callbacks
• Next time Java RMI Activation and RMI IIOP
Slide 2 af 12
Ingeniørhøjskolen i Århus
Outline
• Group presentation
– RMI compared with Web services (SOAP)
• Theory: (35 min.)
– Repetition from last time
• The Architecture of Java RMI and the RMI registry
– The concept of Callbacks in Java RMI
• Exercises
– Many had trouble last time
– Complete this
– Make a callback version of HelloWorld
Slide 3 af 12
Ingeniørhøjskolen i Århus
RMI Client and Server Implementation
Hello.java
HelloClient.java
HelloImpl.java
rmic Compiler
HelloImpl_Stub.class
HelloImpl_Skeleton.class
Java compiler - javac
Java compiler - javac
Client
included in
generates
reads
Slide 4 af 12
Server
Ingeniørhøjskolen i Århus
Architecture
Client
lookup
Stub
rmic generated
Server
coded manually
Registry
Interfaces
bind
Skeleton
rmic generated
Activation
Interfaces
RMI Runtime (rmid,rmiregistry)
Slide 5 af 12
Ingeniørhøjskolen i Århus
Naming in RMI: The RMI Registry
• Simplified version of CORBA Naming
• No composite names
• Security Restriction: Name bindings cannot be
created from remote hosts
• There has to be a registry on each host
Slide 6 af 12
Ingeniørhøjskolen i Århus
Naming in RMI: The RMI Registry
package java.rmi.registry;
public interface Registry extends java.rmi.Remote {
public static final int REGISTRY_PORT = 1099;
public java.rmi.Remote lookup(String name)
throws java.rmi.RemoteException,
java.rmi.NotBoundException,
java.rmi.AccessException;
public void bind(String name, java.rmi.Remote obj)
throws java.rmi.RemoteException,
java.rmi.AlreadyBoundException,
java.rmi.AccessException;
public void rebind(String name, java.rmi.Remote obj)
throws java.rmi.RemoteException,
java.rmi.AccessException;
public void unbind(String name)
throws java.rmi.RemoteException,
java.rmi.NotBoundException,
java.rmi.AccessException;
public String[] list()
throws java.rmi.RemoteException,
java.rmi.AccessException;
}
Slide 7 af 12
Ingeniørhøjskolen i Århus
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! ;
}
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);
Instantiate a new object and register
(bind it) in the ”rmiregistry”
Following methods available:
bind, rebind, unbind, lookup
System.out.println("HelloServer bound in registry");
} catch (Exception e) {
System.out.println("HelloImpl err: " + e.getMessage());
e.printStackTrace();
}
}
}
Slide 8 af 12
Ingeniørhøjskolen i Århus
package examples.hello;
Client object
(HelloClient.java)
import java.rmi.Naming;
import java.rmi.RemoteException;
public class HelloClient {
public static void main(String args[])
{
try {
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();
}
}
}
Slide 9 af 12
Ingeniørhøjskolen i Århus
Limitations of Naming
• Limitation of Naming in all approaches: Client
always has to identify the server by name.
obj = (Hello)Naming.lookup("rmi://192.168.1.101/HelloServer");
• Inappropriate if client just wants to use a service
at a certain quality but does not know from who:
– Automatic cinema ticketing,
– Video on demand,
– Electronic commerce.
• Security restriction breaks name location
transparency
Slide 10 af 12
Ingeniørhøjskolen i Århus
Nice feature – bootstrapping the Registry
• As until now, you have been manually starting the
RMI Registry, which is a constant source of errors
and other inconviances.
• May be solved more elegantly:
– LocateRegistry.createRegistry(PORT);
• And you are up and running, ready to bind remote
objects
Slide 11 af 12
Ingeniørhøjskolen i Århus
Callbacks
•
•
•
•
Remember PRJ3 / OBJ?
Sometimes Client/Server is not enough
Publish/Subscribe pattern / Observer
CORBA has support for this
– An ORB is always both client and server
• Java RMI does not have support for this
• Solution:
– Turn the client object into a remote object
Slide 12 af 12
Ingeniørhøjskolen i Århus