Best way to test RMI connection

below is a static method to check if another RMI server is on the network, I basically call a method that, if it answers true, means that the connection is turned on, if it does not respond and instead gives an exception, it’s wrong. Is there a better way to do this? And is there a better way to speed things up? If there is a connection, it returns with the value quickly, but if it is not required, then time.

public static boolean checkIfOnline(String ip,int port)
{
    boolean online = false;
    try {

    InetAddress ipToConnect;
    ipToConnect = InetAddress.getByName(ip);

    Registry registry = LocateRegistry.getRegistry(ipToConnect.getHostAddress(),port);
    ServerInterface rmiServer = (ServerInterface)registry.lookup("ServerImpl");

    online = rmiServer.checkStudentServerOnline();

    if(online)
    {
        System.out.println("Connected to "+ipToConnect);
    }

    } catch (RemoteException e) {

        System.out.println(e.getMessage());
        //e.printStackTrace();
        return false;
    } catch (NotBoundException e) {
        System.out.println(e.getMessage());
        //e.printStackTrace();
        return false;
    } catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return online;

}
+3
source share
1 answer

The best way to check if a resource is available is to simply try to use it. This will be either successful or unsuccessful, and the error tells you that it is not available (or something else has failed).

, , - . , .

+3

All Articles