Consultor Eletrônico



Kbase 19839: Java Program to Test RMI Calls
Autor   Progress Software Corporation - Progress
Acesso   Público
Publicação   24/09/2008
Status: Verified

GOAL:

Test programs to confirm that the Java RMI mechanism is working.

FACT(s) (Environment):

Progress 9.x

FIX:

Steps to run the test:


    1) Save the first piece of code below as a file named
        "PSCTest.java".
    2) Save the second piece of code below as a file named
        "RMITestServer.java".
    3) Set your path to include your Java compiler (javac).
    4) javac PSCTest.java
    5) javac RMITestServer.java
    6) rmic RMITestServer
    7) rmiregistry &
    8) java RMITestServer
        This should print: RMIServer is ready to take your requests.
    9) Open another session and run the client.  The client will take
        the following commands:

              time - display the current time
              author - display the program's author

   10) java PSCTest\$Client time
        This should print:
        The current date and time is Wed May 31 12:48:42 EDT 2000
   11) java PSCTest\$Client author
        This should print:
        This program was written by Progress Technical Support

To clean up, do a ps -ef|grep <yourlogin> and look for rmiregistry.
There may be another process running that is running something
called RegistryImpl.  This is a subprocess of the process running
rmiregistry. Do a kill -1 on the subprocess, and the rmiregistry
should shut down also. Otherwise, just do a kill -1 on the
rmiregistry process.

/*********PSCTest.java:*************/

import java.rmi.*;

/**
* This class is a placeholder that simply contains other classes.
**/
public class PSCTest {
 /**
  * This is the interface that defines the exported methods of the
  * server.
  **/
 public interface RMITest extends Remote {
   /** Get the current time */
   public String getDateTime()
        throws RemoteException, RMITestException;

   /** Get the program's author */
   public String getAuthor()
        throws RemoteException, RMITestException;
 }

 /**
  * This is a type of exception used to represent exceptional
  * conditions.
  **/
 public static class RMITestException extends Exception {
   public RMITestException(String msg) { super(msg); }
 }

 /**
  * This class is a simple stand-alone client program that interacts
  * with a RMITest server.  It invokes different methods depending on
  * its command-line arguments.
  **/
 public static class Client {
   public static void main(String[] args) {
     try {
       // Set the standard RMI security manager so that we can
       // safely load untrusted rmitest stub code over the network.
       System.setSecurityManager(new RMISecurityManager());

       // Figure out what server to connect to by reading a system
       // property (specified on the command line with a -D option
       // to java) or, if it is not defined, use a default URL.
       // Note that by default this client tries to connect to a
       // server on the local machine
       String url = System.getProperty("rmiserver",
                      "rmi:///RMIServer");

       // Now look up that server using the Naming object, which
       // contacts the rmiregistry server.  Given the url, this call
       // returns a rmiserver object whose methods may be invoked
       // remotely
       RMITest myTest = (RMITest) Naming.lookup(url);

       // Convert the user's command to lower case
       String cmd = args[0].toLowerCase();

       // Now, go test the command against the options
       if (cmd.equals("time")) {           // Show the time
         System.out.println("The current date and time is " +
                             myTest.getDateTime());
       }
       else if (cmd.equals("author")) {    // Show the program author
         S.ystem.out.println(myTest.getAuthor());
       }
       else {
         System.out.println("Unknown command");
         System.err.println("Usage: java [-Dserver=<url>] PSC$Client
<command>");
         System.err.println("where command is: time, author");
       }
     }
     // Catch and display RMI exceptions
     catch (RemoteException e) {System.err.println(e);}
     // Catch and display any related exceptions
     catch (RMITestException e) {System.err.println(e.getMessage());}
     // Other exceptions are probably user syntax errors,
     //so show usage.
     catch (Exception e) {
       System.err.println(e);
       System.out.println("Unknown command");
       System.err.println("Usage: java [-Dserver=<url>] PSC$Client
<command>");
       System.err.println("where command is: time, author");
     }
   }
 }
}

/**************RMITestServer.java:*****************/

import java.rmi.*;
import java.rmi.server.*;
import java.util.*;
import PSCTest.*;

/**
* This class implements the remote methods defined by the PSCTest
* interface.
**/
public class RMITestServer extends UnicastRemoteObject implements
RMITest
{

 /**
  * This constructor doesn't do anything, but because it throws
  * the same exception that the superclass constructor throws, it
  * must be declared.
  **/
 public RMITestServer() throws RemoteException { super(); }

 /**
  * Show the current system time.
  *
  * This method is synchronized to make it thread safe, since it
  * manipulates the accounts hashtable.
  **/
 public synchronized String getDateTime()
      throws RemoteException, RMITestException {
   String currentTime = new Date().toString();
   return currentTime;
 }

 /**
  * Show the program's author.
  *
  * This method is synchronized to make it thread safe, since it
  * manipulates the accounts hashtable.
  **/
 public synchronized String getAuthor()
      throws RemoteException, RMITestException {
   String author =
      "This program was written by Progress Technical Support";
   return author;
 }

 /**
  * The main program that runs this RMITestServer.
  *
  * Create a RMITestServer object and give it a name in the registry.
  * Read a system property to determine the name, but use "RMIServer"
  * as the default name.  This is all that is necessary to set up the
  * service.  RMI takes care of the rest.
  **/
 public static void main(String[] args) {
   try {
     // Create a server object
     RMITestServer myServer = new RMITestServer();
     // Figure out what to name it
     String name = System.getProperty("server", "RMIServer");
     // Name it that
     Naming.rebind(name, myServer);
     // Tell the world we're up and running
     System.out.println(name + " is ready to take your requests.");
   }
   catch (Exception e) {
     System.err.println(e);
     System.err.println("Usage: java [-Dserver=<name>]
RMITestServer");
     System.exit(1); // Force an exit because there might
                     // be other threads
   }
 }
}
.