rmiregistry woes

J

jonck

Hi all,
I was fiddling around with RMI, got it working, and then all of a
sudden things stopped working. My main class is as follows (it's an
example from a book):

import java.io.Serializable;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

public class RatingServer extends UnicastRemoteObject implements
RatingServerInterface, Serializable {
public RatingServer() throws RemoteException {
super();
System.out.println("Entering 0-argument constructor");
}

public int serverRating(MovieInterface m) throws RemoteException {
System.out.println("RatingServer asked for a rating");
int s = m.getScript();
int a = m.getActing();
int d = m.getDirection();
return 3 * Math.max(Math.max(s, a), d);
}

public static void main(String args[]) {
try {
Naming.rebind("//localhost/ratingService", new
RatingServer());
System.out.println("Rating server connected to server");
} catch (RemoteException e) {
System.out.println("Ratingserver exception: " + e);
} catch (MalformedURLException e) {
System.out.println("Ratingserver exception: " + e);
}
}
}

I tried reverting back to the state where things still worked (returned
to the exact code as it is mentioned in my book, this code worked
earlier on), but now all of a sudden whenever I tried running my class,
I got the following exception:

Ratingserver exception: java.rmi.ServerException: RemoteException
occurred in server thread; nested exception is:
java.rmi.UnmarshalException: error unmarshalling arguments;
nested exception is:
java.lang.ClassNotFoundException: RatingServer_Stub

Now I know that this would indicate that RatingServer_Stub cannot be
found, but it's on my classpath (same directory! And this exact setup
worked a few minutes back.

What I think caused all my problems was that during a certain launch,
the 0-argument constructor kept getting called, over and over again. In
fact, it did not stop untill I rebooted my computer. After the reboot
things stopped working.

So my guess is that somehow rmiregistry got screwed up. Could someone
tell me how I could go about re-installing it? I'm on OS X 10.3.7.
Thanks very much for any help, Jonck
 
E

Esmond Pitt

jonck said:
Ratingserver exception: java.rmi.ServerException: RemoteException
occurred in server thread; nested exception is:
java.rmi.UnmarshalException: error unmarshalling arguments;
nested exception is:
java.lang.ClassNotFoundException: RatingServer_Stub

Now I know that this would indicate that RatingServer_Stub cannot be
found, but it's on my classpath (same directory! And this exact setup
worked a few minutes back.

It's on *your* classpath but it's not on your *registry*'s classpath.
The error is coming from the registry during Naming.rebind(). You must
have restarted the registry under different conditions. Nothing wrong
with your code.
 
J

jonck

You are correct, thank you! Wish all problems were solved so simply, I
navigated to the directory where the code is located and started
rmiregistry, and voila, after running my class it works again :)
Thanks again, Jonck
 
J

jonck

Perhaps you know the answer to this one as well? If I were to change
the above posted class into this:

public class RatingServer extends UnicastRemoteObject implements
RatingServerInterface, Serializable {
private Object test;

public RatingServer(Object test) throws RemoteException,
MalformedURLException {

Naming.rebind("//localhost/ratingService", new RatingServer());
System.out.println("Rating server connected to server");

setTest(test);
}

public RatingServer() throws RemoteException {
super();
}

public int serverRating(MovieInterface m) throws RemoteException {
System.out.println("RatingServer asked for a rating");
System.out.println(getTest());
int s = m.getScript();
int a = m.getActing();
int d = m.getDirection();
return 3 * Math.max(Math.max(s, a), d);
}

public static void main(String args[]) {
try {
RatingServer ratingServer = new RatingServer(new Object());
} catch (RemoteException e) {
System.out.println("Ratingserver exception: " + e);
} catch (MalformedURLException e) {
System.out.println("Ratingserver exception: " + e);
}
}

public Object getTest() {
return test;
}

public void setTest(Object test) {
this.test = test;
}
}

(so added a 1-argument constructor, which I call in the main method,
and added a field Object test and its getter and setter)

If I now call the serverRating method over RMI the line
"System.out.println(getTest());" produces "null", while obviously test
is not null. I've noticed that it is possible to call primitives like
int or boolean, but any call to a field that extends Object returns
null.

Is this a bug or is this supposed to happen?

Any tips on how to bypass this, I'd like to be able to access
non-primitive fields in the remotely called method.
Thanks very much, Jonck
 
E

Esmond Pitt

Anything passed or returned by a Remote method needs to be either Remote
or Serializable, and ditto anything reachable via non-transient attributes.

new Object() is neither.
 
J

jonck

I figured that Serializable had something to do with it, so I created
another object that was serializable and tried it, same outcome, the
returned value was null. After that I also implemented the Remote
interface, same thing, returned value was null.

So I'm at a loss here. It doesn't really matter, cause I've solved what
I wanted to do with a simple socket connection, but I'm interested in
how I would get this to work nonetheless.
Thanks for any feedback, Jonck
 
E

Esmond Pitt

My guess is that you are *binding* null, there is really no other way
you can get a null from Registry.lookup.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top