gpt4 book ai didi

Java RMI 对来自客户端本身的客户端变量和来自服务器的远程调用的客户端变量有不同的实例化

转载 作者:行者123 更新时间:2023-11-30 11:12:06 26 4
gpt4 key购买 nike

我们正在尝试使用 Javas RMI 创建一个系统。问题是无法使用 Java RMI 从服务器访问客户端上维护的列表。看起来 RMI 连接正在处理初始化列表的副本。

下面是一个使用整数的最小示例,客户端每秒递增一个整数,直到它等于 10。尽管如此,服务器始终接收到 0。

有人知道我们做错了什么吗?

只需将服务器和客户端作为 java 应用程序运行即可。

ServerDefaultImpl.java

package rmi;

import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;

public class ServerDefaultImpl implements EIServerRemote, Runnable {
ClientRemote client;
private boolean running = true;

public ServerDefaultImpl() {
try {
LocateRegistry.createRegistry(Registry.REGISTRY_PORT);

ServerDefaultImpl server = this;
EIServerRemote stub = (EIServerRemote) UnicastRemoteObject.exportObject(server, 0);

Registry registry = LocateRegistry.getRegistry();
registry.rebind("test", stub);
} catch (RemoteException e) {
e.printStackTrace();
}
new Thread(this).start();
}

public static void main(String[] args) {
new ServerDefaultImpl();
}

@Override
public void run() {
while (true == running) {
try {
Thread.sleep(1000);
if (null != client) { //Client not connected yet.
int test = client.test();
System.out.println(test);
running = test <= 10;
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

@Override
public void attachClientListener(ClientRemote client) throws RemoteException {
this.client = client;
}
}

EIServerRemote.java

package rmi;

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface EIServerRemote extends Remote {
void attachClientListener(ClientRemote client) throws RemoteException;
}

ClientRemote.java

package rmi;

import java.io.Serializable;
import java.rmi.Remote;

public interface ClientRemote extends Remote,Serializable {
int test();
}

ClientDefaultImpl.java

package rmi;

import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

public class ClientDefaultImpl implements Runnable,
ClientRemote {

private static final long serialVersionUID = 4846141863099303590L;

protected EIServerRemote server = null;

public int test;

public boolean running = true;

public ClientDefaultImpl(String serverName) {
test = 0;
try {
connect(serverName);
} catch (RemoteException | NotBoundException e) {
e.printStackTrace();
}
new Thread(this).start();
}

public static void main(String[] args) {
new ClientDefaultImpl("test");
}

public void connect(String serverName) throws RemoteException,
NotBoundException {
Registry registry = LocateRegistry.getRegistry();
EIServerRemote s = (EIServerRemote) registry.lookup(serverName);
server = s;
s.attachClientListener((ClientRemote) this);
}

@Override
public void run() {
while (true == running) {
try {
Thread.sleep(1000);
System.out.println(test++);
running = test <= 10;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

@Override
public int test() {
return test;
}
}

最佳答案

It seems that the RMI connection is handling a copy of the initialized list.

没错。该列表不是远程对象,因此它通过序列化传递和返回。

关于Java RMI 对来自客户端本身的客户端变量和来自服务器的远程调用的客户端变量有不同的实例化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27059033/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com