⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainrmi.java

📁 《精通RMI:Java与EJB企业级应用开发》书籍的源代码. 本书是讲述RMI技术的经典著作
💻 JAVA
字号:
/*
 * Copyright 1999 by dreamBean Software,
 * All rights reserved.
 */
package smartworld.server;

import java.io.IOException;
import java.io.File;
import java.lang.reflect.Proxy;
import java.net.URL;
import java.net.URLClassLoader;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.AlreadyBoundException;
import java.rmi.RemoteException;
import java.rmi.server.RemoteObject;
import java.rmi.server.UnicastRemoteObject;

import smartworld.interfaces.SmartWorld;

import smartproxy.proxies.DynamicStubHandler;
import smartproxy.proxies.LogProxy;
import smartproxy.proxies.ReconnectProxy;
import smartproxy.proxies.RetryProxy;
import smartproxy.proxies.PerformanceProxy;

import com.dreambean.dynaserver.DynaServer;

/**
 *   Main server class. This creates a remote object, and registers it
 *   with the registry service.
 *
 *   The RMI-registry must be started prior to executing this class. Also,
 *   since we are not using dynamic class downloading the RMI-registry must
 *   have all the client-classes of the remote object in its classpath (i.e. the remote interface+the stub).
 *   
 *   Since it does not require dynamic class downloading from the client we do not install a SecurityManager.
 *
 *   @see SmartWorldImpl
 *   @author Rickard 謆erg (rickard@dreambean.com)
 *   @version $Revision:$
 */
public class MainRMI
{
   DynaServer webserver;
   // Static --------------------------------------------------------

   /**
    *   Run server as stand-alone application.
    *
    * @param   args  
    * @exception   Exception  
    */
   public static void main(String[] args)
      throws Exception
   {
      new MainRMI();
   }
   
   // Public --------------------------------------------------------

   /**
    *   Create a remote object and register it with the RMI-registry.
    *
    */
   public MainRMI()
      throws Exception
   {
      startWebserver();
      try
      {
         // Set "java.protocol.handler.pkgs" to "smartproxy"
         DynamicStubHandler.init();
      
         // Create remote object
         SmartWorld server = new SmartWorldImpl();
         UnicastRemoteObject.exportObject(server);
         
         
         // Create smart proxy wrappers
         // Audit trail
         server = (SmartWorld) Proxy.newProxyInstance(SmartWorld.class.getClassLoader(),
                                          new Class[] { SmartWorld.class },
                                          new LogProxy(server));
                           
         // Perform logging on server
         UnicastRemoteObject.exportObject(server); // Comment this out to perform logging on client

         // The proxies below are executed on client
         // Reconnect
         server = (SmartWorld) Proxy.newProxyInstance(server.getClass().getClassLoader(),
                                          new Class[] { SmartWorld.class },
                                          new ReconnectProxy(server, SmartWorld.NAME));
                                 
         // Retry
         server = (SmartWorld) Proxy.newProxyInstance(server.getClass().getClassLoader(),
                                          new Class[] { SmartWorld.class },
                                          new RetryProxy(server));
                                 
         // Performance logging
         server = (SmartWorld) Proxy.newProxyInstance(server.getClass().getClassLoader(),
                                          new Class[] { SmartWorld.class },
                                          new PerformanceProxy(server));

         // Register server with naming service
         Naming.bind(SmartWorld.NAME, server);
         
         System.out.println("Server has been started and registered");
      } catch (AlreadyBoundException e)
      {
         System.out.println("An object had already been bound to that name. Restart the registry");
         e.printStackTrace(System.err);
      } catch (MalformedURLException e)
      {
         System.out.println("The server name was incorrect");
         e.printStackTrace(System.err);
      } catch (RemoteException e)
      {
         System.out.println("The object could not be created");
         e.printStackTrace(System.err);
      }
   }
   
   protected void startWebserver()
      throws IOException
   {
      // Create webserver for dynamic class downloading
      webserver = new DynaServer();
      webserver.setDebug(true);
      
      // Add the classloader for this application
      // This will allow any client to download classes/resources that
      // are in the classpath for this application
      webserver.addClassLoader(Thread.currentThread().getContextClassLoader());
      
      // Start the webserver
      webserver.start();
   }
}

⌨️ 快捷键说明

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