RMI without the rmiregistry

Discussion in 'Java' started by JP Martin, Oct 5, 2004.

  1. JP Martin

    JP Martin Guest

    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)
     
    JP Martin, Oct 5, 2004
    #1
    1. Advertising

  2. JP Martin

    Esmond Pitt Guest

    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.
     
    Esmond Pitt, Oct 6, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rob Dempster

    RMI with rmiregistry on remote host

    Rob Dempster, Oct 8, 2003, in forum: Java
    Replies:
    1
    Views:
    2,745
    Marek Lange
    Oct 8, 2003
  2. Arun Goel
    Replies:
    0
    Views:
    988
    Arun Goel
    Nov 12, 2003
  3. silarri

    Monitoring the rmiregistry

    silarri, Jan 3, 2005, in forum: Java
    Replies:
    3
    Views:
    1,612
    silarri
    Jan 4, 2005
  4. jonck

    rmiregistry woes

    jonck, Jan 21, 2005, in forum: Java
    Replies:
    6
    Views:
    607
    Esmond Pitt
    Jan 24, 2005
  5. John
    Replies:
    0
    Views:
    684
Loading...

Share This Page