⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jmsserver.java

📁 Java实现的Socket框架
💻 JAVA
字号:
/**
 * Project:ms4j (Message Server for Java)
 * Date:2007-2-21 
 * Authoer : jobsun@gmail.com
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 */
package com.sunjob.ms4j.server;

import java.io.IOException;
import java.net.ServerSocket;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.dom4j.Node;

import com.sunjob.ms4j.server.impl.ProtocolParserImpl;
import com.sunjob.ms4j.util.PropertyUtil;
import com.sunjob.ms4j.util.XMLUtil;

/**
 * @author sunjob
 * 
 */
public class JMSServer extends Thread{
	
	private final ServerSocket serverSocket;
    private final ExecutorService pool;
	private final HashMap context = new HashMap();
    
	public JMSServer() 
	throws Exception
	{
		this.initContext();
		int port = Integer.parseInt( PropertyUtil.getProperty("ms4j.server.port") );
		int poolSize = Integer.parseInt( PropertyUtil.getProperty("ms4j.server.pool.size") );
		serverSocket = new ServerSocket(port);
	    pool = Executors.newFixedThreadPool(poolSize);
	}
	
	private void initContext()
	throws Exception
	{
		XMLUtil xmltul = new XMLUtil(JMSServer.class.getResourceAsStream("/msgbean.xml"));
		List listNodes = xmltul.getElements("/msgbeans/msgbean");
		for(int i = 0 ; i < listNodes.size(); i++)
		{
			Node node = (Node)listNodes.get(i);
			String bean = node.valueOf("@name");
			String type = node.valueOf("@type");
			MsgProcesser mp = (MsgProcesser)Class.forName(type).newInstance();
			this.context.put(bean, mp);
		}
		//////////
		ProtocolParser pp = new ProtocolParserImpl();
		pp.setContext(context);
		this.context.put("@ParseProtocol", pp);
	}
	
	public void run()
	{
		try {
	        for (;;) {
	          pool.execute(new ServiceHandler(serverSocket.accept(), this.context));
	        }
	      } catch (IOException ex) {
	        pool.shutdown();
	      }
	}

	public static void main(String[] args) 
	throws Exception
	{
        System.out.println("+---------------------------------------+");
        System.out.println("|     Message Server for Java, v1.0     |");
        System.out.println("|       Author:sunjob ,2007             |");
        System.out.println("+---------------------------------------+");
        System.out.print("\nStarting ..... ");
        JMSServer js = new JMSServer();
        js.start();
        System.out.println("[OK]\n");
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -