A server calling a method on client? Help

C

caruso_XII

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.
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

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
 

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,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top