zoukankan      html  css  js  c++  java
  • XMPP协议学习笔记四(Openfire服务器启动过程)

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

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

    • 概述

        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文件进行操作。

    • 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()方法,简单明了又便于扩展,在今后自己的项目中值得加以运用。

  • 相关阅读:
    程序员的7中武器
    需要强化的知识
    微软中国联合小i推出MSN群Beta 不需任何插件
    XML Notepad 2006 v2.0
    Sandcastle August 2006 Community Technology Preview
    [推荐] TechNet 广播 SQL Server 2000完结篇
    《太空帝国 4》(Space Empires IV)以及 xxMod 英文版 中文版 TDM Mod 英文版 中文版
    IronPython 1.0 RC2 更新 1.0.60816
    Microsoft .NET Framework 3.0 RC1
    《Oracle Developer Suite 10g》(Oracle Developer Suite 10g)V10.1.2.0.2
  • 原文地址:https://www.cnblogs.com/seven1979/p/4221937.html
Copyright © 2011-2022 走看看