最新项目在搞安卓推送,选用的xmpp协议,用了一套androidpn推送框架服务端是tomcat的版本的androidpn-server,网上对于这套服务端源码的理解比较少,于是我抽空把源码给解析了一遍并且了解到了整个服务的启动过程。
下面是项目的git地址:https://github.com/msdx/androidpn/tree/master/androidpn-server-bin-tomcat
工程本身是一套tomcat的web工程,有控制台,本文将从工程建立的视角来观察源码
?入口web.xml
和大多数web工程一样,本项目的入口也是web.xml,通过web.xml启动spring,下面是源码
web.xml
?
<context-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/applicationContext*.xml </param-value> </context-param>
?实际上WEB-INF下并没有applicationContext*.xml(估计是没有配置正确),而是使用了dispatcher-servlet.xml
?
?
?
?
dispatcher-servlet.xml
?
<bean id="notificationController" class="org.androidpn.server.console.controller.NotificationController"> <property name="methodNameResolver" ref="paramResolver" /> </bean>
?在dispatcher-servlet.xml中配置了一个Controller,spring初始化时候会去调用NotificationController的构造方法来生成一个实例
?
?
?
NotificationApiController的构造方法
?
public NotificationApiController() { notificationManager = new NotificationManager(); }
?
?
?
NotificationManager的构造方法
?
/** * Constructor. */ public NotificationManager() { sessionManager = SessionManager.getInstance(); notificationService = ServiceLocator.getNotificationService(); userService = ServiceLocator.getUserService(); }
?ServiceLocator.getNotificationService()和ServiceLocator.getUserService()其实就是从spirng的bean工程中获取service方法不用太在意,下一步到SessionManager.getInstance()
?
?
?
SessionManager
?
private SessionManager() { serverName = XmppServer.getInstance().getServerName();//####启动了XmppServer,成为了重要线索 } /** * Returns the singleton instance of SessionManager. * * @return the instance */ public static SessionManager getInstance() { if (instance == null) { synchronized (SessionManager.class) { instance = new SessionManager(); } } return instance; }
?
?
?
Mina ?server
他的名字虽然是XmppServer,但其实只是load了另外一个spirng的xml,从整体的角度来说,XmppServer的作用是开启一个mina的监听server端,下面先来补充一下mina的知识
一句话概括:mina为开发高性能和高可用性的网络应用程序提供了非常便利的框架。也就是说它是一套网络通信框架,基于socket的。
他的demo如下
public class HelloServer { private static final int PORT = 8080; /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { IoAcceptor acceptor = new SocketAcceptor(); IoAcceptorConfig config = new SocketAcceptorConfig(); DefaultIoFilterChainBuilder chain = config.getFilterChain(); // 使用字符串编码 chain.addLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory())); // 启动HelloServer acceptor.bind(new InetSocketAddress(PORT), new HelloHandler(), config); System.out.println("HelloServer started on port " + PORT); }}/** * HelloServer的处理逻辑 * * @author liudong */class HelloHandler extends IoHandlerAdapter { /** * 当有异常发生时触发 */ @Override public void exceptionCaught(IoSession ssn, Throwable cause) { cause.printStackTrace(); ssn.close(); } /** * 有新连接时触发 */ @Override public void sessionOpened(IoSession ssn) throws Exception { System.out.println("session open for " + ssn.getRemoteAddress()); } /** * 连接被关闭时触发 */ @Override public void sessionClosed(IoSession ssn) throws Exception { System.out.println("session closed from " + ssn.getRemoteAddress()); } /** * 收到来自客户端的消息 */ public void messageReceived(IoSession ssn, Object msg) throws Exception { String ip = ssn.getRemoteAddress().toString(); System.out.println("===> Message From " + ip + " : " + msg); ssn.write("Hello " + msg); }}
?再引用一张架构图:
?
?整体表达的意思也就是,client的请求最后都会交给handler类去执行,用户自己的handler只要去继承IoHandlerAdapter这个父类,就可以拥有?sessionOpened,sessionClosed,messageReceived的功能,客户端的请求也最终分发到了handler类的对应方法
?
?
好了补充完了mina的知识再回到主题,刚才说到XmppServer去load了另外一个spirng.xml,下面是XmppServer.java的源码
?
/** * Returns the singleton instance of XmppServer. * * @return the server instance. */ public static XmppServer getInstance() { // return instance; if (instance == null) { synchronized (XmppServer.class) { instance = new XmppServer(); } } return instance; } /** * Constructor. Creates a server and starts it. */ public XmppServer() { if (instance != null) { throw new IllegalStateException("A server is already running"); } instance = this; start(); } /** * Starts the server using Spring configuration. */ public void start() { try { if (isStandAlone()) { Runtime.getRuntime().addShutdownHook(new ShutdownHookThread()); } // locateServer(); serverName = Config.getString("xmpp.domain", "127.0.0.1") .toLowerCase(); context = new ClassPathXmlApplicationContext("spring-config.xml"); //#######################没错这个xml的名字在这里 log.info("Spring Configuration loaded."); // AdminConsole adminConsole = new AdminConsole(serverHomeDir); // adminConsole.startup(); // if (adminConsole.isHttpStarted()) { // log.info("Admin console listening at http://" // + adminConsole.getAdminHost() + ":" // + adminConsole.getAdminPort()); // } log.info("XmppServer started: " + serverName); log.info("Androidpn Server v" + version); } catch (Exception e) { e.printStackTrace(); shutdownServer(); } }
?
?
?原来是spring-config.xml,下面则是spring-config.xml的关键内容
?
<bean id="xmppHandler" class="org.androidpn.server.xmpp.net.XmppIoHandler" /> <bean id="ioAcceptor" class="org.apache.mina.transport.socket.nio.NioSocketAcceptor" init-method="bind" destroy-method="unbind"> <property name="defaultLocalAddress" value=":5222" /> <property name="handler" ref="xmppHandler" /> <property name="filterChainBuilder" ref="filterChainBuilder" /> <property name="reuseAddress" value="true" /> </bean>
?
?原来,工程在spirng类中又配了一个bean,这个bean是org.apache.mina.transport.socket.nio.NioSocketAcceptor,并且ioc注入了监听的端口号,以及对应的handler类,打开工程中的org.androidpn.server.xmpp.net.XmppIoHandler类,我们就明白了,所有client的链接最初都被发到这个类中,然后再进行了相应的处理。
?
?
以上基本阐述了整个项目的基本流程,下篇文章,我将继续分析XmppIoHandler往下的流程,来看androidpn是如何实现实时推送和链接状态维护的
?
?
?
?
?
?
?
原创文章,转载请声名出处http://spjich.iteye.com/admin/blogs/2225830?
?
?
?