Suggested Solutions To Some Programming Problems
Suggested Solutions To Some Programming Problems
2
Java RMI
Overview – steps for developing Java RMI
Design remote interfaces (identify remote objects, their
roles, what do they take in, what do they produce)
Implementation
Servant: do the actual work – implement the methods
11
Java RMI
In the previous example, the client invokes the
methods at the server to retrieve information, he
can do it from time to time to check whether there
is any update to the information (polling)
Is there any alternative?
Callback: the server informs the client when the
information is updated
Polling vs. Callback
Ask your mom every 5 minutes “Is dinner ready?”
(polling)
Please let me know when dinner is ready (callback)
12
Java RMI
How to implement callback?
Client creates a remote object that implements an interface
containing a method for server to call (callback object)
public interface Callback extends Remote {
void cbMethod(…) throws RemoteException; };
Server provides an operation for interested clients to
14
Suggested Solution to Question 2
Interfaces at the server side:
public interface City2 extends Remote {
String getName() throws RemoteException;
int getPopulation() throws RemoteException;
int getTemperature() throws RemoteException; }
public interface CityFactory extends Remote {
void createCity(String name, int pop, int temp) throws
RemoteException;
City2 lookupCity(String name) throws RemoteException;
void register(Callback cb) throws RemoteException;
void deregister(Callback cb) throws RemoteException; }
15
Suggested Solution to Question 2
Step 2: Design CityFactory servant class (City2
servant class and CityFactory server class remain
unchanged)
public class CityFactoryImpl extends UnicastRemoteObject
implements CityFactory {
private Vector cityList;
private Vector cbList;
17
Suggested Solution to Question 2
public City2 lookupCity(String name)
throws RemoteException { … }
public void register(Callback cb) throws RemoteException {
if (!(cbList.contains(cb)))
cbList.addElement(cb); }
18
Suggested Solution to Question 2
Step 3: Design Callback servant class
import java.rmi.*;
import java.rmi.server.*;
public class CallbackImpl extends UnicastRemoteObject
implements Callback {