Basic model of
Java RMI: Java Runtime distributed objects is possible with the help of Java
RMI. A
client can access a Java object that is hosted in a virtual machine remotely
from any JVM. The first step
is the implementation of Java clients and servers. The
second is that the fundamental model of Java RMI is always a client program,
the client program and is used to tighten the remote objects of any Java
virtual machine. Object
remote server may be located in two different ways servers. Both
methods have their own methods to serve remote reference to the customer.
Java RMI
Architecture:
To
create a mathematical service using Java RMI followed these steps.1. Define a remote interface
2. The running server
3. Client Implementation
4. Compile the source code
5. Start Recording Server and Java RMI client.
Create Remote Interface:
Java RMI is an interface used to extend the "java.rmi.Remote" interface. Set of remote methods also declared in the interface. The following is the source code for "SampleServer.java".
server sample
bite.example package;
java.rmi.Remote import;
import java.rmi.RemoteException;
public interface SampleServer extends away
{
public int addition (int x, int y) throws RemoteException;
Subtract public int (int x, int y) throws RemoteException;
public int multiply (int x, int y) throws RemoteException;
int public square (int x) throws RemoteException;
public int getValue () throws RemoteException;
public String getLastOp () throws RemoteException;
}
Implementation Sever:
In this context, our class "server" exports the remote object and the server maintains a class method "main", it produces an instance of the implementation of the remote object. In the "Server" category said the "main" method for the server, and also make SampleServer remote interface. "Main" method of our server following these steps. A new remote object is produced and export in the first step, the second step is the account of an object with a Java RMI registry.
The source code for the class "SampleServerImpl.java" is as follows.
Example impl server.
bite.example package;
java.rmi.registry.Registry import;
java.rmi.registry.LocateRegistry import;
import java.rmi.RemoteException;
java.rmi.server.UnicastRemoteObject import;
public class implements SampleServerImpl SampleServer {
private int value = 0;
lastOp private String;
SampleServerImpl () {
}
public int addition (int x, int y) throws RemoteException {
value = x + y;
lastOp = "addition";
the return value;
}
Subtract public int (int x, int y) throws RemoteException {
value = x - y;
lastOp = "SUBTRACT";
the return value;
}
public int multiply (int x, int y) throws RemoteException {
value = x * y;
lastOp = "multiply";
the return value;
}
int public square (int x) throws RemoteException {
value = x * x;
lastOp = "square";
the return value;
}
/ * Properties of functions * /
public int getValue () throws RemoteException {
the return value;
}
public void setValue (int value) {
this.value = value;
}
public String getLastOp () throws RemoteException {
lastOp return;
}
public void setLastOp (String lastOp) {
this.lastOp = lastOp;
}
void main (String args []) {public static
try {
// Create and export a remote object
= New SampleServerImpl of SampleServerImpl ();
SampleServer stub = (SampleServer) UnicastRemoteObject.exportObject (obj, 0);
// Register the remote object with a Java RMI registry
// And connecting stub of the remote object in the registry
Registered LocateRegistry.getRegistry = ();
registry.bind ( "SampleServer", top);
System.err.println ( "Ready Server");
} Catch (Exception e) {
System.err.println ( "Server Exception:" + e.ToString ());
e.printStackTrace ();
}
}
}
Client Deployment:
class customer gets a "sketch" for recording on the host server, and the stub search of the remote object in the registration of their names, then it calls the "addition", "subtraction", "Multiply "and methods" square "on the remote object using the stub.
Customer sample
bite.example package;
import java.io. *;
java.rmi.registry.LocateRegistry import;
java.rmi.registry.Registry import;
java.util.Scanner import;
public class {SampleClient
main (args String []) {public static void
Host = string (args.length java start -Djava.rmi.server.codebase = file: C: / RMI / -Djava.rmi.server.name = 192.168.0.03 bite.example.SampleServerImpl "
And then exit "server ready"
Start the client: The last step is to start the client.
0 komentar