Reputation: 51
I have a typical client & server situation where I want the client be able to call methods from objects running on the server. I use the Java RMI to solve this situation.
The serverside code is below and it compiles and runs fine:
ServerInt.java
package gpio.control;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface ServerInt extends Remote{
public void setHeizung(boolean x) throws RemoteException;
public void quitError() throws RemoteException;
public void startPWM(int periode,int pulsbreite,int pulsanzahl) throws RemoteException;
}
ServerImpl.java
package gpio.control;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.RemoteServer;
import java.rmi.server.UnicastRemoteObject;
public class ServerImpl implements ServerInt{
private Model myModel;
public ServerImpl(Model m) throws RemoteException {
myModel = m;
LocateRegistry.createRegistry(1099);
ServerInt stub = (ServerInt) UnicastRemoteObject.exportObject(this, 1099);
RemoteServer.setLog(System.out);
Registry registry = LocateRegistry.getRegistry();
registry.rebind("Server1", stub);
}
@Override
public void setHeizung(boolean x) throws RemoteException {
myModel.setHeizung(x);
}
@Override
public void quitError() throws RemoteException {
myModel.quitError();
}
@Override
public void startPWM(int periode, int pulsbreite, int pulsanzahl) throws RemoteException {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
For testing purposes, I also created a small client application which runs on the same server as the server application:
Client.java
package client;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class Client {
public static void main(String[] args) throws RemoteException, NotBoundException {
Registry registry = LocateRegistry.getRegistry();
ServerInt serverint = (ServerInt) registry.lookup("rmi://127.0.0.1:1099/Server1");
serverint.setHeizung(true);
}
}
When I run the Client program now, I get the following errors: Error from Client:
Exception in thread "main" java.rmi.NotBoundException: rmi://localhost:1099/Server1
at sun.rmi.registry.RegistryImpl.lookup(RegistryImpl.java:166)
at sun.rmi.registry.RegistryImpl_Skel.dispatch(Unknown Source)
at sun.rmi.server.UnicastServerRef.oldDispatch(UnicastServerRef.java:410)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:268)
at sun.rmi.transport.Transport$1.run(Transport.java:200)
at sun.rmi.transport.Transport$1.run(Transport.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:196)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:568)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:826)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$241(TCPTransport.java:683)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:682)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:276)
at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:253)
at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:379)
at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)
at client.Client.main(Client.java:12)
Error from server:
Sep 07, 2016 9:47:04 AM sun.rmi.server.UnicastServerRef logCall
FINER: RMI TCP Connection(1)-127.0.0.1: [127.0.0.1: sun.rmi.registry.RegistryImpl[0:0:0, 0]: void rebind(java.lang.String, java.rmi.Remote)]
Sep 07, 2016 9:47:04 AM sun.rmi.server.UnicastServerRef logCall
FINER: RMI TCP Connection(2)-127.0.0.1: [127.0.0.1: sun.rmi.transport.DGCImpl[0:0:0, 2]: java.rmi.dgc.Lease dirty(java.rmi.server.ObjID[], long, java.rmi.dgc.Lease)]
Sep 07, 2016 9:47:05 AM sun.rmi.server.UnicastServerRef logCall
FINER: RMI TCP Connection(3)-127.0.0.1: [127.0.0.1: sun.rmi.registry.RegistryImpl[0:0:0, 0]: java.rmi.Remote lookup(java.lang.String)]
Sep 07, 2016 9:47:05 AM sun.rmi.server.UnicastServerRef logCallException
FINE: RMI TCP Connection(3)-127.0.0.1: [127.0.0.1] exception:
java.rmi.NotBoundException: rmi://localhost:1099/Server1
at sun.rmi.registry.RegistryImpl.lookup(RegistryImpl.java:166)
at sun.rmi.registry.RegistryImpl_Skel.dispatch(Unknown Source)
at sun.rmi.server.UnicastServerRef.oldDispatch(UnicastServerRef.java:410)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:268)
at sun.rmi.transport.Transport$1.run(Transport.java:200)
at sun.rmi.transport.Transport$1.run(Transport.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:196)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:568)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:826)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$241(TCPTransport.java:683)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:682)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
What am I doing wrong here?
Upvotes: 1
Views: 1236
Reputation: 67
In Client.java change
registry.lookup("rmi://127.0.0.1:1099/Server1")
to
registry.lookup("Server1");
Upvotes: 1
Reputation: 311023
As you are using Registry
rather than Naming
to do the lookup, you should omit the URL part of the lookup string, just as you did when binding. You should just lookup Server1
, just as you did when binding.
Upvotes: 0