在上篇文章中我们成功部署了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()方法
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()方法,简单明了又便于扩展,在今后自己的项目中值得加以运用。