问题描述
我正在尝试使用Java RMI在分布式系统中实现用于组通信的中间件。
在那里,我需要将一个对象发送到服务器并进行修改。因此,更改应反映在客户端。
例如,我将在Oracle网站上提供最常见的教程。
I'm trying to implement a middle-ware for group communication in a distributed system using Java RMI.
In there, I need to send an object to server and modify it. So that changes should be reflected in the client side.
For example, I will give the most common tutorial in Oracle web site.
X.java
public class X implements Serializable, Remote{
public int x; // defined as public, just be simple.
}
Hello.java
Hello.java
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Hello extends Remote {
String sayHello(X c) throws RemoteException;
}
Server.java
Server.java
import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
import java.rmi.server.UnicastRemoteObject;
public class Server implements Hello {
public Server() {
}
public String sayHello(X c) {
c.x = 10;
return "Hello, world!";
}
public static void main(String args[]) {
try {
Server obj = new Server();
Hello stub = (Hello) UnicastRemoteObject.exportObject(obj, 0);
LocateRegistry.createRegistry(1099);
// Bind the remote object's stub in the registry
Registry registry = LocateRegistry.getRegistry();
registry.bind("Hello", stub);
System.err.println("Server ready");
} catch (Exception e) {
System.err.println("Server exception: " + e.toString());
e.printStackTrace();
}
}
}
Client.java
Client.java
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class Client {
public int x = 4;
private Client() {
}
public static void main(String[] args) {
String host = (args.length < 1) ? null : args[0];
try {
Registry registry = LocateRegistry.getRegistry(host);
Hello stub = (Hello) registry.lookup("Hello");
X x = new X();
String response = stub.sayHello(x);
System.out.println("response: " + x.x);
} catch (Exception e) {
System.err.println("Client exception: " + e.toString());
e.printStackTrace();
}
}
}
我的问题是,即使我在服务器端的对象c中更新 x
的值,它不会反映到客户端。我只需使用 return
来获取更新的值,但我打算使用此方法为服务器实现多播功能。
My problem is, even if I update the value for x
in object c in the server side, it is not reflected to the client side. I simply could use the return
to get the updated value, but I'm planning to use this method to implement a multi-cast function for the server.
任何人都可以解释原因吗?如果我需要这样做,那么它的方法是什么?
Can anyone explain why? And If I need to do so, what is the way of doing it?
推荐答案
当你使用RMI时,你有2个(或者更多)在给定计算中涉及的java虚拟机,因此当您在客户端创建对象并在服务器上调用传递此对象作为参数的方法时,对象的状态将被序列化并通过网络发送。在服务器端,创建同一类的新对象并在其上设置状态,但它是一个具有相同值的不同对象。在此克隆上运行不会反映仍然驻留在源虚拟机上的原始对象。
When you use RMI you are having 2 (or more) java virtual machines involved in a given computation, so when you create an object at the client side and call a method on the server passing this object as an argument the state of the objectis serialized and sent through the network. On the server side a new object of the same class is created and the state is set on it, but it is a different object with the same values in it. Operating on this clone won't reflect on the original object that is still residing on the source virtual machine.
您必须在运行a的每台计算机上使用rmiregistry虚拟机并注册每个对象以显示它(这是一个分布式解决方案),或者您可以将所有数据对象集中在具有rmiregistry的计算机上(这是一个集中式解决方案,因为所有对象都在同一台机器上)。
You would have to use a rmiregistry on each machine that runs a virtual machine and registering each object to expose it (this is a distributed solution) or you could concentrate all the data objects on the machine that has the rmiregistry (this is a centralized solution, since all objects are on the same machine).
干杯
这篇关于Java RMI:更新服务器中的客户端对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!