main.java

来自「最新的关于精通rmi这本书的源码」· Java 代码 · 共 104 行

JAVA
104
字号
/*
 * Copyright 1999 by dreamBean Software,
 * All rights reserved.
 */
package smartworld.client;

import java.io.IOException;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.Properties;

import smartworld.interfaces.SmartWorld;

/**
 *   This test application client locates a server object, and invokes it.
 *
 *   This demonstrates a barebones client which is about as simple as it gets, RMI-wise at least.
 *
 *   @author Rickard 謆erg (rickard@dreambean.com)
 *   @version $Revision:$
 */
public class Main
   implements Runnable
{
   // Static --------------------------------------------------------

   /**
    *   Start the client as a stand-alone application.
    *
    * @param   args  
    * @exception   Exception  
    */
   public static void main(String[] args)
      throws Exception
   {
      System.setSecurityManager(new SecurityManager()
      {
         public void checkPermission(java.security.Permission p)
         {
            
         }
      });
      
      new Main().run();
   }
   
   // Public --------------------------------------------------------

   // Runnable implementation ---------------------------------------

   /**
    *   Run the client. Lookup the server from a RMI-registry, call it, and print the result.
    *
    */
   public void run()
   {
      try
      {
         // Locate remote object
         SmartWorld server = (SmartWorld)Naming.lookup(SmartWorld.NAME);
/*         
         // Call it
         String result = server.helloWorld("World");
         
         // Print result
         System.out.println(result);
         
         // Say hi
         server.sayHi("World", true);
         
         // Test arrays
         server.testArrays(new int[1][2]);
*/         
         // Test reconnect
         while (true)
         {
            try
            {
               System.out.println(server.helloWorld("World"));
            } catch (RemoteException e)
            {
               System.out.println("Server not available:"+e.getMessage());
            }
            
            Thread.sleep(1000);
         }
         
      } catch (NotBoundException e)
      {
         System.out.println("The server could not be found");
         e.printStackTrace(System.err);
      } catch (MalformedURLException e)
      {
         System.out.println("The server name was incorrect");
         e.printStackTrace(System.err);
      } catch (Exception e)
      {
         System.out.println("The object could not be created");
         e.printStackTrace(System.err);
      }
   }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?