Basic Model of
Second is that the
• Explicitly.
• Implicitly.
Both are used for "obtaining a remote reference".
To create a Math service using
1. Define a remote interface
2. Implementation of the server
3. Implementation of the client
4. Compile the source code
5. Start
Create the Remote Interface:
In
Example which I have use in this paragraph for remote interface, "bite.example.SampleServer". It declares the four methods, "Addition", "Subtraction", "Multiplication" and "Square".
Following is the source code for "SampleServer.
Sample Server
package bite.example;
import
import
public interface SampleServer extends Remote
{
public int addition(int x, int y) throws RemoteException;
public int subtract(int x, int y)throws RemoteException;
public int multiply(int x, int y)throws RemoteException;
public int square(int x)throws RemoteException;
public int getValue()throws RemoteException;
public String getLastOp()throws RemoteException;
}
Implementation of Sever:
In this context our "server" class exports the remote object and this server class holds a "main" method, this produce an instance of the remote object implementation. Then it combines that instance to a name in a
In the class "Server" we declare the "main" method for server, and it also perform the remote interface SampleServer. Our server's "main" method follows these two steps. A new remote object is produced and export in first step, second step is about the registration of an object with a
Source code for class "SampleServerImpl.
Sample Server Impl.
package bite.example;
import
import
import
import
public class SampleServerImpl implements SampleServer {
private int value =0;
private String lastOp;
SampleServerImpl(){
}
public int addition(int x, int y) throws RemoteException {
value = x + y;
lastOp = "ADDITION";
return value;
}
public int subtract(int x, int y)throws RemoteException{
value = x - y;
lastOp = "SUBTRACTION";
return value;
}
public int multiply(int x, int y)throws RemoteException{
value = x*y;
lastOp = "MULTIPLY";
return value;
}
public int square(int x)throws RemoteException{
value = x*x;
lastOp = "SQUARE";
return value;
}
/* Resource properties */
public int getValue() throws RemoteException {
return value;
}
public void setValue(int value) {
this.value = value;
}
public String getLastOp()throws RemoteException {
return lastOp;
}
public void setLastOp(String lastOp) {
this.lastOp = lastOp;
}
public static void main(String args[]) {
try {
//Create and export a remote object
SampleServerImpl obj = new SampleServerImpl();
SampleServer stub = (SampleServer)UnicastRemoteObject.exportObject(obj, 0);
//Register the remote object with a
//and bind the remote object's stub in the registry
Registry registry = LocateRegistry.getRegistry();
registry.bind("SampleServer", stub);
System.err.println("Server ready");
} catch (Exception e) {
System.err.println("Server exception: " + e.toString());
e.printStackTrace();
}
}
}
Implementation of Client:
Client class acquires a "stub" for the registry on the server's host, and it is searches remote object's stub in the registry by their names, and then it invokes the "Addition", "Subtraction", "Multiply" and "Square" methods on the remote object using stub.
Source code for the Client is following.
Sample Client
package bite.example;
import
import
import
import
public class SampleClient {
public static void main(String[] args) {
String host = (args.length start
And then I have got the output "Server ready"
Start the Client: Final step is to start the client. When server was ready I open another window of command prompt line and then run the client as follow "C:rmi>
Source by Muhammad Tanveer
Post a Comment