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

📄 main.java

📁 分别介绍RMI,分布式对象与CORBA,动态接口,CORBA客户端开发的课件,以及相关的例子
💻 JAVA
字号:
/*
 * Copyright 1999 by dreamBean Software,
 * All rights reserved.
 */
package masteringrmi.chat.server;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.MalformedURLException;
import java.rmi.server.UnicastRemoteObject;
import java.rmi.AlreadyBoundException;
import java.rmi.RemoteException;
import java.rmi.ServerException;
import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
import java.util.Properties;

import javax.naming.InitialContext;

import masteringrmi.chat.interfaces.TopicInfo;

import com.dreambean.dynaserver.DynaServer;

/**
 *   This is the admin class that manages the chat server.
 *      
 *   @see TopicServerImpl
 *   @author Rickard 謆erg (rickard@dreambean.com)
 *   @version $Revision:$
 */
public class Main
{
   // Attributes ----------------------------------------------------
   TopicServerImpl server; // To prevent GC
   
   // Static --------------------------------------------------------
   public static void main(String[] args)
      throws Exception
   {
      // Load system properties

      System.getProperties().load(
         Thread.currentThread().
         getContextClassLoader().
         getResourceAsStream("system.properties"));
      
      new Main();
   }
   
   // Constructors --------------------------------------------------
   public Main()
   {
      // Start server
      try
      {
         startWebServer();
         
         startNamingServer();
         
         startTopicServer();
         
         System.out.println("Server has been started and registered");
      
      } catch (Exception e)
      {
         System.out.println("The server could not be started");
         e.printStackTrace(System.err);
      }
   }

   // Public --------------------------------------------------------
   public void startWebServer()
      throws IOException
   {
      // Start webserver for dynamic class loading
      DynaServer srv = new DynaServer();
      srv.addClassLoader(Thread.currentThread().getContextClassLoader());
      srv.start();
   }
   
   public void startNamingServer()
      throws Exception
   {
      // Create registry if not already running
      try 
      {
         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);
      }
   }
   
   public void startTopicServer()
      throws Exception
   {
      // Create remote object
      server = new TopicServerImpl(Integer.getInteger("chat.server.threads", 5).intValue());
      
      // Load configuration
      server.setPort(Integer.getInteger("chat.server.port", 0).intValue());
      
      // Export server
      UnicastRemoteObject.exportObject(server, server.getPort());
      
      // Create a few topics
      server.createTopic(new TopicInfo("Beginner RMI", "Welcome to the RMI beginner forum"));
      server.createTopic(new TopicInfo("Advanced Java", "Welcome to the RMI advanced forum"));
      server.createTopic(new TopicInfo("Cool tricks in RMI", "Welcome to the forum for cool RMI tricks"));
   
      // Register server with naming service
      new InitialContext().bind("topics",server);
   }
}

⌨️ 快捷键说明

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