简体   繁体   English

使用Java RMI,为什么远程方法调用会产生客户端的时区而不是服务器的时区?

[英]Using Java RMI, why does a remote method call yield the client's timezone instead of the server's?

I'm trying to grasp the basics of Java RMI. 我正在尝试掌握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 client Client作为客户Client运行
  • Server running as a server Server作为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. 第三类Document充当共享对象(它是Strings的简单收集器),其方法addTimestamp()保存当前Timestamp以便在控制台中进一步打印。 This method is also one available as a remote method, as defined in DocumentInterface . 此方法也可用作远程方法,如DocumentInterface所定义。

Executing the two mains on two different cmd windows yields a perfectly functioning system: 在两个不同的cmd窗口上执行两个主电源可以产生一个功能完善的系统:

  • Server : java -cp rmi.jar Server Serverjava -cp rmi.jar Server

  • Client : java -cp rmi.jar Client Clientjava -cp rmi.jar Client

As expected the client's output is: 正如预期的那样,客户的输出是:

CLIENT - Viewed on: 2016.09.30 01.53.01 客户 - 观看日期:2016.09.30 01.53.01

SERVER - Viewed on: 2016.09.30 01.53.01 SERVER - 观看日期: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 Serverjava -Duser.timezone=PST -cp rmi.jar Server

  • Client : java -cp rmi.jar Client Clientjava -cp rmi.jar Client

I still get the original client output: 我仍然得到原始客户端输出:

CLIENT - Viewed on: 2016.09.30 01.53.01 客户 - 观看日期:2016.09.30 01.53.01

SERVER - Viewed on: 2016.09.30 01.53.01 SERVER - 观看日期:2016.09.30 01.53.01

I would expect the second line to have the server's PST-based Timestamp . 我希望第二行有服务器的基于PST的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 2016.09.29 16.55.57

From what I've understood so far, when calling the addTimestamp() method remotely on the remote object: 从我到目前为止所理解的,在远程对象上远程调用addTimestamp()方法时:

  • the current Document is passed by copy to the server 当前Document通过副本传递给服务器
  • the Timezone is appended by the server, using its instance of the Document class Timezone由服务器使用其Document类的实例附加
  • the returned Document is passed by copy back to the client 返回的Document通过副本传递回客户端
  • the Document is displayed by the client Document由客户端显示

In this case I would thus expect that Timezone to be based on the server's settings, not the client's. 在这种情况下,我希望Timezone基于服务器的设置,而不是客户端的设置。 Why is this not the case? 为什么不是这样?

Here are some code snippets from the four classes: 以下是来自四个类的一些代码片段:

Document.java : 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 : DocumentInterface.java

public interface DocumentInterface extends Remote
    {
    public Document addTimestamp(Document document) throws RemoteException;
    }

Server.java - main : Server.java - main

Registry registry = LocateRegistry.createRegistry(1099);
Document document = new Document();
Naming.bind("rmi:///Document", document);

Client.java - main : 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)
    {
    }

Because Document is an exported remote object, it is running as a callback at the client. 由于Document是导出的远程对象,因此它在客户端作为回调运行。 Not by copy at the server. 不是在服务器上复制。

Remove the remote interface and extends UnicastRemoteObject , and make it serializable. 删除远程接口并extends UnicastRemoteObject ,并使其可序列化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM