- 注册时间
- 2011-3-22
- 最后登录
- 2013-6-27
- 在线时间
- 11644 小时
- 阅读权限
- 150
- 积分
- 62779
- 帖子
- 28923
- 精华
- 1
- UID
- 6
|
- Openfire和MINA
- 看了几天的openfire,网上的资料太少了,只有一个国外的网站不错,http://www.igniterealtime.org/ ,其他的只能自己摸索了。
- openfire启动:
- ServerStarter
- 会加载 org.jivesoftware.openfire.XMPPServer
- 在XMPPServer会加载一系列模块
- 其中的ConnectionManagerImpl 是连接模块
- // Load this module always last since we don't want to start listening for clients
- // before the rest of the modules have been started loadModule(ConnectionManagerImpl.class.getName());
- ConnectionManagerImpl 会启动一系列的监听。
- 其中的createClientListeners和startClientListeners是我比较关心的,先看看在这里面openfire都做了什么!
- private void createClientListeners() {
- // Start clients plain socket unless it's been disabled.
- if (isClientListenerEnabled()) {
- // Create SocketAcceptor with correct number of processors
- socketAcceptor = buildSocketAcceptor();
- // Customize Executor that will be used by processors to process incoming stanzas
- ExecutorThreadModel threadModel = ExecutorThreadModel.getInstance("client" );
- int eventThreads = JiveGlobals.getIntProperty("xmpp.client.processing.threads" , 16);
- ThreadPoolExecutor eventExecutor = (ThreadPoolExecutor)threadModel.getExecutor(); eventExecutor.setCorePoolSize(eventThreads + 1);
- eventExecutor.setMaximumPoolSize(eventThreads + 1);
- eventExecutor.setKeepAliveTime(60, TimeUnit.SECONDS );
- socketAcceptor .getDefaultConfig().setThreadModel(threadModel); // Add the XMPP codec filter socketAcceptor .getFilterChain().addFirst("xmpp" , new ProtocolCodecFilter(new XMPPCodecFactory()));
- // Kill sessions whose outgoing queues keep growing and fail to send traffic
- socketAcceptor .getFilterChain().addAfter("xmpp" , "outCap ", new StalledSessionsFilter()); } }
- 对了这里就是和用的mina框架去做联网处理,首先设置mina框架的线程池,然后把由XMPPCodecFactory做为 ProtocolCodecFilter的chain添加到FilterChain中!
- 然后
- private void startClientListeners(String localIPAddress) {
- // Start clients plain socket unless it's been disabled.
- if (isClientListenerEnabled()) {
- int port = getClientListenerPort();
- try { // Listen on a specific network interface if it has been set.
- String interfaceName = JiveGlobals.getXMLProperty("network.interface");
- InetAddress bindInterface = nu``ll;
- if (interfaceName != null) {
- if (interfaceName.trim().length() > 0) {
- bindInterface = InetAddress.getByName(interfaceName);
- } }
- // Start accepting connections
- socketAcceptor.bind(new InetSocketAddress(bindInterface, port), new ClientConnectionHandler(serverName)); ports.add(new ServerPort(port, serverName, localIPAddress, false, null, ServerPort.Type.client));
- List<String> params = new ArrayList<String>();
- params.add(Integer.toString(port));
- Log.info(LocaleUtils.getLocalizedString("startup.plain", params));
- } catch (Exception e) {
- System.err.println("Error starting XMPP listener on port " + port + ": " + e.getMessage());
- Log.error(LocaleUtils.getLocalizedString("admin.error.socket-setup"), e);
- } } }
- socketAcceptor.bind(new InetSocketAddress(bindInterface, port), new ClientConnectionHandler(serverName));
- 将ClientConnectionHandler作为数据处理
- 服务器去监听5222端口去了,mina真方便!
- 关于MINA框架可以去网上找找资料,这里就不说了。
- 这里要说下MINA中的IoHandler这个接口,IoHandler是最终面对用户的接口,看下这个接口中的方法:
- public interface IoHandler {
- /** * Invoked from an I/O processor thread when a new connection has been created.
- * Because this method is supposed to be called from the same thread that
- * handles I/O of multiple sessions, please implement this method to perform
- * tasks that consumes minimal amount of time such as socket parameter
- * and user-defined session attribute initialization.
- */
- void sessionCreated(IoSession session) throws Exception;
- /**
- * Invoked when a connection has been opened. This method is invoked after
- * {@link #sessionCreated(IoSession)}. The biggest difference from
- * {@link #sessionCreated(IoSession)} is that it's invoked from other thread
- * than an I/O processor thread once thread model is configured properly.
- */
- void sessionOpened(IoSession session) throws Exception;
- /**
- * Invoked when a connection is closed.
- */ void sessionClosed(IoSession session) throws Exception;
- /**
- * Invoked with the related {@link IdleStatus} when a connection becomes idle.
- * This method is not invoked if the transport type is UDP; it's a known bug,
- * and will be fixed in 2.0.
- */
- void sessionIdle(IoSession session, IdleStatus status) throws Exception;
- /**
- * Invoked when any exception is thrown by user {@link IoHandler}
- * implementation or by MINA. If <code>cause</code> is an instance of
- * {@link IOException}, MINA will close the connection automatically.
- */
- void exceptionCaught(IoSession session, Throwable cause) throws Exception;
- /**
- * Invoked when a message is received.
- */
- void messageReceived(IoSession session, Object message) throws Exception;
- /**
- * Invoked when a message written by {@link IoSession#write(Object)} is
- * sent out.
- */
- void messageSent(IoSession session, Object message) throws Exception;}
- 在mina中实现这个接口的类是IoHandlerAdapter 这个类
- /**
- * An abstract adapter class for {@link IoHandler}. You can extend this
- * class and selectively override required event handler methods only. All
- * methods do nothing by default.
- *
- * @author The Apache MINA Project (dev@mina.apache.org)
- * @version $Rev: 671827 $, $Date: 2008-06-26 10:49:48 +0200 (jeu, 26 jun 2008) $
- */
- public class IoHandlerAdapter implements IoHandler {
- private final Logger logger = LoggerFactory.getLogger(getClass());
- public void sessionCreated(IoSession session) throws Exception {
- }
- public void sessionOpened(IoSession session) throws Exception {
- }
- public void sessionClosed(IoSession session) throws Exception {
- }
- public void sessionIdle(IoSession session, IdleStatus status)throws Exception {
- }
- public void exceptionCaught(IoSession session, Throwable cause)throws Exception {
- if (logger.isWarnEnabled()) {
- logger.warn("EXCEPTION, please implement "+ getClass().getName()+ ".exceptionCaught() for proper handling:", cause);
- }
- }
- public void messageReceived(IoSession session, Object message)throws Exception {
- }
- public void messageSent(IoSession session, Object message) throws Exception {
- }
- }
- 接下来转到openfire,在openfire中ConnectionHandler类继承自IoHandlerAdapter,也就充当着最后要面对用户的角色。
- public abstract class ConnectionHandler extends IoHandlerAdapter
复制代码 |
|