Java 一个绑定的RMI名称可由多个客户端使用?
我试图在一台(96 Ram)机器上运行3台RMI服务器。来自三个不同机器的客户端正在调用,但对于三个客户端,我给出了三个不同的端口号,并且所有三个客户端的绑定对象名称也不同Java 一个绑定的RMI名称可由多个客户端使用?,java,rmi,Java,Rmi,我试图在一台(96 Ram)机器上运行3台RMI服务器。来自三个不同机器的客户端正在调用,但对于三个客户端,我给出了三个不同的端口号,并且所有三个客户端的绑定对象名称也不同 对于前2个客户机,我得到了输出,对于第1个客户机,我没有得到任何输出。只有“空指针异常”我在客户端得到。在服务器端,我在所有3台服务器上都给出了-Xms250m到-xmx20g。在客户机上,我提供的所有8 GB ram都是-Xmx6g。您需要使用自定义的RMIRegistry端口,以便在同一台机器上运行两个或多个注册表实例。
对于前2个客户机,我得到了输出,对于第1个客户机,我没有得到任何输出。只有“空指针异常”我在客户端得到。在服务器端,我在所有3台服务器上都给出了-Xms250m到-xmx20g。在客户机上,我提供的所有8 GB ram都是-Xmx6g。您需要使用自定义的RMIRegistry端口,以便在同一台机器上运行两个或多个注册表实例。请参阅此完全工作的RMI服务器和客户端示例,您可以运行多个连接到同一远程对象实例的客户端。记住同步服务实现的内部行为
import java.util.*;
import java.rmi.*;
import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
import java.rmi.server.UnicastRemoteObject;
import java.rmi.server.ExportException;
public class RMITest1Client {
private static Random randomInt = new Random();
public static int getRandomInt(int min, int max) {
int range = (max - min) + 1;
return randomInt.nextInt(range) + min;
}
public static void main(String[] args) throws Exception {
String rmiEndPoint = args[0];
String serviceName = args[1];
CounterService counter = (CounterService)Naming.lookup(rmiEndPoint+"/"+serviceName);
System.out.println("Connected to " + rmiEndPoint+"/"+serviceName);
for(int idx=0; idx<10; idx++) {
System.out.println("getValue="+counter.getValue());
Thread.sleep(getRandomInt(1, 5)*1000);
counter.setValue( "val"+getRandomInt(100, 999) );
Thread.sleep(getRandomInt(1, 5)*1000);
}
}
}
- - - - - -
import java.rmi.*;
import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
import java.rmi.server.UnicastRemoteObject;
import java.rmi.server.ExportException;
public class RMITest1Server {
public static void main(String[] args) throws Exception {
// create the RMIregistry service
int port = Integer.parseInt(args[0]);
Registry registry;
try {
System.out.println("RMIRegistry on port " + port);
registry = LocateRegistry.createRegistry(port);
} catch (ExportException ex) {
// registry may already be created by another process,
// get reference to an existing registry instance.
System.out.println("Creating registry failed, try to connect an existing registry, ex="+ex.getMessage());
registry = LocateRegistry.getRegistry(port);
}
CounterService counter = new CounterServiceImpl("counter1");
UnicastRemoteObject.exportObject(counter, port);
registry.rebind("counter1", counter);
counter = new CounterServiceImpl("counter2");
UnicastRemoteObject.exportObject(counter, port);
registry.rebind("counter2", counter);
System.out.println("Running...");
Thread.sleep(30000);
// close registry objects
for(String serviceName : registry.list()) {
try {
System.out.println("RMIRegistry unbind " + serviceName);
Remote obj = (Remote)registry.lookup(serviceName);
UnicastRemoteObject.unexportObject(obj, true);
} catch (Exception ex) { }
try { registry.unbind(serviceName); } catch (Exception ex) { }
}
System.out.println("RMIRegistry closed");
System.exit(0); // mandatory if RMIRegistry was started in this JVM instance
}
}
这是一个在服务器机器上运行的远程服务接口和实现
import java.rmi.*;
public interface CounterService extends Remote {
public void setValue(String value) throws RemoteException;
public String getValue() throws RemoteException;
}
- - - - - -
import java.rmi.*;
public class CounterServiceImpl implements CounterService {
private int callCount=0;
private String name;
private String value;
public CounterServiceImpl(String name) {
this.name=name;
this.value="";
}
public synchronized void setValue(String value) throws RemoteException {
callCount++;
this.value=value;
}
public synchronized String getValue() throws RemoteException {
callCount++;
return String.format("%s (name=%s, callcount=%d)", value, name, callCount);
}
}
下面是一个RMI客户机和服务器实现
import java.util.*;
import java.rmi.*;
import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
import java.rmi.server.UnicastRemoteObject;
import java.rmi.server.ExportException;
public class RMITest1Client {
private static Random randomInt = new Random();
public static int getRandomInt(int min, int max) {
int range = (max - min) + 1;
return randomInt.nextInt(range) + min;
}
public static void main(String[] args) throws Exception {
String rmiEndPoint = args[0];
String serviceName = args[1];
CounterService counter = (CounterService)Naming.lookup(rmiEndPoint+"/"+serviceName);
System.out.println("Connected to " + rmiEndPoint+"/"+serviceName);
for(int idx=0; idx<10; idx++) {
System.out.println("getValue="+counter.getValue());
Thread.sleep(getRandomInt(1, 5)*1000);
counter.setValue( "val"+getRandomInt(100, 999) );
Thread.sleep(getRandomInt(1, 5)*1000);
}
}
}
- - - - - -
import java.rmi.*;
import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
import java.rmi.server.UnicastRemoteObject;
import java.rmi.server.ExportException;
public class RMITest1Server {
public static void main(String[] args) throws Exception {
// create the RMIregistry service
int port = Integer.parseInt(args[0]);
Registry registry;
try {
System.out.println("RMIRegistry on port " + port);
registry = LocateRegistry.createRegistry(port);
} catch (ExportException ex) {
// registry may already be created by another process,
// get reference to an existing registry instance.
System.out.println("Creating registry failed, try to connect an existing registry, ex="+ex.getMessage());
registry = LocateRegistry.getRegistry(port);
}
CounterService counter = new CounterServiceImpl("counter1");
UnicastRemoteObject.exportObject(counter, port);
registry.rebind("counter1", counter);
counter = new CounterServiceImpl("counter2");
UnicastRemoteObject.exportObject(counter, port);
registry.rebind("counter2", counter);
System.out.println("Running...");
Thread.sleep(30000);
// close registry objects
for(String serviceName : registry.list()) {
try {
System.out.println("RMIRegistry unbind " + serviceName);
Remote obj = (Remote)registry.lookup(serviceName);
UnicastRemoteObject.unexportObject(obj, true);
} catch (Exception ex) { }
try { registry.unbind(serviceName); } catch (Exception ex) { }
}
System.out.println("RMIRegistry closed");
System.exit(0); // mandatory if RMIRegistry was started in this JVM instance
}
}
您的问题是什么?因为两个以上的客户端无法同时访问RMI服务器,直到没有问题为止。@AshutoshKumar“两个以上的客户端无法同时访问RMI服务器”为false。你不需要这些。只需使用一个端口和一个远程对象。安全策略文件在客户端和服务器端是必需的吗?@AshutoshKumar No。