Java - System time using RMI

DateServerIntf.java


import java.rmi.*;
import java.rmi.server.*;


public interface DateServerIntf extends Remote {
    public String getSystemDate() throws RemoteException;
}





DateServerImpl.java




import java.rmi.*;
import java.rmi.server.*;
import java.util.*;
import java.text.*;

public class DateServerImpl extends UnicastRemoteObject implements DateServerIntf {

    public DateServerImpl() throws RemoteException {
    }

    public String getSystemDate() throws RemoteException {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Date date = new Date();
        return dateFormat.format(date);
    }
}




DateServer.java


import java.rmi.*;
import java.rmi.server.*;

public class DateServer {

    public static void main(String[] args) {
        try {
            DateServerImpl dateServerImpl = new DateServerImpl();
            Naming.rebind("DateServer", dateServerImpl);
        } catch(Exception exp) {
            exp.printStackTrace();
        }
    }
}





DateClient.java




import java.rmi.*;
import java.rmi.server.*;

public class DateClient {

    public static void main(String[] args) {
        try {
            String dateServerURL = "rmi://" + args[0] + "/DateServer";
            DateServerIntf dateServerIntf = (DateServerIntf)Naming.lookup(dateServerURL);
            System.out.println(dateServerIntf.getSystemDate());
        } catch(Exception exp) {
            exp.printStackTrace();
        }
    }
}



c:\rmi\server>java DateServer

c:\rmi\client> java DateClient 127.0.0.1
 
NB: Refer the previous post for directions to run a RMI program.

Post a Comment

0 Comments