RMI SERVER
Basic Steps: RMI Server
1. Define (or locate) the remote interface in agreement with the client.
2. Specify the remote interface being implemented.
3. Define the constructor for the remote object.
4. Provide implementations for the methods that can be invoked remotely.
5. Create and install a security manager.
6. Create one or more instances of a remote object.
7. Register at least one of the remote objects with the RMI remote object registry.
This implementation divides the server into the traditional two parts, a main program and an implementation class. It is just as feasible to combine these in a single class. The main program shown in next example simply constructs an instance of the implementation and registers it with the lookup service.
1. Define (or locate) the remote interface in agreement with the client.
2. Specify the remote interface being implemented.
3. Define the constructor for the remote object.
4. Provide implementations for the methods that can be invoked remotely.
5. Create and install a security manager.
6. Create one or more instances of a remote object.
7. Register at least one of the remote objects with the RMI remote object registry.
This implementation divides the server into the traditional two parts, a main program and an implementation class. It is just as feasible to combine these in a single class. The main program shown in next example simply constructs an instance of the implementation and registers it with the lookup service.
Example DateServer.java
package darwinsys.distdate;
import java.rmi.*;
public class DateServer {
public static void main(String[] args) {
// we may want a SecurityManager for downloading of classes:
// System.setSecurityManager(new RMISecurityManager( ));
try {
// Create an instance of the server object
RemoteDateImpl im = new RemoteDateImpl( );
System.out.println("DateServer starting...");
// Locate it in the RMI registry.
Naming.rebind(RemoteDate.LOOKUPNAME, im);
System.out.println("DateServer ready.");
} catch (Exception e) {
System.err.println(e);
System.exit(1);
}}
}The Naming.bind( ) method creates an association between the lookup name and the instance of the server object. This method will fail if the server already has an instance of the given name, requiring we to call rebind( ) to overwrite it. But since that's exactly where we'll find ourself if the server crashes (or we kill it while debugging) and we restart it, many people just use rebind( ) all the time.
The implementation class must implement the given remote interface.
import java.rmi.*;
public class DateServer {
public static void main(String[] args) {
// we may want a SecurityManager for downloading of classes:
// System.setSecurityManager(new RMISecurityManager( ));
try {
// Create an instance of the server object
RemoteDateImpl im = new RemoteDateImpl( );
System.out.println("DateServer starting...");
// Locate it in the RMI registry.
Naming.rebind(RemoteDate.LOOKUPNAME, im);
System.out.println("DateServer ready.");
} catch (Exception e) {
System.err.println(e);
System.exit(1);
}}
}The Naming.bind( ) method creates an association between the lookup name and the instance of the server object. This method will fail if the server already has an instance of the given name, requiring we to call rebind( ) to overwrite it. But since that's exactly where we'll find ourself if the server crashes (or we kill it while debugging) and we restart it, many people just use rebind( ) all the time.
The implementation class must implement the given remote interface.
Example RemoteDateImpl.java
package darwinsys.distdate;
import java.rmi.*;
import java.rmi.server.*;
import java.util.*;
public class RemoteDateImpl extends UnicastRemoteObject implements
RemoteDate
{
/** Construct the object that implements the remote server.
* Called from main, after it has the SecurityManager in place.
*/
public RemoteDateImpl( ) throws RemoteException {
super( ); // sets up networking
}
/** The remote method that "does all the work". This won't get
* called until the client starts up.
*/
public Date getRemoteDate( ) throws RemoteException {
return new Date( );
}}
import java.rmi.*;
import java.rmi.server.*;
import java.util.*;
public class RemoteDateImpl extends UnicastRemoteObject implements
RemoteDate
{
/** Construct the object that implements the remote server.
* Called from main, after it has the SecurityManager in place.
*/
public RemoteDateImpl( ) throws RemoteException {
super( ); // sets up networking
}
/** The remote method that "does all the work". This won't get
* called until the client starts up.
*/
public Date getRemoteDate( ) throws RemoteException {
return new Date( );
}}
Using the server
Once compiled the implementation class, we can run rmic (RMI compiler) to build some glue files and install them in the client's CLASSPATH:
$ jikes -d . RemoteDateImpl.java
$ ls darwinsys/distdate
DateApplet$1.class DateClient.class RemoteDate.class
DateApplet.class DateServer.class
RemoteDateImpl.class
$ rmic -d . darwinsys.distdate.RemoteDateImpl
$ ls darwinsys/distdate
DateApplet$1.class DateServer.class
RemoteDateImpl_Skel.class
DateApplet.class RemoteDate.class
RemoteDateImpl_Stub.class
DateClient.class RemoteDateImpl.class
$We must also ensure that TCP/IP networking is running, and then start the RMI registry program.
If we're doing this by hand, just type the command rmiregistry in a separate window, or start or background it on systems that support this.
$ jikes -d . RemoteDateImpl.java
$ ls darwinsys/distdate
DateApplet$1.class DateClient.class RemoteDate.class
DateApplet.class DateServer.class
RemoteDateImpl.class
$ rmic -d . darwinsys.distdate.RemoteDateImpl
$ ls darwinsys/distdate
DateApplet$1.class DateServer.class
RemoteDateImpl_Skel.class
DateApplet.class RemoteDate.class
RemoteDateImpl_Stub.class
DateClient.class RemoteDateImpl.class
$We must also ensure that TCP/IP networking is running, and then start the RMI registry program.
If we're doing this by hand, just type the command rmiregistry in a separate window, or start or background it on systems that support this.
No comments:
Post a Comment