RMI without the rmiregistry

J

JP Martin

All,

I am trying to use RMI to get to java programs to talk to each other
in a simpler way than sending messages over sockets. In my case, the
two programs already use sockets for some of their communication, so
it seems that I should not have to bother with the RMI Registry... or
should I?

Here's my client code:

public void hit() throws Exception {
Socket socket = new Socket("localhost",9943);
ObjectInputStream in = new
ObjectInputStream(socket.getInputStream());
Object o = in.readObject();
System.out.println("Received: "+o);
((HitMe)o).hit();
}

public static void main(String argv[]) throws Exception {
new TestRMIClient().hit();
}

The HitMe interface extends Remote and only has the hit() method. I
wrote an implementation for it that just displays a message.

The server does mostly the same thing, sends the object to the
client. The relevant lines are:

// in the constructor; MyHitMe extends UnicastRemoteObject
// and implements the HitMe interface.
hitme = new MyHitMe();

// in response to a connection from a client
out.writeObject( hitme );


Here's the output I get from the client:

Received: MyHitMe[RemoteStub [ref:
[endpoint:[<my-ip-here>:46846](local),objID:[0]]]]
I'm hit!

This shows that the passed object was (as I was expecting) not of the
implementation type (MyHitMe) but instead of the stub type generated
by
rmic (RemoteStub). Now, I'm expecting the stub to forward method calls
to the server process, so then the "I'm hit" message should be
displayed
at the server.

But it's not. The message gets displayed on the client, and I don't
understand why.


Does anyone here understand what's happening? Will I have to use the
RMI Registry even though I have no need for discovery since my app
already runs at a well-known location on a well-known port?

Thanks in advance,
JP
(please reply to the thread since I don't check that email often)
 
E

Esmond Pitt

ObjectOutputStream.writeObject() doesn't replace a remote object by its
stub, only RMI does this, so you are serializing the object itself and
so it acts at the wrong peer. Try out.writeObject(
RemoteObject.toStub(hitme) ); You may then run into codebase issues if
you haven't deployed the stubs, which you can solve by wrapping the stub
in a MarshalledObject. Easier to use the registry, on the whole.
 

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

RMI & connection refused 10
Java RMI - registry questions 3
Simple RMI 0
rmiregistry woes 6
RMI and thread safety 1
simple RMI chat problem 1
RMI without registry 1
RMI and references / Proxy classes 0

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top