tanszek:oktatas:java_rmi
Simple Java-RMI example
1.) Should we define RObject interfaces, both implements Remore interface
import java.rmi.*;
public interface RObject extends Remote {
// simple parameters
void primitiveArg(int num) throws RemoteException;
// parameters by value
void argumentByValue(Integer num) throws RemoteException;
}
2.) Lets implement the codes of the remote objects
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class RObjectImpl extends UnicastRemoteObject implements RObject {
private static final long serialVersionUID = 6350331764929058681L;
public RObjectImpl() throws RemoteException {
}
@Override
public void primitiveArg(int num) throws RemoteException {
System.out.println(num);
}
@Override
public void argumentByValue(Integer num) throws RemoteException {
System.out.println(num);
}
}
3.) Lets create an instance of the remote object and launch registry and bind to the registry.
import java.rmi.Naming;
import org.ait.RObject;
import org.ait.RObjectImpl;
public class RegisterService {
/**
* @param args
*/
public static void main(String[] args) {
try {
java.rmi.registry.LocateRegistry.createRegistry(1099);
RObject robj = new RObjectImpl();
Naming.rebind("rmi://localhost:1099/RObjectServer", robj);
System.out.println("Registered...");
} catch (Exception e) {
e.printStackTrace();
}
}
}
4.) Use the remote object
import java.rmi.Naming;
public class Client {
public static void main(String[] args) {
try {
// registry binding
RObject robj = (RObject) Naming
.lookup("rmi://localhost:1099/RObjectServer");
robj.primitiveArg(2012);
robj.argumentByValue(new Integer(2012));
} catch (Exception e) {
e.printStackTrace();
}
}
}
tanszek/oktatas/java_rmi.txt · Last modified: 2015/03/23 11:08 by 127.0.0.1

