Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2006

Tip: Looking for answers? Try searching our database.

A server calling a method on client? Help

Thread view: 
caruso_XII@yahoo.com - 10 Aug 2006 17:13 GMT
Can RMI go both ways? More precise, can I use a remote object to call
from the server a method that runs on the client (assume client has RMI
connection already to server and created the remote object locally and
passed it to server)?

In the RMI tutorial
(http://java.sun.com/docs/books/tutorial/rmi/implementing.html),

only one way happens: the client calls a method that runs on server.
So, the ClientPi uses the remote object Compute to run the method
executeTask on the server.

To demonstrate if RMI can go both ways, suppose we depart from the RMI
tutorial, but server wants to run a method on the client. I think you
can have this interface:

package compute;

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface Compute2 extends Remote {
   void executeAtCli(Integer i) throws RemoteException;
}

Suppose we change the server so we can send from client a reference to
our remote object Compute2:

package compute;

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface Compute extends Remote {
   Object executeTask(Task t) throws RemoteException;
   public void recvCompute2(Compute2 c2) throws RemoteException;
}

Then the server implementation can do this (I only list important
lines):

public class ComputeEngine extends UnicastRemoteObject
                          implements Compute
{
   public ComputeEngine() throws RemoteException {
       super();
   }

   public Object executeTask(Task t) {
       return t.execute();
   }
   private Compute2 c2;
   public void recvCompute2(Compute2 c2) throws RemoteException {
     this.c2 = c2; }

   // non-remote method to invoke on client
   public void cliRun() {
     Integer ret = (Integer)c2.executeAtCli(new Integer(1));
     System.out.println("Client computed: " + ret); }
...
}

The ClientPi will have to create the Compute2 instance and pass it to
server (again, I put only important lines):

       try {
           String name = "//" + args[0] + "/Compute";
           Compute comp = (Compute) Naming.lookup(name);
           Pi task = new Pi(Integer.parseInt(args[1]));
           BigDecimal pi = (BigDecimal) (comp.executeTask(task));
           System.out.println(pi);
        Compute2 c2 = new Compute2Imp(); // created locally
        comp.recvCompute2(c2);
       } catch (Exception e) {
           System.err.println("ComputePi exception: " +
e.getMessage());
           e.printStackTrace();
       }
Compute2Imp is a concrete of Compute2 and exists on client as (again,
only some lines):

public class Compute2Imp extends UnicastRemoteObject // need this?
                          implements Compute2
{
   public ComputeEngine() throws RemoteException {
       super();
   }

   public Object executeTask(Integer i) {
     // some simple computation
       return new Integer(i + 10);
   }
...
}

I am having many troubles running the tutorial, so I can't check this
so that's why I post. If I run tutorial, I will post my result.
Arne Vajhøj - 11 Aug 2006 02:35 GMT
> Can RMI go both ways? More precise, can I use a remote object to call
> from the server a method that runs on the client (assume client has RMI
> connection already to server and created the remote object locally and
> passed it to server)?

Yes.

Search for RMI callback.

There should be plenty of examples.

And it is not so difficult.

Arne


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.