RMI Registry

Download Report

Transcript 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)
• Introduced to RMI Callbacks
• Next time Java RMI over IIOP
Architecture
Client
lookup
Stub
rmic generated
Server
coded manually
Registry
Interfaces
bind
Skeleton
rmic generated
Activation
Interfaces
RMI Runtime (rmid,rmiregistry)
• RMI registry is light-weight naming service
• Independent proces with RMI interface
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;
}
package examples.hello;
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();
}
Server object
(HelloImpl.java)
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);
System.out.println("HelloServer bound in registry");
} catch (Exception e) {
System.out.println("HelloImpl err: " + e.getMessage());
e.printStackTrace();
}
}
}
Instantiate a new object and register
(bind it) in the ”rmiregistry”
Following methods available:
bind, rebind, unbind, lookup
package examples.hello;
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);
} catch (Exception e) {
System.out.println("HelloApplet exception: " + e.getMessage());
e.printStackTrace();
}
}
}
Client object
(HelloClient.java)
”lookup” the HelloServer – and call
Method sayHello() on Stub
Limitations of RMI Registry
• 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
• DNS usage + load balancer will partly solve this
• Security Restriction: Name bindings cannot be
created from remote hosts
• One Registry pr Server
• There has to be a registry on each host
• No composite names
• Alternatives: make your own or JNDI
Alternative Registry
•
•
Use JNDI:
Java Naming and Directory
Interface
• Standard API for accessing
naming and directory services
(like JDBC to databases)
• Standard in Java: LDAP, RMI
Registry, CORBA Naming
service
• http://java.sun.com/products/jndi/tutori
al/getStarted/overview/index.html
intro to JNDI
• http://java.sun.com/products/jndi/tutori
al/objects/storing/remote.html JNDI
and RMI
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 inconveniences.
• May be solved more elegantly:
• LocateRegistry.createRegistry(PORT);
• And you are up and running, ready to bind remote
objects
• Access via class Naming as usual
// HelloServer.java
import javax.naming.Context;
import javax.naming.InitialContext;
public class HelloServer {
// The main method is used to bootstrap the HelloImpl and register it
// with the Naming Service
public static void main(String[] args) {
try {
BootStrap Server
(HelloServer.java)
// Start with instantiating the Hello servant
HelloImpl hello = new HelloImpl();
// Next we publish the reference in the Naming Service
Context initialNamingContext = new InitialContext();
initialNamingContext.rebind("HelloAppService", hello);
System.out.println("Hello Server is up and running...");
} catch (Exception e) {
System.out.println("An exception occurred: " + e);
}
}
}
We now use InitialContext as the
Naming service
ORBD is the Java CORBA
Naming Service
Starting the server program with
info on the ORBD
Callbacks
• 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
- BUT: turn the client object into a remote object
- Web services
- No support.
Data Collection & Presentation
TRS
New Reading
PSP
View Readings
Server
RMI/CORBA
Object
DB
Classic Client / Sever model is
sufficient for Data Collection & Presentation
Alarm level surveillance
TRS
New Reading
PSP
View Readings
Server
RMI/CORBA
Object
Present Alarm
DB
Problem: The client / server pattern breaks down when
we want to notify FROM the server to the client.
Solution: Client polling OR peer-to-peer model -> e.g. using Callbacks
Issues
•
Distributed Deadlock
•
•
•
•
If client and server are single-threaded
Client calls server, server calls client back instantly
Deadlock – both are blocked, waiting for a response
Solution: Do not make single-threaded applications
• Problem: not all OS’s support multithreading
• Inconvenience : multithreading introduces new complexities
•
Callback Persistence & Keep-Alive
•
•
•
•
Callback Failure
•
•
What happens if server breaks down?
Server should store registered callbacks on persistent storage in case of server
failure
Consider implementing “heart-beat” or “keep-alive”
As callback objects are transient, server should employ a “timeout” strategy for
callback communication
Coupling
•
Callback objects comes at a price of higher coupling