多个客户端可以使用一个绑定的 RMI 名称吗? [英] One bound RMI name can be used by Multiple client?

查看:66
本文介绍了多个客户端可以使用一个绑定的 RMI 名称吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在单台(96 Ram)机器上运行 3 个 RMI 服务器.来自三个不同机器的客户端正在调用,但是对于三个客户端,我给出了 3 个不同的端口号,并且所有三个客户端的绑定对象名称也不同.

I am trying to run 3 RMI servers on single (96 Ram) machine. From three different machine clients are calling, but for three clients I have given 3 different port numbers and binded object name are also different for all three client.

对于第 1 个 2 客户端,我得到了输出,而对于第一个 2 客户端,我没有得到任何输出.只有空指针异常"我在客户端.在服务器端,我在所有 3 个服务器上都给了 -Xms250m 到 -Xmx 20g.在客户端都有 8 GB 内存,我给了 -Xmx6g.

For 1st 2 client I got the output and for thirst one I am not getting any output. Only "Null Pointer exception" I am getting on client side. On Server side I have given -Xms250m to -Xmx 20g on all 3 server. On client all have 8 GB ram I have given -Xmx6g.

推荐答案

您需要使用自定义的 RMIRegistry 端口才能在同一台机器上运行两个或多个注册表实例.请参阅这个完全工作的 RMI 服务器和客户端示例,您可以运行多个客户端连接到同一个远程对象实例.请记住同步服务实现的内部行为.

You need to use a customized RMIRegistry port to have two or more registry instances run in a same machine. See this fully working RMI Server and Client example, you can run multiple clients connecting to the same remote object instance. Remember to synchronize internal behaviour of the service implementation.

这是在服务器机器上运行的远程服务接口和实现.

Here is a remote service interface and implementation run on server machine.

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 客户端和服务器实现.

Here is a RMI client and server implementation.

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
    }

}

这里是运行服务器和客户端的测试脚本.

Here are test scripts to run server and client.

**goServer.bat**
@SET /p port=Server port (2222, 3333, 0=exit): 
@IF "0"=="%port%" GOTO :EOF
java -cp "./lib/*;" RMITest1Server %port%
pause

- - - - - -

**goClient.bat**
@SET /p port=Server port (2222, 3333, 0=exit): 
@IF "0"=="%port%" GOTO :EOF
@SET /p service=Service index (1,2): 
java -cp "./lib/*;" RMITest1Client rmi://127.0.0.1:%port% counter%service%
pause

这篇关于多个客户端可以使用一个绑定的 RMI 名称吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆