I'm trying to grasp the basics of Java RMI.
The idea is to have two separate processes running, each primed by a separate main in one of two classes:
Client
running as a clientServer
running as a server
A third class Document
acts as the shared object (it being a simple collector of Strings
), with its method addTimestamp()
saving the current Timestamp
for further printing in the console. This method is also one available as a remote method, as defined in DocumentInterface
.
Executing the two mains on two different cmd
windows yields a perfectly functioning system:
Server
:java -cp rmi.jar Server
Client
:java -cp rmi.jar Client
As expected the client's output is:
CLIENT - Viewed on: 2016.09.30 01.53.01
SERVER - Viewed on: 2016.09.30 01.53.01
When I start the server under a different timezone though:
Server
:java -Duser.timezone=PST -cp rmi.jar Server
Client
:java -cp rmi.jar Client
I still get the original client output:
CLIENT - Viewed on: 2016.09.30 01.53.01
SERVER - Viewed on: 2016.09.30 01.53.01
I would expect the second line to have the server's PST-based Timestamp
. I checked the flag was set correctly by having it printed directly by the server's main, and it is indeed different:
2016.09.29 16.55.57
From what I've understood so far, when calling the addTimestamp()
method remotely on the remote object:
- the current
Document
is passed by copy to the server - the
Timezone
is appended by the server, using its instance of theDocument
class - the returned
Document
is passed by copy back to the client - the
Document
is displayed by the client
In this case I would thus expect that Timezone
to be based on the server's settings, not the client's. Why is this not the case?
Here are some code snippets from the four classes:
Document.java:
public Document addTimestamp(Document document) throws RemoteException
{
String timestamp = new SimpleDateFormat("yyyy.MM.dd HH.mm.ss").format(new Date());
document.strings.add("Viewed on: "+timestamp);
return document;
}
DocumentInterface.java:
public interface DocumentInterface extends Remote
{
public Document addTimestamp(Document document) throws RemoteException;
}
Server.java - main
:
Registry registry = LocateRegistry.createRegistry(1099);
Document document = new Document();
Naming.bind("rmi:///Document", document);
Client.java - main
:
Document document = new Document();
DocumentInterface remoteDocument;
try
{
remoteDocument = (DocumentInterface) Naming.lookup("rmi:///Document");
document.addString("USER - ");
document.addTimestamp(document);
document.addString("\n");
document.addString("SERVER - ");
document = remoteDocument.addTimestamp(document);
System.out.println(document.toString());
}
catch (Exception except)
{
}