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

📄 main.java

📁 最新的关于精通rmi这本书的源码
💻 JAVA
字号:
/*
 * Copyright 1999 by dreamBean Software,
 * All rights reserved.
 */
package masteringrmi.hellosocket.server;

import java.io.IOException;
import java.net.MalformedURLException;
import java.rmi.RemoteException;
import java.rmi.ServerException;
import java.rmi.AlreadyBoundException;
import java.rmi.server.UnicastRemoteObject;
import java.util.Properties;

import java.rmi.registry.Registry;

import javax.naming.InitialContext;
import javax.naming.NamingException;

import masteringrmi.hellosocket.interfaces.IdentityClientSocketFactory;
import masteringrmi.hellosocket.server.IdentityServerSocketFactory;

/**
 *   This is the main server class. It creates an internal RMI-registry for naming, 
 *   creates a remote object, and registers this remote object in the
 *   created registry by using JNDI.
 *
 *   The remote object is exported with our custom connection factories, so that
 *   we can identity the clients that call our server. 
 *   
 *   NOTE: They are only identified, not authenticated! The difference is that 
 *   we only know the name of the client, but not if this is actually his/her real name.
 *   Adding functionality to make sure that this is the case is left as an exercise to the reader...
 *   
 *   @see HelloWorldImpl
 *   @author Rickard 謆erg
 *   @version $Revision:$
 */
public class Main
{
   // Attributes ----------------------------------------------------


   /**
    * @link aggregation
    * @label manages 
    */
   HelloWorldImpl server;
   
   // Static --------------------------------------------------------
   public static void main(String[] args)
      throws Exception
   {
      // Create server
      new Main();
      
      System.out.println("Server has been started");
   }
   
   // Constructors --------------------------------------------------
   public Main()
      throws IOException
   {
      startNaming();
      startService();
 
      // Done. 
      // The remote object, HelloWorldImpl, can now be invoked through the client!
   }
   
   // Protected -----------------------------------------------------
   protected void startNaming()
      throws RemoteException
   {
      // Create registry if not already running
      try 
      {
         java.rmi.registry.LocateRegistry.createRegistry(Registry.REGISTRY_PORT);
      } catch (java.rmi.server.ExportException ee) 
      {
         // Registry already exists
      } catch (RemoteException e) 
      {
         throw new ServerException("Could not create registry", e);
      }
   }

   protected void startService()
      throws RemoteException
   {
      // Create remote object. Export it on port 1234.
      // Use our custom factories
      server = new HelloWorldImpl();
      UnicastRemoteObject.exportObject(server, 1234, new IdentityClientSocketFactory(), new IdentityServerSocketFactory());
   
      // Register server with naming service
      // Use JNDI to hide away the fact that we're using the registry
      // This allows us to easily change the naming service later on
      try
      {
         new InitialContext().rebind(HelloWorldImpl.NAME,server);
      } catch (NamingException e)
      {
         throw new ServerException("The server could not be bound into the naming service", e);
      } 
   }
}

⌨️ 快捷键说明

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