JDK6 Http server的设置

地球人都知道sun在jdk中六种自带了一个轻量级http server.
用它可以很容易实现一些轻量级的http服务,用来做测试和辅助工具很方便。
不过关于其的文档很少,参数调节等都没有文档说明,一切只能去看其源代码。
今天遇到一个问题:当客户端超过一定时间未发送数据,则连接被服务端自动关闭。
必须调整连接时间才行。

经过查找,JDK中,有个叫做ServerConfig的类,这里面设置了这个http server的一些必要参数,比如读取等待时间,写入等待时间等。

 

class ServerConfig {

    static int clockTick;

    static int defaultClockTick = 10000 ; // 10 sec.

    /* These values must be a reasonable multiple of clockTick */
    static long defaultReadTimeout = 20 ; // 20 sec.
    static long defaultWriteTimeout = 60 ; // 60 sec.
    static long defaultIdleInterval = 300 ; // 5 min
    static long defaultSelCacheTimeout = 120 ;  // seconds
    static int defaultMaxIdleConnections = 200 ; 

    static long defaultDrainAmount = 64 * 1024;

    static long readTimeout;
    static long writeTimeout;
    static long idleInterval;
    static long selCacheTimeout;	
    static long drainAmount;	// max # of bytes to drain from an inputstream
    static int maxIdleConnections;	
    static boolean debug = false;

    static {
	
	idleInterval = ((Long)java.security.AccessController.doPrivileged(
		new sun.security.action.GetLongAction(
		"sun.net.httpserver.idleInterval", 
		defaultIdleInterval))).longValue() * 1000;
	
	clockTick = ((Integer)java.security.AccessController.doPrivileged(
		new sun.security.action.GetIntegerAction(
		"sun.net.httpserver.clockTick", 
		defaultClockTick))).intValue();
	
	maxIdleConnections = ((Integer)java.security.AccessController.doPrivileged(
		new sun.security.action.GetIntegerAction(
		"sun.net.httpserver.maxIdleConnections", 
		defaultMaxIdleConnections))).intValue();
	
	readTimeout = ((Long)java.security.AccessController.doPrivileged(
		new sun.security.action.GetLongAction(
		"sun.net.httpserver.readTimeout", 
		defaultReadTimeout))).longValue()* 1000;
	
	selCacheTimeout = ((Long)java.security.AccessController.doPrivileged(
		new sun.security.action.GetLongAction(
		"sun.net.httpserver.selCacheTimeout", 
		defaultSelCacheTimeout))).longValue()* 1000;
	
	writeTimeout = ((Long)java.security.AccessController.doPrivileged(
		new sun.security.action.GetLongAction(
		"sun.net.httpserver.writeTimeout", 
		defaultWriteTimeout))).longValue()* 1000;
	
	drainAmount = ((Long)java.security.AccessController.doPrivileged(
		new sun.security.action.GetLongAction(
		"sun.net.httpserver.drainAmount", 
		defaultDrainAmount))).longValue();
	
	debug = ((Boolean)java.security.AccessController.doPrivileged(
		new sun.security.action.GetBooleanAction(
		"sun.net.httpserver.debug"))).booleanValue();
    }

    static long getReadTimeout () {
	return readTimeout;
    }

    static long getSelCacheTimeout () {
	return selCacheTimeout;
    }

    static boolean debugEnabled () {
	return debug;
    }

    static long getIdleInterval () {
	return idleInterval;
    }

    static int getClockTick () {
	return clockTick;
    }

    static int getMaxIdleConnections () {
	return maxIdleConnections;
    }

    static long getWriteTimeout () {
	return writeTimeout;
    }

    static long getDrainAmount () {
	return drainAmount;
    }
}

 从代码可知,修改这些细节参数,只要添加必要的java启动变量即可。

你可能感兴趣的:(jdk,http,server,sun,serverconfig)