I've searched in many places for the solution and i couldn't find one.
My question is how to define an offline method, so after i get the Object from the RMI server, i will be able to call this method and it wont get the data from the server it will take it from the object which in the client.
To be more specific lets say that i have Database class with 2 methods and one private variable. Method 1: returns the value of the variable (a property method..) - should be offline method because i already have the variable in the client's object. Method 2: Insert new data to the database (sql command..). - should be online method so the server can connect to the database.
Thanks alot!
You can't do that. You will need to adjust your design. Happily, i don't think you need to adjust your design much.
The key thing is to be able to decide whether every object is either a remote object or a local one. Calls to the remote objects will go to the server, whereas calls to the local one will remain local. If you have one object which you want to be both, you need to split it in two, so you have a local part and a remote part. To maintain a convenient interface for clients, you can hide the remote object behind the local one.
Here's a sketch of how you could do this:
public interface RemoteDatabase extends Remote {
public void insertData(String data) throws RemoteException;
}
public class RemoteDatabaseImpl implements RemoteDatabase {
@Override
public void insertData(String data) throws RemoteException {
// whatever
}
}
public class Database implements Serializable {
private final RemoteDatabase remote;
private final String property;
public Database(RemoteDatabase remote, String property) {
this.remote = remote;
this.property = property;
}
public String getProperty() {
return property;
}
public void insertData(String data) throws RemoteException {
remote.insertData(data);
}
}
Note that when you create instances of Database
on the server, you need to give it a stub for the RemoteDatabaseImpl
implementation, rather than the real thing (i think).
You could even make these implement the same interface, to reinforce the relationship of the objects. Something like this:
public interface RemoteDatabase extends Remote {
public String getProperty() throws RemoteException;
public void insertData(String data) throws RemoteException;
}
public class RemoteDatabaseImpl implements RemoteDatabase {
@Override
public void insertData(String data) throws RemoteException {
// whatever
}
@Override
public String getProperty() {
return "whatever";
}
public Database toLocal() throws NoSuchObjectException {
return new Database((RemoteDatabase) PortableRemoteObject.toStub(this), getProperty());
}
}
public class Database implements RemoteDatabase, Serializable {
private final RemoteDatabase remote;
private final String property;
public Database(RemoteDatabase remote, String property) {
this.remote = remote;
this.property = property;
}
@Override
public String getProperty() {
return property;
}
@Override
public void insertData(String data) throws RemoteException {
remote.insertData(data);
}
}