Creating new remote objects with RMI

A

acamposr

Hello I know the basic things about RMI: a server object placed into
Naming and a client which can access to the server via
Naming.lookup("ServerName").

But now I want to create new remote objects and use then from the
client.

That is:

/*** Client: ***/

// First
MainServer mainServer = (MainServer) Naming.lookup("MainServer");

// Second
Session mySession = mainServer.newSession(); // mySession is a remote
object, like mainServer
....
mySession.executeTask(task); // This code runs on the server side.

How can I do that? I want to get remote objects not always calling
Naming.lookup, but getting them from another remote object.

/*** Server ****/

public class MainServerImpl extends RemoteObject implements MainServer
{

Session newSession() {
// ?????????????
}

public static void main(String[] args) {
...
Naming.rebind("//localhost/MainServer",new
MainServerImpl());
}
}

public class SessionImpl extends RemoteObject implements Session {
....
}
 
A

acamposr

I have already found how to do it. I'll write it here to help anyone
with the same problem:

public class SessionImpl extends UnicastRemoteObject implements Session
{
....
}

public class MainServerImpl extends UnicastRemoteObject implements
MainServer
{

Session newSession() {
// ?????????????

Session s = new Session()
return (Sesion) UnicastRemoteObject.toStub(s);

}

public static void main(String[] args) {
...
Naming.rebind("//localhost/MainServer",new
MainServerImpl());
}

}
 
T

Thomas Fritsch

I have already found how to do it. I'll write it here to help anyone
with the same problem:

public class SessionImpl extends UnicastRemoteObject implements Session
{
...
}

public class MainServerImpl extends UnicastRemoteObject implements
MainServer
{

Session newSession() {
// ?????????????

Session s = new Session()
return (Sesion) UnicastRemoteObject.toStub(s);
I doubt that the above will work. I expect a compiler error ("Session is
an interface; cannot be instantiated" or similarly) on your line
Session s = new Session();
Instead I would use:
Session s = new SessionImpl();
return s;
}

public static void main(String[] args) {
...
Naming.rebind("//localhost/MainServer",new
MainServerImpl());
}

}
 
A

acamposr

It is like this:

Session s = new SessionImpl();
return s.toStub();

Just a mistake typing it here.
 
A

acamposr

Arggg, another mistake. Finally it is like this:


Session s = new SessionImpl();
return UnicastRemoteObject.toStub(s);
 
E

EJP

Arggg, another mistake. Finally it is like this:


Session s = new SessionImpl();
return UnicastRemoteObject.toStub(s);

Actually

return s;

will do just as well and it's simpler. RMI takes care of mapping impls
to stubs during marshalling.
 
A

acamposr

Actually
return s;
will do just as well and it's simpler. RMI takes care of mapping impls
to stubs during marshalling.

Thanks for that information. Now I have another 2 question about RMI.

1. When the client program ends up, does the garbage collector delete
the exported object 's' form the server?

2. I am getting NoSuchObjectException some times when returning 's'. I
just have to repeat the operation and it works fine. What's wrong with
that?

Thanks!!
 
E

EJP

1. When the client program ends up, does the garbage collector delete
the exported object 's' form the server?

No. When the *last* client exits, the server's
Unreferenced.unreferenced() method will be called (if it implements
Unreferenced), and if there are no other *local* references to the
server it can then be GC'd. When and if GC happens the server will be
unexported.

Note that for this purpose the Registry is a client too so if a server
is bound in the Registry none of this will happen.
2. I am getting NoSuchObjectException some times when returning 's'. I
just have to repeat the operation and it works fine. What's wrong with
that?

Curious that it works the second time for you, but if you ever get this
exception the general idea is that the stub is stale: you should try to
reacquire the stub and retry the call. Maybe you're doing that?
 
A

acamposr

This is my last question (I hope so :)

I am having trouble when connecting to my RMI server class from a
computer outside my LAN.

I create a RMI registry before exporting the server:

Registry registry = LocateRegistry.createRegistry(1099);
Naming.rebind("//localhost/ServerName", serverObject);

I can get that object via Naming.lookup from any computer in the LAN,
but not anywhere outside (using my public IP).

Of course, I have set up the router to pass traffic through port 1099
to the IP of the server.

What's wrong? Is there any other port I have to forward?

Thanks!
 
E

EJP

What's wrong? Is there any other port I have to forward?

Yes. You also need to construct/export your remote object on a fixed
port number (by calling super(NNNN) or
UnicastRemoteObject.exportObject(NNNN, this) as appropriate) and forward
that port too.

If you run the Registry in the same JVM as your server they can both use
1099.

EJP
 
A

acamposr

Yes. You also need to construct/export your remote object on a fixed
port number (by calling super(NNNN) or
UnicastRemoteObject.exportObject(NNNN, this) as appropriate) and forward
that port too.

I am afraid that is not working with me. It can't connect from outside
my LAN.

I wrote a very simple server which calls super(1099) and creates a
registry at port 1099, but it doesn't work. The client (a friend from
his house) tries Naming.lookup("Server"); but the it gets "Connection
refused to host <my *private* IP>"
 
E

EJP

I am afraid that is not working with me. It can't connect from outside
my LAN.

You need to set -Djava.rmi.server.hostname in the server JVM to your
external IP address.
 

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

Similar Threads

How to remotely construct a remote object with RMI 14
java rmi error 1
RMI Objectreferences 2
RMI newbie 1
RMI activatable objects error 0
RMI & connection refused 10
RMI - Trying a simple example 0
RMI in java 15

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top