XMPP协议学习笔记四(Openfire服务器启动过程)

XMPP协议学习笔记四(Openfire服务器启动过程) - nomousewch的专栏 - 博客频道 - CSDN.NETjava

   在上篇文章中咱们成功部署了openfire的源码,这一篇咱们来初步了解一下openfire的项目结构。web

  • 概述

    Openfire最主要的功能是实现XMPP服务器,简单来讲,openfire为咱们提供一个固定的地址,咱们只须要向openfire服务器发送标准的XMPP信息(即XML文件流),那么openfire服务器应当给予咱们回应,这里的openfire服务器也能够看作一个容器,咱们在聊天时,须要在这个服务器上注册一个会话,在会话存在的时间,咱们能够实现即时聊天的一些经常使用功能,好比创建本身的组,添加好友,聊天,以及传送文件等等,同时,openfire服务器也须要实现本身的管理界面,这样openfire服务器也扮演一个web容器的角色。数据库

 

    XMPP协议是基于TCP/IP协议进行传输的,在openfire中,应用了apache的mina框架做为NIO框架,简单的来讲,openfire服务器用mina框架创建一个简单的服务器,能够接收和发送基本的IO流,而后在此基础上把接收到的IO流解析为XML文件,而后在根据XMPP协议对XML文件进行操做。apache

  • Openfire启动过程

    系统启动时调用org.jivesoftware.openfire.starter.ServerStarter类中的start()方法,加载org.jivesoftware.openfire.XMPPServer类,并调用这个类的start()方法进行一系列的初始化操做,下面是XMPPServer的Start()方法服务器

  1. public void start() {  
  2.         try {  
  3.             initialize();  
  4.   
  5.             startDate = new Date();  
  6.             // Store server info  
  7.             xmppServerInfo = new XMPPServerInfoImpl(name, host, version, startDate, getConnectionManager());  
  8.   
  9.             // Create PluginManager now (but don't start it) so that modules may use it  
  10.             File pluginDir = new File(openfireHome, "plugins");  
  11.             pluginManager = new PluginManager(pluginDir);  
  12.   
  13.             // If the server has already been setup then we can start all the server's modules  
  14.             if (!setupMode) {  
  15.                 //这个方法是验证数据库链接是否正确  
  16.                 verifyDataSource();  
  17.                 //加载全部模块  
  18.                 // First load all the modules so that modules may access other modules while  
  19.                 // being initialized  
  20.                 loadModules();  
  21.                 // Initize all the modules  
  22.                 initModules();  
  23.                 // Start all the modules  
  24.                 startModules();  
  25.             }  
  26.             // Initialize statistics  
  27.             ServerTrafficCounter.initStatistics();  
  28.   
  29.             // Load plugins (when in setup mode only the admin console will be loaded)  
  30.             pluginManager.start();  
  31.   
  32.             // Log that the server has been started  
  33.             String startupBanner = LocaleUtils.getLocalizedString("short.title") + " " + version.getVersionString() +  
  34.                     " [" + JiveGlobals.formatDateTime(new Date()) + "]";  
  35.             Log.info(startupBanner);  
  36.             System.out.println(startupBanner);  
  37.   
  38.             started = true;  
  39.               
  40.             // Notify server listeners that the server has been started  
  41.             for (XMPPServerListener listener : listeners) {  
  42.                 listener.serverStarted();  
  43.             }  
  44.         }  
  45.         catch (Exception e) {  
  46.             e.printStackTrace();  
  47.             Log.error(e.getMessage(), e);  
  48.             System.out.println(LocaleUtils.getLocalizedString("startup.error"));  
  49.             shutdownServer();  
  50.         }  
  51.     }  
public void start() {
        try {
            initialize();

            startDate = new Date();
            // Store server info
            xmppServerInfo = new XMPPServerInfoImpl(name, host, version, startDate, getConnectionManager());

            // Create PluginManager now (but don't start it) so that modules may use it
            File pluginDir = new File(openfireHome, "plugins");
            pluginManager = new PluginManager(pluginDir);

            // If the server has already been setup then we can start all the server's modules
            if (!setupMode) {
                //这个方法是验证数据库链接是否正确
                verifyDataSource();
                //加载全部模块
                // First load all the modules so that modules may access other modules while
                // being initialized
                loadModules();
                // Initize all the modules
                initModules();
                // Start all the modules
                startModules();
            }
            // Initialize statistics
            ServerTrafficCounter.initStatistics();

            // Load plugins (when in setup mode only the admin console will be loaded)
            pluginManager.start();

            // Log that the server has been started
            String startupBanner = LocaleUtils.getLocalizedString("short.title") + " " + version.getVersionString() +
                    " [" + JiveGlobals.formatDateTime(new Date()) + "]";
            Log.info(startupBanner);
            System.out.println(startupBanner);

            started = true;

            // Notify server listeners that the server has been started
            for (XMPPServerListener listener : listeners) {
                listener.serverStarted();
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            Log.error(e.getMessage(), e);
            System.out.println(LocaleUtils.getLocalizedString("startup.error"));
            shutdownServer();
        }
    }

    我的以为,openfire服务器的启动过程至关清晰,全部的模块都实现了Module接口,而后在启动时统一调用全部模块的initial()方法和start()方法,简单明了又便于扩展,在从此本身的项目中值得加以运用。app