Methods call using RMI in JAVA

Asked

Viewed 73 times

1

I’m starting to work with remote procedures in JAVA and I’m having trouble calling methods in sequence. In the code below, when I invoke the sayHello() method through the stub, the code works perfectly. However, when I call the sub() method, an error sequence is displayed. Someone could instruct me how to call other methods?

Client.java:

public class Client {

    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");

            /*
             * String response = stub.sayHello();     
             * System.out.println("response: " + response);
             */

            float result = stub.sub(10, 8);
            System.out.println(result);

        } catch (Exception e) {
            System.err.println("Client exception: " + e.toString());
         e.printStackTrace();
     }
}

Java server.:

    public class Server implements Hello {

    public Server() {
    }

    public String sayHello() {
        return "Hello, world!";
    }

    public float sum(float a, float b) {
        return a + b;
    }

    public float div(float a, float b) {
        if (b != 0) {
            a = a / b;
        } else {
            System.out.println("DIVISAO POR ZERO!");
            a = 0;
        }
        return a;
    }

    public float mult(float a, float b) {
        return a * b;
    }

    public float sub(float a, float b) {
        return a - b;
    }

    public static void main(String args[]) {

        try {
            Server obj = new Server();
            Hello stub = (Hello) UnicastRemoteObject.exportObject(obj, 0);

            // 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();
        }
    }
}

Hello.java:

public interface Hello extends Remote {
    float sum(float a, float b) throws RemoteException;

    float div(float a, float b) throws RemoteException;

    float mult(float a, float b) throws RemoteException;

    float sub(float a, float b) throws RemoteException;

    String sayHello() throws RemoteException;
}

Terrifying error message:

Client exception: java.rmi.ServerException: RemoteException occurred in server thread; nested exception is: 
    java.rmi.UnmarshalException: unrecognized method hash: method not supported by remote object
java.rmi.ServerException: RemoteException occurred in server thread; nested exception is: 
    java.rmi.UnmarshalException: unrecognized method hash: method not supported by remote object
    at java.rmi/sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:391)
    at java.rmi/sun.rmi.transport.Transport$1.run(Transport.java:200)
    at java.rmi/sun.rmi.transport.Transport$1.run(Transport.java:197)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at java.rmi/sun.rmi.transport.Transport.serviceCall(Transport.java:196)
    at java.rmi/sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:562)
    at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:796)
    at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:677)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:676)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1135)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
    at java.base/java.lang.Thread.run(Thread.java:844)
    at java.rmi/sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:283)
    at java.rmi/sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:260)
    at java.rmi/sun.rmi.server.UnicastRef.invoke(UnicastRef.java:161)
    at java.rmi/java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(RemoteObjectInvocationHandler.java:209)
    at java.rmi/java.rmi.server.RemoteObjectInvocationHandler.invoke(RemoteObjectInvocationHandler.java:161)
    at com.sun.proxy.$Proxy0.sub(Unknown Source)
    at Client.main(Client.java:28)
Caused by: java.rmi.UnmarshalException: unrecognized method hash: method not supported by remote object
    at java.rmi/sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:324)
    at java.rmi/sun.rmi.transport.Transport$1.run(Transport.java:200)
    at java.rmi/sun.rmi.transport.Transport$1.run(Transport.java:197)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at java.rmi/sun.rmi.transport.Transport.serviceCall(Transport.java:196)
    at java.rmi/sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:562)
    at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:796)
    at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:677)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:676)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1135)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
    at java.base/java.lang.Thread.run(Thread.java:844)
  • Have you ever tried to clean the project and recompile it? Sometimes you made some changes to the interface, but some class of the project still got an earlier version and then the error. The same I would say of stubs. There may be some compilation conflict and then the system does not recognize its method.

  • The problem is that I needed to generate the project novamento stub every time I made any changes to the interface. Fortunately, I was able to solve.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.